Last active
December 28, 2015 15:19
-
-
Save Veedrac/7520740 to your computer and use it in GitHub Desktop.
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
// Let "int[] test" make a reference R. | |
// If R is passed by reference the function | |
// will mutate "test". If passed by value the inner | |
// modified reference will be changed but not the | |
// outer one. | |
public class Eight { | |
public static void mutate_reference(int[] foo) { | |
foo = null; | |
System.out.println(foo); | |
} | |
public static void main(String[] args) { | |
int[] test = {}; | |
System.out.println(test); | |
mutate_reference(test); | |
System.out.println(test); | |
} | |
} |
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
public class Eighteen { | |
public static void main(String[] args) { | |
OOPSortedLinkedList ll = new OOPSortedLinkedList(new int[] {0, 4, 2, 1}); | |
System.out.println(ll.toString()); | |
// Addition to head | |
ll.addHead(3); | |
System.out.println(ll.toString()); | |
// Removal of head | |
ll.dropHead(); | |
System.out.println(ll.toString()); | |
// Query of head | |
System.out.println(ll.getHead()); | |
// Get nth element | |
System.out.println(ll.nth(2)); | |
// Get length | |
System.out.println(ll.length()); | |
} | |
} | |
class OOPSortedLinkedList extends OOPLinkedList { | |
public String getName() { return "Sorted Linked List"; } | |
// This seems like it shouldn't be needed. | |
public OOPSortedLinkedList(int[] elements) { | |
super(elements); | |
} | |
public void addHead(int element) { | |
this.ll = new OOPLinkedListElement(element, this.ll); | |
OOPLinkedListElement active = this.ll; | |
while (!active.tail.end && (active.head > active.tail.head)) { | |
int tmp = active.head; | |
active.head = active.tail.head; | |
active.tail.head = tmp; | |
active = active.tail; | |
} | |
} | |
} |
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
public class Eleven { | |
public static void main(String[] args) { | |
System.out.println("Mutables:"); | |
VectorMutable pos_mutable = new VectorMutable(0, 0); | |
System.out.println(pos_mutable.toString()); | |
pos_mutable.add(new VectorMutable(10, 5)); | |
pos_mutable.add(new VectorMutable(3, 6)); | |
System.out.println(pos_mutable.toString()); | |
System.out.println(pos_mutable.dot(new VectorMutable(3, 2))); | |
pos_mutable.normailize(); | |
System.out.println(pos_mutable.toString()); | |
System.out.println("Immutables:"); | |
Vector pos = new Vector(0, 0); | |
Vector cache = pos; | |
System.out.println(pos.toString()); | |
pos = pos.add(new Vector(10, 5)); | |
pos = pos.add(new Vector(3, 6)); | |
System.out.println(pos.toString()); | |
System.out.println(pos.dot(new Vector(3, 2))); | |
pos = pos.normailize(); | |
System.out.println(pos.toString()); | |
System.out.println(cache.toString()); | |
} | |
} | |
class VectorMutable { | |
public float i; | |
public float j; | |
public VectorMutable(float i, float j) { | |
this.i = i; | |
this.j = j; | |
} | |
public void add(VectorMutable that) { | |
this.i += that.i; | |
this.j += that.j; | |
} | |
public float dot(VectorMutable that) { | |
return this.i*that.i + this.j*that.j; | |
} | |
public double magnitude() { | |
return Math.sqrt(this.i*this.i + this.j*this.j); | |
} | |
public void normailize() { | |
float magnitude = (float)this.magnitude(); | |
this.i /= magnitude; | |
this.j /= magnitude; | |
} | |
public String toString() { | |
return String.format("[ %s %s ]", this.i, this.j); | |
} | |
} | |
// These are the changes needed to become immutable | |
class Vector { | |
public float i; | |
public float j; | |
public Vector(float i, float j) { | |
this.i = i; | |
this.j = j; | |
} | |
public Vector add(Vector that) { | |
return new Vector(this.i+that.i, this.j+that.j); | |
} | |
public float dot(Vector that) { | |
return this.i*that.i + this.j*that.j; | |
} | |
public double magnitude() { | |
return Math.sqrt(this.i*this.i + this.j*this.j); | |
} | |
public Vector normailize() { | |
float magnitude = (float)this.magnitude(); | |
return new Vector(this.i/magnitude, this.j/magnitude); | |
} | |
public String toString() { | |
return String.format("[ %s %s ]", this.i, this.j); | |
} | |
} |
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
public class Fifteen extends fifteen.FifteenPackage { | |
public static void main(String[] args) { | |
System.out.println(fifteen.FifteenPackage.value); | |
} | |
} |
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
public class Four { | |
public static void recurse() { recurse(); } | |
public static void main(String[] args) { | |
recurse(); | |
} | |
} |
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
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class Nineteen { | |
public static void main(String[] args) { | |
OOPLazySortedLinkedList ll = new OOPLazySortedLinkedList(new int[] {1, 2, 3}); | |
System.out.println(ll.toString()); | |
// Addition to head | |
ll.addHead(0); | |
System.out.println(ll.toString()); | |
// Removal of head | |
ll.dropHead(); | |
System.out.println(ll.toString()); | |
// Query of head | |
System.out.println(ll.getHead()); | |
// Get nth element | |
System.out.println(ll.nth(2)); | |
// Get length | |
System.out.println(ll.length()); | |
} | |
} | |
// I think you might be wanting a heap, dude. | |
// All this sorting isn't healthy for you. | |
class OOPLazySortedLinkedList extends OOPLinkedList { | |
public String getName() { return "Lazy Sorted Linked List"; } | |
// Again, I have to redefine the initialiser. | |
// What is this and why is it so? | |
public OOPLazySortedLinkedList(int[] elements) { super(elements); } | |
// Because the data will normally be very close to | |
// sorted, you'd want to use an algorithm with O(n) | |
// best case. However, this basically means that I | |
// want to use a simple Timsort which is too much to do | |
// at 1:24 in the morning. | |
// | |
// Thankfully Timsort simplifies to insertion sort | |
// for arrays smaller than 64 elements so I'll | |
// "prototype" it with that. 'Cause it's not like I | |
// know enough Java to write good code in it yet | |
// anyway ;). | |
public void sort() { | |
if (this.ll.end) return; | |
// Reverse for efficiency | |
OOPLinkedList reversed = new OOPLinkedList(new int[0]); | |
for (int element : this) { reversed.addHead(element); } | |
// Insertion sort | |
OOPSortedLinkedList sorted = new OOPSortedLinkedList(new int[0]); | |
for (int element : reversed) { sorted.addHead(element); } | |
this.ll = sorted.ll; | |
} | |
public String toString() { | |
this.sort(); | |
return super.toString(); | |
} | |
public int getHead() { | |
this.sort(); | |
return super.getHead(); | |
}; | |
public void dropHead() { | |
this.sort(); | |
super.dropHead(); | |
} | |
// I'd do this but it causes an infinite loop with | |
// the "for (in element : this) { ... }" block. | |
// I would then try "for (in element : super) { ... }" | |
// and variants but none of them work. What's the solution? | |
// public Iterator<Integer> iterator() { | |
// this.sort(); | |
// return super.iterator(); | |
// } | |
} |
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
public class Seven { | |
// I don't know what to do here. | |
// This is the only things I came up with, other | |
// than using a wrapper class. This is, however, | |
// completely stupid and should never be done so | |
// I'd hope this isn't the right answer. | |
public static void triple(int[] an_x) { | |
an_x[0] *= 3; | |
} | |
public static void main(String[] args) { | |
int[] x = {1}; | |
triple(x); | |
System.out.println(x[0]); | |
} | |
} |
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
public class Thirteen { | |
public static void main(String[] args) { | |
BinaryTree<Integer> bt = ( | |
new BinaryTree<Integer>(1, | |
new BinaryTree<Integer>(2, | |
new BinaryTree<Integer>(), | |
new BinaryTree<Integer>(3, | |
new BinaryTree<Integer>(), | |
new BinaryTree<Integer>() | |
) | |
), | |
new BinaryTree<Integer>(4, | |
new BinaryTree<Integer>(), | |
new BinaryTree<Integer>() | |
) | |
) | |
); | |
System.out.println(bt.toString()); | |
FunctionalArray<Integer> fa = new FunctionalArray<Integer>( | |
new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9} | |
); | |
System.out.println(fa.toString()); | |
System.out.println(fa.get(5)); | |
} | |
} | |
class BinaryTree<T> { | |
public T head; | |
public BinaryTree<T> left; | |
public BinaryTree<T> right; | |
public boolean end; | |
public BinaryTree() { | |
this.end = true; | |
} | |
public BinaryTree(T head, BinaryTree<T> left, BinaryTree<T> right) { | |
this.head = head; | |
this.left = left; | |
this.right = right; | |
this.end = false; | |
} | |
public String toString() { | |
StringBuilder builder = new StringBuilder("BinaryTree "); | |
this.buildString(builder); | |
return builder.toString(); | |
} | |
protected void buildString(StringBuilder builder) { | |
if (this.end) { | |
builder.append("{}"); | |
} else { | |
builder.append("{"); | |
builder.append(this.head); | |
if (!this.left.end) { | |
builder.append(" "); | |
this.left.buildString(builder); | |
} | |
if (!this.right.end) { | |
builder.append(" "); | |
this.right.buildString(builder); | |
} | |
builder.append("}"); | |
} | |
} | |
} | |
class FunctionalArray<T> { | |
BinaryTree<T> tree; | |
public FunctionalArray() { | |
this.tree = new BinaryTree<T>(); | |
} | |
public FunctionalArray(T[] items) { | |
this.tree = new BinaryTree<T>(); | |
int i = 1; | |
for (T item : items) { | |
this.set(i, item); | |
i ++; | |
} | |
} | |
public void set(int position, T item) { | |
BinaryTree<T> active = this.tree; | |
for (int i=position; i>1; i/=2) { | |
assert !active.end; | |
active = (i%2 == 0) ? active.left : active.right; | |
} | |
active.head = item; | |
if (active.end) { | |
active.left = new BinaryTree<T>(); | |
active.right = new BinaryTree<T>(); | |
active.end = false; | |
} | |
} | |
public T get(int position) { | |
BinaryTree<T> active = this.tree; | |
for (int i=position; i>1; i/=2) { | |
assert !active.end; | |
active = (i%2 == 0) ? active.left : active.right; | |
} | |
return active.head; | |
} | |
public String toString() { | |
StringBuilder builder = new StringBuilder("FunctionalArray "); | |
tree.buildString(builder); | |
return builder.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
public class Thirty { | |
public static void main(String[] args) { | |
// Catch is required | |
try { throw new CheckedException(); } | |
catch (CheckedException e) {} | |
// Catch is not required | |
throw new UncheckedException(); | |
} | |
} | |
class CheckedException extends Exception {} | |
class UncheckedException extends RuntimeException {} |
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
import java.util.Arrays; | |
public class Thirtyfive { | |
public static void main(String[] args) {} | |
} | |
/*public*/ class MyClass implements Cloneable { | |
private String mName; | |
private int[] mData; | |
// Copy constructor | |
public MyClass(MyClass toCopy) { | |
this.mName = toCopy.mName; | |
this.mData = toCopy.mData.clone(); | |
} | |
public MyClass clone() throws CloneNotSupportedException { | |
MyClass cloned = (MyClass)super.clone(); | |
cloned.mData = Arrays.copyOf(this.mData, this.mData.length); | |
return cloned; | |
} | |
} | |
// Copy constructors can be disliked because they always | |
// produce the given type, even if the input is a subclass. | |
// This is not the case with clone. | |
// | |
// The advantages of a copy constructor are mainly in its | |
// simplicity; it's really easy. "clone" seems to be violently | |
// hated by the Java community, and if the Java community | |
// thinks bad of it, it must be atrocious! |
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
// My guess is that it works exactly like Python: | |
// Finally always trumps and allows replacing | |
// returns, but will fall back to previous return | |
// if no new one is found. | |
public class Thirtyfour { | |
public static void main(String[] args) { | |
System.out.println(exampleOne()); | |
System.out.println(exampleTwo()); | |
System.out.println(exampleThree()); | |
} | |
public static String exampleOne() { | |
try { return "try"; } finally {} | |
} | |
public static String exampleTwo() { | |
try { return "try"; } finally { return "finally"; } | |
} | |
public static String exampleThree() { | |
try { return "try"; } finally { throw new RuntimeException(); } | |
} | |
} | |
// Yay, I'm correct! |
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
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class Thirtynine { | |
public static void main(String[] args) { | |
OOPLinkedList<Double> ll = new OOPLinkedList<Double>(new Double[] {1.0, 2.0, 3.0}); | |
System.out.println(ll.toString()); | |
// Addition to head | |
ll.addHead(0.0); | |
System.out.println(ll.toString()); | |
// Removal of head | |
ll.dropHead(); | |
System.out.println(ll.toString()); | |
// Query of head | |
System.out.println(ll.getHead()); | |
// Get nth element | |
System.out.println(ll.nth(2)); | |
// Get length | |
System.out.println(ll.length()); | |
} | |
} | |
interface OOPList<T> extends Iterable<T> { | |
abstract T nth(int n); | |
abstract int length(); | |
abstract T getHead(); | |
abstract void addHead(T element); | |
abstract void dropHead(); | |
} | |
class OOPLinkedList<T> implements OOPList<T>, Iterable<T> { | |
protected OOPLinkedListElement<T> ll; | |
public String getName() { return "Linked List"; } | |
public String getSeperator() { return " → "; } | |
public OOPLinkedList(T[] elements) { | |
this.ll = new OOPLinkedListElement<T>(); | |
for (int i=elements.length-1; i>=0; i--) this.addHead(elements[i]); | |
} | |
public T nth(int n) { | |
OOPLinkedListElement<T> active = this.ll; | |
for (; n>0; n--) active = active.tail; | |
return active.head; | |
} | |
public int length() { | |
OOPLinkedListElement<T> active = this.ll; | |
int n = 0; | |
while (!active.end) { | |
active = active.tail; | |
n++; | |
} | |
return n; | |
} | |
public String toString() { | |
StringBuilder builder = new StringBuilder(this.getName()); | |
builder.append(" {"); | |
boolean first = true; | |
for (T element : this) { | |
if (!first) { | |
builder.append(this.getSeperator()); | |
} else { | |
first = false; | |
} | |
builder.append(element); | |
} | |
builder.append("}"); | |
return builder.toString(); | |
} | |
public T getHead() { return this.ll.head; } | |
public void addHead(T element) { | |
this.ll = new OOPLinkedListElement<T>(element, this.ll); | |
} | |
public void dropHead() { this.ll = this.ll.tail; } | |
public boolean isEmpty() { return this.ll.end; } | |
public Iterator<T> iterator() { | |
return new OOPLinkedListIterator<T>(this.ll); | |
} | |
} | |
class OOPLinkedListIterator<T> implements Iterator<T> { | |
protected OOPLinkedListElement<T> cursor; | |
public OOPLinkedListIterator(OOPLinkedListElement<T> cursor) { | |
this.cursor = cursor; | |
} | |
public boolean hasNext() { return !this.cursor.end; } | |
public T next() { | |
if (!this.hasNext()) throw new NoSuchElementException(); | |
T res = this.cursor.head; | |
this.cursor = cursor.tail; | |
return res; | |
} | |
public void remove() { throw new UnsupportedOperationException(); } | |
} | |
class OOPLinkedListElement<T> { | |
T head; | |
OOPLinkedListElement<T> tail; | |
boolean end; | |
public OOPLinkedListElement() { | |
this.end = true; | |
} | |
public OOPLinkedListElement(T head, OOPLinkedListElement<T> tail) { | |
this.end = false; | |
this.head = head; | |
this.tail = tail; | |
} | |
} |
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
public class Thirtyone { | |
public static void main(String[] args) { | |
for (int i=0; i<1000000; i++) { | |
squareroot(Math.random() * Double.MAX_VALUE); | |
if (i%50000 == 0) { | |
System.out.print(String.format("\r%s%%", i/10000)); | |
} | |
} | |
System.out.println("\nDone, no Floating Point Error hangs detected."); | |
System.out.println(squareroot(-2.0)); | |
} | |
public static double squareroot(double value) { | |
if (value < 0) { | |
throw new ValueOutOfBoundsError(); | |
} | |
// f(x) = x² - value; f(x) = 0 when x² = value. | |
// Initial estimate | |
double prev_x = 0; | |
double x = 1; | |
// Using fractional error | |
while (Math.abs(x*x / value - 1) > 0.000000000001) { | |
prev_x = x; | |
x -= (x - value / x) / (2); | |
} | |
return x; | |
} | |
} | |
class ValueOutOfBoundsError extends RuntimeException {} |
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
// The superclass would not run its copy and thus its information | |
// might not get copied. Furthermore this assumed more about the | |
// initialisation for SomeClass than before. | |
public class Thirtysix { | |
public static void main(String[] args) throws CloneNotSupportedException { | |
SomeClass sc = new SomeClass(); | |
sc.notCopied = "Not Copied"; | |
// Prints "null". | |
System.out.println(sc.clone().notCopied); | |
} | |
} | |
class SomeClass extends SomeOtherClass implements Cloneable { | |
private int[] mData; | |
public SomeClass() { this.mData = new int[0]; } | |
public SomeClass clone() throws CloneNotSupportedException { | |
SomeClass sc = new SomeClass(); | |
sc.mData = this.mData.clone(); | |
return sc; | |
} | |
} | |
class SomeOtherClass implements Cloneable { | |
public String notCopied; | |
public Object clone() throws CloneNotSupportedException { | |
return super.clone(); | |
} | |
} |
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
import java.util.Arrays; | |
public class Three { | |
public static float[][] square(int n) { | |
float[][] ret = new float[n][n]; | |
for (int i=0; i<n; i++) ret[i][i] = 1; | |
return ret; | |
} | |
public static void transpose_ip(float[][] arr) { | |
float tmp; | |
if (arr.length != 0) assert arr[0].length == arr.length; | |
for (int x=0; x<arr.length; x++) { | |
for (int y=x+1; y<arr.length; y++) { | |
tmp = arr[x][y]; | |
arr[x][y] = arr[y][x]; | |
arr[y][x] = tmp; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println("5✕5 identity"); | |
System.out.println(Arrays.deepToString(square(5))); | |
System.out.println(""); | |
float[][] mat = {{1,2,3},{4,5,6},{7,8,9}}; | |
System.out.println("Matrix and transposition"); | |
System.out.println(Arrays.deepToString(mat)); | |
transpose_ip(mat); | |
System.out.println(Arrays.deepToString(mat)); | |
} | |
} |
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
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class Twelve { | |
public static void main(String[] args) { | |
OOPLinkedList ll = new OOPLinkedList(new int[] {1, 2, 3}); | |
System.out.println(ll.toString()); | |
// Addition to head | |
ll.addHead(0); | |
System.out.println(ll.toString()); | |
// Removal of head | |
ll.dropHead(); | |
System.out.println(ll.toString()); | |
// Query of head | |
System.out.println(ll.getHead()); | |
// Get nth element | |
System.out.println(ll.nth(2)); | |
// Get length | |
System.out.println(ll.length()); | |
} | |
} | |
// OOPList from later question | |
class OOPLinkedList implements OOPList, Iterable<Integer> { | |
protected OOPLinkedListElement ll; | |
public String getName() { return "Linked List"; } | |
public String getSeperator() { return " → "; } | |
public OOPLinkedList(int[] elements) { | |
this.ll = new OOPLinkedListElement(); | |
for (int i=elements.length-1; i>=0; i--) this.addHead(elements[i]); | |
} | |
public int nth(int n) { | |
OOPLinkedListElement active = this.ll; | |
for (; n>0; n--) active = active.tail; | |
return active.head; | |
} | |
public int length() { | |
OOPLinkedListElement active = this.ll; | |
int n = 0; | |
while (!active.end) { | |
active = active.tail; | |
n++; | |
} | |
return n; | |
} | |
public String toString() { | |
StringBuilder builder = new StringBuilder(this.getName()); | |
builder.append(" {"); | |
boolean first = true; | |
for (int element : this) { | |
if (!first) { | |
builder.append(this.getSeperator()); | |
} else { | |
first = false; | |
} | |
builder.append(element); | |
} | |
builder.append("}"); | |
return builder.toString(); | |
} | |
public int getHead() { return this.ll.head; } | |
public void addHead(int element) { | |
this.ll = new OOPLinkedListElement(element, this.ll); | |
} | |
public void dropHead() { this.ll = this.ll.tail; } | |
public boolean isEmpty() { return this.ll.end; } | |
public Iterator<Integer> iterator() { | |
return new OOPLinkedListIterator(this.ll); | |
} | |
} | |
class OOPLinkedListIterator implements Iterator<Integer> { | |
protected OOPLinkedListElement cursor; | |
public OOPLinkedListIterator(OOPLinkedListElement cursor) { | |
this.cursor = cursor; | |
} | |
public boolean hasNext() { return !this.cursor.end; } | |
public Integer next() { | |
if (!this.hasNext()) throw new NoSuchElementException(); | |
int res = this.cursor.head; | |
this.cursor = cursor.tail; | |
return res; | |
} | |
public void remove() { throw new UnsupportedOperationException(); } | |
} | |
class OOPLinkedListElement { | |
int head; | |
OOPLinkedListElement tail; | |
boolean end; | |
public OOPLinkedListElement() { | |
this.end = true; | |
} | |
public OOPLinkedListElement(int head, OOPLinkedListElement tail) { | |
this.end = false; | |
this.head = head; | |
this.tail = tail; | |
} | |
} |
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
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class Twentyfive { | |
public static void main(String[] args) { | |
OOPListQueue lq = new OOPListQueue(new int[] {1, 2, 3, 4, 5}); | |
System.out.println(lq.toString()); | |
// Addition to head | |
lq.add(0); | |
System.out.println(lq.toString()); | |
// Removal of tail | |
lq.drop(); | |
System.out.println(lq.toString()); | |
// Query of tail | |
System.out.println(lq.getNext()); | |
// Get length | |
System.out.println(lq.length()); | |
System.out.println(); | |
OOPArrayQueue aq = new OOPArrayQueue(new int[] {1, 2, 3, 4, 5}); | |
System.out.println(aq.toString()); | |
// Addition to head | |
aq.add(0); | |
System.out.println(aq.toString()); | |
// Removal of tail | |
aq.drop(); | |
System.out.println(aq.toString()); | |
// Query of tail | |
System.out.println(aq.getNext()); | |
// Get length | |
System.out.println(aq.length()); | |
} | |
} | |
interface Queue extends Iterable<Integer> { | |
abstract int length(); | |
abstract void add(int element); | |
abstract int getNext(); | |
abstract void drop(); | |
} | |
class OOPListQueue implements Queue { | |
protected OOPLinkedList in; | |
protected OOPLinkedList out; | |
public String getName() { return "Linked List Queue"; } | |
public String getSeperator() { return " ← "; } | |
public OOPListQueue(int[] elements) { | |
this.in = new OOPLinkedList(new int[0]); | |
this.out = new OOPLinkedList(new int[0]); | |
for (int element : elements) { this.add(element); } | |
} | |
public int length() { | |
int n = 0; | |
for (int element : this) { n++; } | |
return n; | |
} | |
public void add(int element) { this.in.addHead(element); } | |
public int getNext() { | |
if (this.out.isEmpty()) { this.normalize(); } | |
return this.out.getHead(); | |
} | |
public void drop() { | |
if (this.out.isEmpty()) { this.normalize(); } | |
this.out.dropHead(); | |
} | |
public void normalize() { | |
// Cache the out into a new linked list | |
OOPLinkedList tmp = new OOPLinkedList(new int[0]); | |
for (int element : this.out) { tmp.addHead(element); } | |
this.out = new OOPLinkedList(new int[0]); | |
// Put the in out | |
for (int element : this.in) { this.out.addHead(element); } | |
this.in = new OOPLinkedList(new int[0]); | |
// Put the out cache out | |
for (int element : tmp) { this.out.addHead(element); } | |
} | |
public Iterator<Integer> iterator() { | |
this.normalize(); | |
return this.out.iterator(); | |
} | |
public String toString() { | |
StringBuilder builder = new StringBuilder(this.getName()); | |
builder.append(" {"); | |
boolean first = true; | |
for (int element : this) { | |
if (!first) { | |
builder.append(this.getSeperator()); | |
} else { | |
first = false; | |
} | |
builder.append(element); | |
} | |
builder.append("}"); | |
return builder.toString(); | |
} | |
} | |
class OOPArrayQueue extends OOPArrayList implements Queue { | |
public String getName() { return "Array Queue"; } | |
public String getSeperator() { return " ← "; } | |
public OOPArrayQueue(int[] elements) { super(elements); } | |
public void add(int element) { this.addRight(element); } | |
public int getNext() { return this.nth(0); } | |
public void drop() { this.dropLeft(); } | |
} |
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
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class Twentyfour { | |
public static void main(String[] args) { | |
OOPArrayList ll = new OOPArrayList(new int[] {1, 2, 3, 4, 5}); | |
System.out.println(ll.toString()); | |
// Addition to head | |
ll.addHead(0); | |
System.out.println(ll.toString()); | |
// Removal of head | |
ll.dropHead(); | |
System.out.println(ll.toString()); | |
// Query of head | |
System.out.println(ll.getHead()); | |
// Get nth element | |
System.out.println(ll.nth(2)); | |
// Get length | |
System.out.println(ll.length()); | |
} | |
} | |
interface OOPList extends Iterable<Integer> { | |
abstract int nth(int n); | |
abstract int length(); | |
abstract int getHead(); | |
abstract void addHead(int element); | |
abstract void dropHead(); | |
} | |
class OOPArrayList implements OOPList { | |
protected int start; | |
protected int end; | |
protected int[] elements; | |
public String getName() { return "Sorted Linked List"; } | |
public String getSeperator() { return ", "; } | |
public OOPArrayList(int[] elements) { | |
this.start = 1; | |
this.end = 1; | |
this.elements = new int[2]; | |
for (int element : elements) { this.addRight(element); } | |
} | |
public int nth(int n) { | |
if (n >= this.length()) { throw new IndexOutOfBoundsException(); } | |
return this.elements[this.start + n]; | |
} | |
public int length() { return this.end-this.start; } | |
public int getHead() { return this.nth(0); } | |
public void addHead(int element) { this.addLeft(element); } | |
public void dropHead() { this.dropLeft(); } | |
public Iterator<Integer> iterator() { | |
// I would use this.elements.iterator(), but supposedly | |
// Java's scared of being helpful. | |
return new OOPArrayListIterator(this.elements, this.start, this.end); | |
} | |
//public void print(String s) { | |
// System.out.println(String.format( | |
// "[%s] (%s : %s) %s", | |
// s, this.start, this.end, Arrays.toString(this.elements) | |
// )); | |
//} | |
public void resize() { | |
int length = this.length(); | |
int[] new_elements = new int[length * 2 + 2]; | |
int new_start = (int)Math.floor(length / 2) + 1; | |
int new_end = new_start + length; | |
for (int i=0; i<length; i++) { | |
new_elements[new_start+i] = this.elements[this.start+i]; | |
} | |
this.elements = new_elements; | |
this.start = new_start; | |
this.end = new_end; | |
} | |
public void addLeft(int element) { | |
// Resize if it's pointing at the first item 'cause we'd | |
// need space to add the next item | |
if (this.start == 0) { this.resize(); } | |
this.start -= 1; | |
this.elements[this.start] = element; | |
} | |
public void addRight(int element) { | |
// Resize if it's pointing at the last item 'cause we'd | |
// need space to add the next item. | |
if (this.end == this.elements.length) { this.resize(); } | |
this.end += 1; | |
this.elements[this.end-1] = element; | |
} | |
public void dropLeft() { | |
if (this.length() == 0) { throw new IndexOutOfBoundsException(); } | |
this.start += 1; | |
// Guess when it's worth resizing. Don't do it often. | |
if (this.length()*4 + 2 <= this.elements.length) { this.resize(); } | |
} | |
public void dropRight() { | |
if (this.length() == 0) { throw new IndexOutOfBoundsException(); } | |
this.end -= 1; | |
// Guess when it's worth resizing. Don't do it often. | |
if (this.length()*4 + 2 <= this.elements.length) { this.resize(); } | |
} | |
public String toString() { | |
StringBuilder builder = new StringBuilder(this.getName()); | |
builder.append(" {"); | |
boolean first = true; | |
for (int element : this) { | |
if (!first) { | |
builder.append(this.getSeperator()); | |
} else { | |
first = false; | |
} | |
builder.append(element); | |
} | |
builder.append("}"); | |
return builder.toString(); | |
} | |
} | |
class OOPArrayListIterator implements Iterator<Integer> { | |
protected int start; | |
protected int end; | |
protected int[] arr; | |
public OOPArrayListIterator(int[] arr, int start, int end) { | |
this.start = start; | |
this.end = end; | |
this.arr = arr; | |
} | |
public boolean hasNext() { return this.start < this.end; } | |
public Integer next() { | |
if (!this.hasNext()) throw new NoSuchElementException(); | |
this.start += 1; | |
return this.arr[this.start-1]; | |
} | |
public void remove() { throw new UnsupportedOperationException(); } | |
} |
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
public class Twentyseven { | |
public static void main(String[] args) {} | |
} | |
class A { | |
public A(int x) {} | |
} | |
// Seeing as the question was so vague, | |
// I'm going to use "it compiles" as evidence | |
// I am right; there is no defined behaviour | |
// so that should be sufficient. | |
class B extends A { | |
public B() { super(1); } | |
} | |
class C extends B {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment