Skip to content

Instantly share code, notes, and snippets.

@pyropeter
Created February 15, 2013 14:43
Show Gist options
  • Save pyropeter/4960781 to your computer and use it in GitHub Desktop.
Save pyropeter/4960781 to your computer and use it in GitHub Desktop.
Just some strange stuff that apparently works in Java.
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
public class Modul1
{
/**
* Diese Methode erhaelt zwei Integer-Iterator-Listen und muss eine
* weitere Integer-Iterator-Liste zurueckgeben. Die beiden Eingaben
* haben beliebige "Formen", allerdings kann man sich darauf verlassen,
* dass beide Eingaben die gleiche Form haben. Der Rueckgabewert soll
* dieser Form folgen und jeweils die zwei Elemente der Eingaben per
* XOR zu einem Ausgabewert zusammenfassen.
*
* Beispiel:
*
* 10 20 11 17 | 1 2 3 4 = 11 22 8 21
* 3 5 | 18 32 = 17 37
* 1 2 3 | 42 17 7 = 43 19 4
*
*/
public static List<Iterator<Integer>> xorReallyStrangeListThingies(
List<Iterator<Integer>> a, List<Iterator<Integer>> b)
{
class Pair<T>
{
public T a, b;
public Pair(T a, T b)
{
this.a = a;
this.b = b;
}
}
class PairIterator<T> implements Iterator<Pair<T>>
{
private Iterator<T> a, b;
public PairIterator(Iterator<T> a, Iterator<T> b)
{
this.a = a;
this.b = b;
}
public boolean hasNext()
{
return a.hasNext();
}
public void remove()
{
a.remove();
b.remove();
}
public Pair<T> next()
{
return new Pair<T>(a.next(), b.next());
}
}
class PairIterable<T> implements Iterable<Pair<T>>
{
private Iterator<T> a, b;
public PairIterable(Iterator<T> a, Iterator<T> b)
{
this.a = a;
this.b = b;
}
public Iterator<Pair<T>> iterator()
{
return new PairIterator<T>(a, b);
}
}
List<Iterator<Integer>> res = new LinkedList<Iterator<Integer>>();
for (Pair<Iterator<Integer>> row : new PairIterable<Iterator<Integer>>(
a.iterator(), b.iterator())) {
List<Integer> rowres = new LinkedList<Integer>();
for (Pair<Integer> pair : new PairIterable<Integer>(row.a, row.b)) {
rowres.add(pair.a ^ pair.b);
}
res.add(rowres.iterator());
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment