Created
November 10, 2014 00:17
-
-
Save jamesdaily/990083c9fcd28f9afbb5 to your computer and use it in GitHub Desktop.
Cut the sticks! (HackerRank)
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
package com.jamesdaily.study; | |
public class Stick implements Comparable<Stick> { | |
protected Integer length; | |
public Stick(Integer length) { | |
if (length == 0) | |
throw new IllegalArgumentException("Stick cannot be length 0!"); | |
if (length < 0) | |
throw new IllegalArgumentException( | |
"Stick cannot be negative length!"); | |
setLength(length); | |
} | |
public Integer getLength() { | |
return length; | |
} | |
protected void setLength(Integer length) { | |
this.length = length; | |
} | |
public Integer reduceLengthBy(Integer cutLength) { | |
if (this.getLength() < cutLength) | |
throw new IllegalArgumentException("Cannot cut to negative length!"); | |
this.setLength(Math.max(0, this.length - cutLength)); | |
return this.getLength(); | |
} | |
public Integer reduceLengthBy(Stick shorterStick) { | |
return this.reduceLengthBy(shorterStick.getLength()); | |
} | |
@Override | |
public int compareTo(Stick otherStick) { | |
return this.getLength() > otherStick.getLength() ? 1 | |
: this.getLength() < otherStick.getLength() ? -1 : 0; | |
} | |
@Override | |
public String toString() { | |
return this.length.toString(); | |
} | |
} |
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
package com.jamesdaily.study; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.NoSuchElementException; | |
public class StickCutter { | |
protected List<Stick> sticks; | |
public StickCutter(Integer[] stickLengths) { | |
this.sticks = new ArrayList<Stick>(); | |
for (Integer len : stickLengths) { | |
this.sticks.add(new Stick(len)); | |
} | |
} | |
public Stick getShortestStick() { | |
if (this.sticks.size() == 0) | |
return null; | |
Stick shorty = Collections.min(this.sticks); | |
return shorty; | |
} | |
public void cutBySmallestStick() { | |
if (this.sticks.size() == 0) | |
return; | |
Stick shortestStick = this.getShortestStick(); | |
Integer shortestLength = shortestStick.getLength(); | |
List<Stick> remainingSticks = new ArrayList<Stick>(); | |
for (Stick stick : sticks) { | |
if (stick.reduceLengthBy(shortestLength) > 0) | |
remainingSticks.add(stick); | |
} | |
this.sticks = remainingSticks; | |
} | |
} |
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
package com.jamesdaily.study; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class StickCutterTest { | |
StickCutter sc; | |
final Integer[] stickLengths = { 6, 3, 1, 12, 5, 6, 8, 3, 3, 9, 5, 7, 1 }; | |
@Before | |
public void setUp() throws Exception { | |
sc = new StickCutter(stickLengths); | |
} | |
@Test | |
public void testGetShortestStick() { | |
Stick shortestStick = sc.getShortestStick(); | |
assertTrue(1 == shortestStick.getLength()); | |
} | |
@Test | |
public void testCutBySmallestStick() { | |
System.out.println(sc.getShortestStick().getLength()); | |
sc.cutBySmallestStick(); // there are now three 2's (formerly 3's) | |
System.out.println(sc.sticks.toString()); | |
assertTrue(sc.sticks.size() == (stickLengths.length - 2)); | |
System.out.println(sc.getShortestStick().getLength()); | |
sc.cutBySmallestStick(); // there are not two 1's (formerly 5's) | |
System.out.println(sc.sticks.toString()); | |
assertTrue(sc.sticks.size() == (stickLengths.length - (2 + 3))); | |
System.out.println(sc.getShortestStick().getLength()); | |
sc.cutBySmallestStick(); | |
System.out.println(sc.sticks.toString()); | |
assertTrue(sc.sticks.size() == (stickLengths.length - (2 + 3 + 2))); | |
} | |
@Test | |
public void testPileOfOne() { | |
final Integer[] oneStickPile = { 6 }; // there are two 1's | |
sc = new StickCutter(oneStickPile); | |
assertTrue(6 == sc.getShortestStick().getLength()); | |
System.out.println("Before: " + sc.sticks.toString()); | |
sc.cutBySmallestStick(); | |
System.out.println("After: " + sc.sticks.toString()); | |
assertTrue(sc.sticks.size() == 0); // there are now three 2's (formerly | |
// 3's) | |
sc.cutBySmallestStick(); | |
assertTrue(sc.sticks.size() == 0); // still empty | |
} | |
} |
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
package com.jamesdaily.study; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class StickTest { | |
Stick sevenStick; | |
Stick fiveStick; | |
@Before | |
public void setUp() throws Exception { | |
sevenStick = new Stick(7); | |
fiveStick = new Stick(5); | |
} | |
@Test | |
public void testReduceLengthByInteger() { | |
assertTrue((7 - 3) == sevenStick.reduceLengthBy(3)); | |
} | |
@Test | |
public void testReduceLengthByStick() { | |
assertTrue((7 - 5) == sevenStick.reduceLengthBy(fiveStick)); | |
} | |
@Test | |
public void testReduceLengthByLongerStick() { | |
try { | |
fiveStick.reduceLengthBy(sevenStick); | |
fail("Should have thrown!"); | |
} catch (IllegalArgumentException e) { | |
} | |
} | |
// @Test | |
public void testCompareTo() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment