Skip to content

Instantly share code, notes, and snippets.

@kingsleyh
Created September 30, 2015 18:21
Show Gist options
  • Save kingsleyh/2516d682a57367686c07 to your computer and use it in GitHub Desktop.
Save kingsleyh/2516d682a57367686c07 to your computer and use it in GitHub Desktop.
pirate treasure
import std.stdio;
import std.conv;
import std.exception;
// rdmd -unittest --main pirate_treasure.d
class FairTreasureSplitNotPossible : Exception {
this (string msg) {
super(msg);
}
}
class PirateTreasure {
public int numberPirates;
public int[] gems;
this(int numberPirates, int[] gems){
this.numberPirates = numberPirates;
this.gems = gems;
}
public int[][] split(){
return [[]];
}
}
unittest{
// successful fair split
PirateTreasure pt1 = new PirateTreasure(2,[9,12,14,17,23,32,34,40,42,49]);
assert( pt1.split() == [[9,12,32,34,49],[14,17,23,40,42]],"Expected the treasure to be split fairly but it was not");
// could not split fairly
PirateTreasure pt2 = new PirateTreasure(3,[1,2,3,4]);
assertThrown!FairTreasureSplitNotPossible(pt2.split(), "Expected an exception to be thrown when the treasure cannot be fairly split amongst the pirates");
}
@dudeOFprosper
Copy link

Yarrrrrrr!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment