Skip to content

Instantly share code, notes, and snippets.

@jcsirot jcsirot/Coin.java
Last active Dec 11, 2015

Embed
What would you like to do?
Implementation de Scalaskel
public enum Coin
{
FOO(1),
BAR(7),
QIX(11),
BAZ(21);
private final int value;
Coin(int value)
{
this.value = value;
}
int getValue()
{
return value;
}
}
import java.util.*;
public class Partition
{
public static List<Partition> computePartitions(int value)
{
return computePartitions(value, Arrays.asList(Coin.FOO, Coin.BAR, Coin.QIX, Coin.BAZ));
}
private static List<Partition> computePartitions(int value, List<Coin> coins)
{
if (coins.isEmpty()) {
return new ArrayList<Partition>();
}
Coin c = coins.get(0);
int x = c.getValue();
List<Partition> partitions;
if (value > x) {
partitions = computePartitions(value - x, coins);
for (Partition p: partitions) {
p.add(c);
}
} else if (value == x) {
partitions = new ArrayList<Partition>();
partitions.add(new Partition(c));
} else {
partitions = new ArrayList<Partition>();
}
partitions.addAll(computePartitions(value, coins.subList(1, coins.size())));
return partitions;
}
private int foo, bar, qix, baz;
public Partition(int foo, int bar, int qix, int baz)
{
this.foo = foo;
this.bar = bar;
this.qix = qix;
this.baz = baz;
}
public Partition(Coin c)
{
this(0, 0, 0, 0);
add(c);
}
public void add(Coin c)
{
switch (c) {
case BAR:
this.bar += 1;
break;
case FOO:
this.foo += 1;
break;
case QIX:
this.qix += 1;
break;
case BAZ:
this.baz += 1;
break;
}
}
public int getFoo()
{
return foo;
}
public int getBar()
{
return bar;
}
public int getQix()
{
return qix;
}
public int getBaz()
{
return baz;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.