diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7bd5f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea/* +*.iml +target/* +*.iws +.DS_Store +log/* \ No newline at end of file diff --git a/README.md b/README.md index 3aa6b74..4903349 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,33 @@ 2. Write a function that takes a BitSet and returns the number of steps needed to shift all `1` bits to the left (maintaining the same `length()` of the BitSet). -3. Write a function that takes a BitSet and returns the number of steps needed to shift all `1` bits to the right. \ No newline at end of file +3. Write a function that takes a BitSet and returns the number of steps needed to shift all `1` bits to the right. + +## Stack Microlab + +A stack is a data structure that stores a list of elements. Each element is added with the `push(element)` method and the most recent element can be retreived with the `pop()` method. The `peek()` method returns the top element on the stack without removing it from the stack. The **last** element put **in** the stack is the **first** one to come **out** -- this is why it's called a **Last In First Out** (LIFO) data structure. + +Create your own Stack for Strings called `StringStack` and implement these three methods. + +**Part 2**: + +Generalize your StringStack to a generic Stack implementation. It should include appropriate type parameters and allow storage of any object type. + +*hint*: There may be another existing collection that is useful for implementing a stack. + +## Listbuilder + +Create a ListBuilder interface with one method: `buildList(Array a):List` + +Implement the interface and its method with the following classes: + +- `ArrayListBuilder` -- Creates an ArrayList from the given array +- `LinkedListBuilder` -- Creates a LinkedList from the given array + +Part 2: Override `buildList` with `buildList(Collection c):List` to take any collection type and produce the corresponding List type to the implementing class (just like part one). You may want to look at the `Collection.toArray()` method for this. + + +## Iterator + +Create an `Iterator` that produces the Fibonacci series; call it `FibonacciIterator`. The `hasNext()` method should always return true because the Fibonacci series is an infinite set. + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..625b977 --- /dev/null +++ b/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + + kim.christopher + BitSetMicroLab + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + + + + jar + + BitSetMicroLab + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.12 + test + + + diff --git a/src/main/java/kim/christopher/bitwise/LightSwitcher.java b/src/main/java/kim/christopher/bitwise/LightSwitcher.java new file mode 100644 index 0000000..57c5533 --- /dev/null +++ b/src/main/java/kim/christopher/bitwise/LightSwitcher.java @@ -0,0 +1,104 @@ +package kim.christopher.bitwise; + +import java.util.BitSet; + +public class LightSwitcher { + + public static BitSet turnOnSwitches(BitSet switches, BitSet switchesToTurnOn) { + BitSet result = (BitSet) switches.clone(); + result.or(switchesToTurnOn); + return result; + } + + public static BitSet turnOnAllSwitches(BitSet switches) { + BitSet result = (BitSet) switches.clone(); + result.or(fromString("11111111")); + return result; + } + + public static BitSet turnOffSwitches(BitSet switches, BitSet switchesToTurnOff) { + BitSet result = (BitSet) switches.clone(); + result.andNot(switchesToTurnOff); + return result; + } + + public static BitSet turnOffAllSwitches(BitSet switches) { + return new BitSet(); + } + + + public static BitSet flipSwitches(BitSet switches, BitSet switchesToFlip) { + BitSet result = (BitSet) switches.clone(); + for (int i = 0; i < 8; i++) { + if (switchesToFlip.get(i)) + result.flip(i); + } + return result; + } + + public static BitSet flipAllSwitches(BitSet switches) { + BitSet result = (BitSet) switches.clone(); + for (int i = 0; i < 8; i++) + result.flip(i); + return result; + } + + public static int getSwitchPositionAt(BitSet switches, int position) { + return switches.get(position) ? 1 : 0; + } + + public static BitSet moveRightBy(BitSet switches, int count) { + BitSet result = (BitSet) switches.clone(); + for (int i = 0; i < count; i++) { + for (int j = 1; j < 9; j++) { + if (result.get(j)) + result.set(j - 1); + else result.set(j - 1, false); + } + } + return result; + } + + public static BitSet moveLeftBy(BitSet switches, int count) { + BitSet result = (BitSet) switches.clone(); + for (int i = 0; i < count; i++) { + for (int j = 6; j >= 0; j--) { + if (result.get(j)) + result.set(j + 1); + else result.set(j + 1, false); + } + result.set(0, false); + } + return result; + } + + public static String viewSwitches(int switches) { + return String.format("%8s", Integer.toBinaryString((switches & 0b11111111))).replace(' ', '0'); + } + + public static BitSet fromString(final String s) { + return BitSet.valueOf(new long[]{Long.parseLong(s, 2)}); + } + + public static int leftShiftSteps(BitSet bs) { + int count = 0; + int result = 0; + for (int i = 7; i >= 0; i--) { + if (bs.get(i)) + result += count; + else count++; + } + return result; + } + + public static int rightShiftSteps(BitSet bs) { + int count = 0; + int result = 0; + for (int i = 0; i < 8; i++) { + if (bs.get(i)) + result += count; + else count++; + } + return result; + } +} \ No newline at end of file diff --git a/src/main/java/kim/christopher/stack/StringStack.java b/src/main/java/kim/christopher/stack/StringStack.java new file mode 100644 index 0000000..bbfa935 --- /dev/null +++ b/src/main/java/kim/christopher/stack/StringStack.java @@ -0,0 +1,24 @@ +package kim.christopher.stack; + +import java.util.LinkedList; + +public class StringStack { + + LinkedList stack; + + public StringStack() { + stack = new LinkedList<>(); + } + + public void push(String input){ + stack.push(input); + } + + public String pop(){ + return stack.pop(); + } + + public String peek(){ + return stack.peek(); + } +} diff --git a/src/test/java/kim/christopher/bitwise/LightSwitcherTest.java b/src/test/java/kim/christopher/bitwise/LightSwitcherTest.java new file mode 100644 index 0000000..56639cb --- /dev/null +++ b/src/test/java/kim/christopher/bitwise/LightSwitcherTest.java @@ -0,0 +1,241 @@ +package kim.christopher.bitwise; + +import kim.christopher.bitwise.LightSwitcher; +import org.junit.Before; +import org.junit.Test; + +import java.util.BitSet; + +import static kim.christopher.bitwise.LightSwitcher.*; +import static org.junit.Assert.assertEquals; + +public class LightSwitcherTest { + private BitSet baseByte; + private BitSet flippedBaseByte; + private BitSet allOnes; + private BitSet allZeros; + + @Before + public void initialize() { + baseByte = fromString("10101010"); + flippedBaseByte = fromString("01010101"); + allOnes = fromString("11111111"); + allZeros = fromString("00000000"); + } + + @Test + public void testTurnOnSwitches() { + BitSet result1 = turnOnSwitches(baseByte, fromString("11110000")); + BitSet expected1 = fromString("11111010"); + BitSet result2 = turnOnSwitches(flippedBaseByte, fromString("11110000")); + BitSet expected2 = fromString("11110101"); + BitSet result3 = turnOnSwitches(baseByte, fromString("00001111")); + BitSet expected3 = fromString("10101111"); + BitSet result4 = turnOnSwitches(flippedBaseByte, fromString("00001111")); + BitSet expected4 = fromString("01011111"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); + } + + @Test + public void testTurnOnAllSwitches() { + BitSet result1 = turnOnAllSwitches(baseByte); + BitSet result2 = turnOnAllSwitches(flippedBaseByte); + BitSet result3 = turnOnAllSwitches(allOnes); + BitSet result4 = turnOnAllSwitches(allZeros); + assertEquals(allOnes, result1); + assertEquals(allOnes, result2); + assertEquals(allOnes, result3); + assertEquals(allOnes, result4); + } + + @Test + public void testTurnOffSwitches() { + BitSet result1 = turnOffSwitches(baseByte, fromString("11110000")); + BitSet expected1 = fromString("00001010"); + BitSet result2 = turnOffSwitches(flippedBaseByte, fromString("11110000")); + BitSet expected2 = fromString("00000101"); + BitSet result3 = turnOffSwitches(baseByte, fromString("00001111")); + BitSet expected3 = fromString("10100000"); + BitSet result4 = turnOffSwitches(flippedBaseByte, fromString("00001111")); + BitSet expected4 = fromString("01010000"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); + } + + @Test + public void testTurnOffAllSwitches() { + BitSet result1 = turnOffAllSwitches(baseByte); + BitSet result2 = turnOffAllSwitches(flippedBaseByte); + BitSet result3 = turnOffAllSwitches(allOnes); + BitSet result4 = turnOffAllSwitches(allZeros); + assertEquals(allZeros, result1); + assertEquals(allZeros, result2); + assertEquals(allZeros, result3); + assertEquals(allZeros, result4); + } + + @Test + public void testFlipSwitches() { + BitSet result1 = flipSwitches(baseByte, fromString("11110000")); + BitSet expected1 = fromString("01011010"); + BitSet result2 = flipSwitches(flippedBaseByte, fromString("11110000")); + BitSet expected2 = fromString("10100101"); + BitSet result3 = flipSwitches(baseByte, fromString("00001111")); + BitSet expected3 = fromString("10100101"); + BitSet result4 = flipSwitches(flippedBaseByte, fromString("00001111")); + BitSet expected4 = fromString("01011010"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); + } + + @Test + public void testFlipAllSwitches() { + BitSet result1 = flipAllSwitches(baseByte); + BitSet result2 = flipAllSwitches(flippedBaseByte); + BitSet result3 = flipAllSwitches(allOnes); + BitSet result4 = flipAllSwitches(allZeros); + assertEquals(flippedBaseByte, result1); + assertEquals(baseByte, result2); + assertEquals(allZeros, result3); + assertEquals(allOnes, result4); + } + + @Test + public void testGetSwitchPositionAt() { + for(int i = 0; i < 8; i++) { + assertEquals(i%2, getSwitchPositionAt(baseByte, i)); + } + for(int i = 0; i < 8; i++) { + assertEquals((i + 1) % 2, getSwitchPositionAt(flippedBaseByte, i)); + } + assertEquals(0, getSwitchPositionAt(fromString("11111110"), 0)); + assertEquals(1, getSwitchPositionAt(fromString("11111110"), 1)); + assertEquals(1, getSwitchPositionAt(fromString("11111110"), 7)); + } + + @Test + public void testMoveRightBy() { + BitSet result1 = moveRightBy(baseByte, 1); + BitSet result2 = moveRightBy(baseByte, 2); + BitSet result3 = moveRightBy(baseByte, 3); + BitSet result4 = moveRightBy(baseByte, 4); + BitSet result5 = moveRightBy(baseByte, 5); + BitSet result6 = moveRightBy(baseByte, 6); + BitSet result7 = moveRightBy(baseByte, 7); + BitSet result8 = moveRightBy(baseByte, 8); + BitSet expected1 = fromString("01010101"); + BitSet expected2 = fromString("00101010"); + BitSet expected3 = fromString("00010101"); + BitSet expected4 = fromString("00001010"); + BitSet expected5 = fromString("00000101"); + BitSet expected6 = fromString("00000010"); + BitSet expected7 = fromString("00000001"); + BitSet expected8 = fromString("00000000"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); + assertEquals(expected5, result5); + assertEquals(expected6, result6); + assertEquals(expected7, result7); + assertEquals(expected8, result8); + } + + @Test + public void testMoveLeftBy() { + BitSet result1 = moveLeftBy(baseByte, 1); + BitSet result2 = moveLeftBy(baseByte, 2); + BitSet result3 = moveLeftBy(baseByte, 3); + BitSet result4 = moveLeftBy(baseByte, 4); + BitSet result5 = moveLeftBy(baseByte, 5); + BitSet result6 = moveLeftBy(baseByte, 6); + BitSet result7 = moveLeftBy(baseByte, 7); + BitSet result8 = moveLeftBy(baseByte, 8); + BitSet expected1 = fromString("01010100"); + BitSet expected2 = fromString("10101000"); + BitSet expected3 = fromString("01010000"); + BitSet expected4 = fromString("10100000"); + BitSet expected5 = fromString("01000000"); + BitSet expected6 = fromString("10000000"); + BitSet expected7 = fromString("00000000"); + BitSet expected8 = fromString("00000000"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); + assertEquals(expected5, result5); + assertEquals(expected6, result6); + assertEquals(expected7, result7); + assertEquals(expected8, result8); + } + + @Test + public void testViewSwitches() { + assertEquals("11111111", LightSwitcher.viewSwitches(0b111111111111)); + assertEquals("00000000", LightSwitcher.viewSwitches(0)); + assertEquals("01010101", LightSwitcher.viewSwitches(0b111101010101)); + } + + @Test + public void leftShiftStepsTest(){ + //Given + int expected1 = 6; + int expected2 = 10; + int expected3 = 12; + int expected4 = 4; + int expected5 = 16; + int expected6 = 8; + + //When + int actual1 = leftShiftSteps(baseByte); + int actual2 = leftShiftSteps(flippedBaseByte); + int actual3 = leftShiftSteps(fromString("00110011")); + int actual4 = leftShiftSteps(fromString("11001100")); + int actual5 = leftShiftSteps(fromString("00001111")); + int actual6 = leftShiftSteps(fromString("00111100")); + + //Then + assertEquals("Number of steps should be 6", expected1, actual1); + assertEquals("Number of steps should be 10", expected1, actual1); + assertEquals("Number of steps should be 12", expected1, actual1); + assertEquals("Number of steps should be 4", expected1, actual1); + assertEquals("Number of steps should be 16", expected1, actual1); + assertEquals("Number of steps should be 8", expected1, actual1); + + } + + @Test + public void rightShiftStepsTest(){ + //Given + int expected1 = 10; + int expected2 = 6; + int expected3 = 4; + int expected4 = 12; + int expected5 = 0; + int expected6 = 8; + + //When + int actual1 = rightShiftSteps(baseByte); + int actual2 = rightShiftSteps(flippedBaseByte); + int actual3 = rightShiftSteps(fromString("00110011")); + int actual4 = rightShiftSteps(fromString("11001100")); + int actual5 = rightShiftSteps(fromString("00001111")); + int actual6 = rightShiftSteps(fromString("00111100")); + + //Then + assertEquals("Number of steps should be 10", expected1, actual1); + assertEquals("Number of steps should be 6", expected1, actual1); + assertEquals("Number of steps should be 4", expected1, actual1); + assertEquals("Number of steps should be 12", expected1, actual1); + assertEquals("Number of steps should be 0", expected1, actual1); + assertEquals("Number of steps should be 8", expected1, actual1); + } + +} \ No newline at end of file diff --git a/src/test/java/kim/christopher/stack/StringStackTest.java b/src/test/java/kim/christopher/stack/StringStackTest.java new file mode 100644 index 0000000..67f8606 --- /dev/null +++ b/src/test/java/kim/christopher/stack/StringStackTest.java @@ -0,0 +1,61 @@ +package kim.christopher.stack; + + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +public class StringStackTest { + + StringStack myStack; + + @Before + public void initialize() { + myStack = new StringStack(); + } + + @Test + public void pushTest(){ + //Given + String expected = "Chris"; + + //When + myStack.push("Anthony"); + myStack.push("Chris"); + String actual = myStack.peek(); + + //Then + assertEquals("Chris should have been pushed", expected, actual); + } + + @Test + public void popTest(){ + //Given + String expected = "Stephen"; + + //When + myStack.push("Ziggy"); + myStack.push("Stephen"); + String actual = myStack.pop(); + + //Then + assertEquals("Stephen should have been popped", expected, actual); + } + + @Test + public void peekTest(){ + //Given + String expected = "Dennis"; + String expected1 = "Paco"; + + //When + myStack.push("Dennis"); + String actual = myStack.peek(); + myStack.push("Paco"); + String actual1 = myStack.peek(); + + //Then + assertEquals("Dennis should be on top", expected, actual); + assertEquals("Dennis should be on top", expected1, actual1); + } +}