Last active
December 12, 2015 01:18
-
-
Save ggtools/4689818 to your computer and use it in GitHub Desktop.
CodeStory 2013 : l'échoppe de monade sur Scalaskel en Java
This file contains hidden or 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
private static void addToChange(COIN currentCoin, int value, Map<COIN, Integer> changeMap) { | |
if (value != 0) { | |
changeMap.put(currentCoin, value); | |
} | |
} | |
// Méthode principale on l'appelle avec computeChange(COIN.baz, value) afin en commençant donc par la plus grosse | |
// pièce. | |
public List<Map<COIN, Integer>> computeChange(COIN currentCoin, int value) { | |
if (currentCoin.getValue() == 1) { | |
Map<COIN, Integer> changeMap = Maps.newHashMapWithExpectedSize(COIN.values().length); | |
addToChange(currentCoin, value, changeMap); | |
return Lists.newArrayList(changeMap); | |
} | |
int maxCoins = value / currentCoin.getValue(); | |
List<Map<COIN, Integer>> results = Lists.newArrayList(); | |
for (int i = 0; i <= maxCoins; i++) { | |
List<Map<COIN, Integer>> subChanges = computeChange(COIN.values()[currentCoin.ordinal() - 1], value - i * currentCoin.getValue()); | |
for (Map<COIN, Integer> change : subChanges) { | |
addToChange(currentCoin, i, change); | |
results.add(change); | |
} | |
} | |
return results; | |
} | |
protected enum COIN { | |
//Foo vaut 1 cent, le Bar vaut 7 cents, le Qix vaut 11 cents et le Baz vaut 21 cents. | |
foo(1), bar(7), qix(11), baz(21); | |
private final int value; | |
private COIN(int value) { | |
this.value = value; | |
} | |
public int getValue() { | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment