mirror of
https://github.com/jlengrand/FunctionalProgrammingScalaCoursera.git
synced 2026-03-10 08:21:22 +00:00
Get countChange working
This commit is contained in:
10
example/.idea/modules/root.iml
generated
10
example/.idea/modules/root.iml
generated
@@ -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>
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user