Created
January 31, 2013 03:42
-
-
Save ybonnel/4679871 to your computer and use it in GitHub Desktop.
Scalaskel en ceylon
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
import java.util { ArrayList } | |
import ceylon.collection { LinkedList } | |
shared class Coin(Integer coinValue) { | |
shared Integer valeur = coinValue; | |
shared Boolean canPay(Integer centsToPay) { | |
return centsToPay >= valeur; | |
} | |
} | |
Coin[] possibleCoins = { | |
Coin(1), | |
Coin(7), | |
Coin(11), | |
Coin(21) | |
}; | |
shared class Change(Change? currentChange = null) { | |
shared variable Integer foo := currentChange?.foo else 0; | |
shared variable Integer bar := currentChange?.bar else 0; | |
shared variable Integer qix := currentChange?.qix else 0; | |
shared variable Integer baz := currentChange?.baz else 0; | |
shared void pay(Integer coin) { | |
if (coin == 1) { | |
foo++; | |
} | |
if (coin == 7) { | |
bar++; | |
} | |
if (coin == 11) { | |
qix++; | |
} | |
if (coin == 21) { | |
baz++; | |
} | |
} | |
} | |
shared class Scalaskel() { | |
shared LinkedList<Change> calculate(Integer cents) { | |
return completeChanges(cents, Change(), Coin(1)); | |
} | |
LinkedList<Change> completeChanges(Integer cents, Change currentChange, Coin lastCoin) { | |
// Stop condition of recursivity | |
value changes = LinkedList<Change>(); | |
if (cents == 0) { | |
changes.add(currentChange); | |
return changes; | |
} | |
for (Coin coin in possibleCoins) { | |
if (lastCoin.valeur <= coin.valeur && coin.canPay(cents)) { | |
Change change = Change(currentChange); | |
change.pay(coin.valeur); | |
for (Change newChange in completeChanges(cents - coin.valeur, change, coin)) { | |
changes.add(newChange); | |
} | |
} | |
} | |
return changes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment