Skip to content

Instantly share code, notes, and snippets.

@RavuAlHemio
Last active December 30, 2015 19:49
Show Gist options
  • Save RavuAlHemio/7876487 to your computer and use it in GitHub Desktop.
Save RavuAlHemio/7876487 to your computer and use it in GitHub Desktop.
PK Cycle class
public class Cycle
{
private int elem;
private Cycle next;
public Cycle(int value, Cycle cycle) {
elem = value;
if (cycle == null) {
next = this;
} else {
next = cycle.next;
cycle.next = this;
}
}
// Hilfsmethode
public void add(int i) {
Cycle walk = this.next;
while (walk.next != this) {
walk = walk.next;
}
walk.next = new Cycle(i, walk);
}
// Aufgabe 2
@Override
public String toString() {
String ret = "[" + elem;
Cycle walk = this.next;
while (walk != this) {
ret += ", " + walk.elem;
walk = walk.next;
}
return ret + "]";
}
// Aufgabe 3
@Override
public boolean equals(Object o) {
if (o == null)
return false;
else if (!this.getClass().equals(o.getClass()))
return false;
Cycle other = (Cycle)o;
if (this.elem != other.elem) {
return false;
}
Cycle walkMe = this.next;
Cycle walkOther = other.next;
while (walkMe != this) {
if (walkMe.elem != walkOther.elem) {
return false;
}
walkMe = walkMe.next;
walkOther = walkOther.next;
}
// Bin ich auch beim anderen Zyklus wieder am Anfang?
return (walkOther == other);
}
// Aufgabe 4
public int getFirst() {
return this.elem;
}
public int getIndex(int n) {
if (n == 0) {
// das bin ich!
return this.elem;
} else {
return next.getIndex(n - 1);
}
}
public int getLast() {
Cycle walk = this.next;
while (walk.next != this) {
walk = walk.next;
}
return walk.elem;
}
// Aufgabe 5
public int fold() {
int sum = this.elem;
Cycle walk = this.next;
while (walk != this) {
sum += walk.elem;
walk = walk.next;
}
return sum;
}
// Aufgabe 6
public Cycle map() {
Cycle ret = new Cycle(this.elem % 5, null);
Cycle walk = this.next;
while (walk != this) {
ret.add(walk.elem % 5);
walk = walk.next;
}
return ret;
}
// Aufgabe 7
public Cycle reduce() {
if (this.elem < 0) {
// Ich selbst bin negativ; mit dem nächsten Element weitermachen.
// (Vorsicht: Endlosrekursion wenn alle Elemente negativ sind.)
return next.reduce();
}
Cycle ret = new Cycle(this.elem, null);
Cycle walk = this.next;
while (walk != this) {
if (walk.elem >= 0)
ret.add(walk.elem);
walk = walk.next;
}
return ret;
}
// Aufgabe 8
public static void main(String[] args) {
Cycle one = new Cycle(13, null);
one.add(99);
one.add(-12);
one.add(-18);
one.add(2);
Cycle two = new Cycle(13, null);
two.add(99);
two.add(-66);
two.add(-18);
two.add(2);
System.out.println(one.equals(two));
// false
Cycle m1 = one.map();
Cycle m2 = two.map();
System.out.println(m1.equals(m2));
// false
Cycle mr1 = m1.reduce();
Cycle mr2 = m2.reduce();
System.out.println(mr1.equals(mr2));
// true
int mrf1 = mr1.fold();
int mrf2 = mr2.fold();
System.out.println(mrf1 == mrf2);
// true
//zusatztests();
}
private static void zusatztests() {
// ggf. aus der main-Methode aufrufen
Cycle one = new Cycle(1, null);
System.out.println(one);
// [1]
Cycle eqOne = new Cycle(10, null);
eqOne.add(15);
Cycle eqTwo = new Cycle(10, null);
eqTwo.add(15);
System.out.println(eqOne.equals(eqTwo));
// true
Cycle eq3 = new Cycle(10, null);
eq3.add(15);
eq3.add(10);
System.out.println(eqOne.equals(eq3));
// false
System.out.println(eqOne.getFirst());
// 10
System.out.println(eqOne.getIndex(0));
// 10
System.out.println(eqOne.getIndex(1));
// 15
System.out.println(eqOne.getIndex(2));
// 10
System.out.println(eqOne.getIndex(3));
// 15
System.out.println(eqOne.getLast());
// 15
System.out.println(eqOne.fold());
// 25
Cycle testMap = new Cycle(3, null);
testMap.add(14);
System.out.println(testMap.map());
// [3, 4]
System.out.println(testMap);
// [3, 14]
System.out.println(eqOne.reduce());
// [10, 15]
System.out.println(eqOne);
// [10, 15]
Cycle negatives = new Cycle(-5, null);
negatives.add(-10);
negatives.add(13);
negatives.add(-18);
System.out.println(negatives.reduce());
// [13]
System.out.println(negatives);
// [-5, -10, 13, -18]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment