Created
March 27, 2014 20:54
-
-
Save peter-tackage/9818470 to your computer and use it in GitHub Desktop.
Integer array incrementer
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
public class Incrementer { | |
public static int[] increment(int[] input) { | |
int index = input.length -1; | |
while(++input[index] % 10 == 0) { | |
input[index] = input[index] % 10; | |
if(index == 0) { | |
int[] result = new int[input.length + 1]; | |
result[0] = 1; | |
return result; | |
} | |
index--; | |
} | |
return input; | |
} | |
} |
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
public class IncrementerTest extends TestCase { | |
public void test_incrementSingleFromZero() { | |
assertEquals(new int[] { 1 }, Incrementer.increment(new int[] { 0 })); | |
} | |
public void test_incrementSingleFromNine() { | |
assertEquals(new int[] { 1, 0 }, Incrementer.increment(new int[] { 9 })); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment