Created
March 1, 2023 23:05
-
-
Save renanreismartins/3d2ad063caa82be5c7c3f9406fde8ae4 to your computer and use it in GitHub Desktop.
move the stop to the beginning
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
object CanSum extends App { | |
def canSum(n: Int, coins: Array[Int]): Boolean = { | |
if (n == 0) return true | |
if (n < 0) return false | |
for (coin <- coins) { | |
val rest = n - coin | |
return canSum(rest, coins) | |
} | |
false | |
} | |
println(canSum(7, Array[Int](7))) | |
println(canSum(7, Array[Int](1))) | |
println(canSum(7, Array[Int](1, 7))) | |
println() | |
println(canSum(7, Array[Int](1, 2, 3, 8, 7))) | |
println(canSum(7, Array[Int]( 2, 3, 8, 7))) | |
println() | |
println(canSum(300, Array[Int](7, 14))) | |
println(canSum(7, Array[Int](8))) | |
println(canSum(7, Array[Int](6, 7))) | |
println(canSum(7, Array[Int](5, 3, 4, 7))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment