Skip to content

Bitset Lab complete #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea/*
*.iml
target/*
*.iws
.DS_Store
log/*
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.

37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>kim.christopher</groupId>
<artifactId>BitSetMicroLab</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>BitSetMicroLab</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
104 changes: 104 additions & 0 deletions src/main/java/kim/christopher/bitwise/LightSwitcher.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 24 additions & 0 deletions src/main/java/kim/christopher/stack/StringStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kim.christopher.stack;

import java.util.LinkedList;

public class StringStack {

LinkedList<String> 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();
}
}
Loading