Get countChange working

This commit is contained in:
julien Lengrand-Lambert
2017-03-19 11:51:03 +01:00
parent 4ce7d36811
commit 0c330f21a6
2 changed files with 32 additions and 27 deletions

View File

@@ -24,11 +24,11 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="SBT: junit:junit:4.10:jar" level="project" />
<orderEntry type="library" scope="TEST" name="SBT: org.hamcrest:hamcrest-core:1.1:jar" level="project" />
<orderEntry type="library" name="SBT: org.scala-lang:scala-library:2.11.7:jar" level="project" />
<orderEntry type="library" name="SBT: org.scala-lang:scala-reflect:2.11.2:jar" level="project" />
<orderEntry type="library" name="SBT: org.scala-lang.modules:scala-parser-combinators_2.11:1.0.4:jar" level="project" />
<orderEntry type="library" name="SBT: org.scala-lang.modules:scala-xml_2.11:1.0.2:jar" level="project" />
<orderEntry type="library" name="SBT: org.scalatest:scalatest_2.11:2.2.4:jar" level="project" />
<orderEntry type="library" name="SBT: org.scala-lang.modules:scala-xml_2.11:1.0.2:jar" level="project" />
<orderEntry type="library" name="SBT: org.scala-lang.modules:scala-parser-combinators_2.11:1.0.4:jar" level="project" />
<orderEntry type="library" name="SBT: org.scala-lang:scala-reflect:2.11.2:jar" level="project" />
<orderEntry type="library" name="SBT: org.scala-lang:scala-library:2.11.7:jar" level="project" />
<orderEntry type="library" scope="TEST" name="SBT: org.hamcrest:hamcrest-core:1.1:jar" level="project" />
</component>
</module>

View File

@@ -11,30 +11,35 @@ object Main {
}
/**
* Exercise 1
*/
def pascal(c: Int, r: Int): Int =
if (r == c || c == 0) 1
else pascal(c, r - 1) + pascal(c - 1, r - 1)
/**
* Exercise 2
*/
def balance(chars: List[Char]): Boolean = {
def balanced(chars: List[Char], currentBalance: Int) : Boolean = {
if(chars.isEmpty) currentBalance == 0
else if(chars.head == ')' && currentBalance == 0) false
else if(chars.head == ')') balanced(chars.tail, currentBalance - 1)
else if(chars.head == '(') balanced(chars.tail, currentBalance + 1)
else balanced(chars.tail, currentBalance)
}
* Exercise 1
*/
def pascal(c: Int, r: Int): Int =
if (r == c || c == 0) 1
else pascal(c, r - 1) + pascal(c - 1, r - 1)
balanced(chars, 0)
/**
* Exercise 2
*/
def balance(chars: List[Char]): Boolean = {
def balanced(chars: List[Char], currentBalance: Int) : Boolean = {
if(chars.isEmpty) currentBalance == 0
else if(chars.head == ')' && currentBalance == 0) false
else if(chars.head == ')') balanced(chars.tail, currentBalance - 1)
else if(chars.head == '(') balanced(chars.tail, currentBalance + 1)
else balanced(chars.tail, currentBalance)
}
balanced(chars, 0)
}
/**
* Exercise 3
*/
def countChange(money: Int, coins: List[Int]): Int = ???
* Exercise 3
*/
def countChange(money: Int, coins: List[Int]): Int = {
if (money < 0) 0
else if(coins.isEmpty) 0
else if(money == 0) 1
else countChange(money , coins.tail) + countChange(money - coins.head, coins)
}
}