Last active
June 2, 2020 13:41
-
-
Save RyanSusana/d3dde5d402a51877fd789f5e674359d9 to your computer and use it in GitHub Desktop.
ScalaWeek1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Excercise 1 | |
def pascal(c: Int, r: Int): Int = | |
if (c == 0 || r == c) 1 else pascal(c - 1, r - 1) + pascal(c, r - 1) | |
// Excercise 2 | |
def balance(chars: List[Char]): Boolean = { | |
def loop(i: Int, amountOpen: Int): Boolean = | |
if (i >= chars.size) amountOpen == 0 | |
else if (amountOpen < 0) false | |
else if (chars(i).equals('(')) loop(i + 1, amountOpen + 1) | |
else if (chars(i).equals(')')) loop(i + 1, amountOpen - 1) | |
else loop(i + 1, amountOpen) | |
loop(0, 0) | |
} | |
//Exercise 3 | |
def countChange(money: Int, coins: List[Int]): Int ={ | |
if (money < 0 || coins.isEmpty) | |
0 | |
else if (money == 0) | |
1 | |
else | |
countChange(money - coins.head, coins) + countChange(money, coins.tail) | |
} | |
//Excercise 4 | |
def factorial(n: Int): Int = | |
if (n == 0) 1 else n * factorial(n-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment