Created
July 7, 2011 11:36
-
-
Save salzig/1069332 to your computer and use it in GitHub Desktop.
Exampleexam 2011
This file contains hidden or 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
package klausur; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
public class Pruefung { | |
public static List<List<String>> transposeList(List<List<String>> list) { | |
if (list == null) throw new RuntimeException("Argument Error"); | |
int size = list.get(0).size(); | |
for (List<String> e : list) { | |
if (e.size() != size) { | |
throw new RuntimeException("Argument Error"); | |
} | |
} | |
List<List<String>> result = new ArrayList<List<String>>(); | |
for (int x = 0; x < list.size(); x++) { | |
for (int y = 0; y < list.get(x).size(); y++) { | |
try { | |
result.get(y); | |
} | |
catch (Exception e) { | |
result.add(y, new ArrayList<String>()); | |
} | |
result.get(y).add(x,list.get(x).get(y)); | |
} | |
} | |
return result; | |
} | |
public static Set<String> symmDiff(Set<String> set1, Set<String> set2) { | |
if (set1 == null || set2 == null) throw new RuntimeException("Argument Error"); | |
Set<String> setA = new HashSet<String>(set1); | |
setA.removeAll(set2); | |
Set<String> setB = new HashSet<String>(set2); | |
setB.removeAll(set1); | |
Set<String> result = new HashSet<String>(); | |
result.addAll(setA); | |
result.addAll(setB); | |
return result; | |
} | |
public static Set<?> deepCopyTree(Set<?> set) { | |
if (set == null) throw new RuntimeException("Argument Error"); | |
Set<Object> result = new HashSet<Object>(); | |
for (Object o : set) { | |
if (o instanceof Set) { | |
result.add(deepCopyTree((Set<?>)o)); | |
} | |
else { | |
result.add(o); | |
} | |
} | |
return result; | |
} | |
public static Map<String, List<String>> setToMultiMap(Set<List<String>> set) { | |
if (set == null) throw new RuntimeException("Argument Error"); | |
for (List<String> e : set) { | |
if (e.size() != 2) { | |
throw new RuntimeException("Argument Error"); | |
} | |
} | |
Map<String, List<String>> result = new HashMap<String, List<String>>(); | |
for (List<String> e : set) { | |
if (!result.containsKey(e.get(0))) { | |
result.put(e.get(0), new ArrayList<String>()); | |
} | |
result.get(e.get(0)).add(e.get(1)); | |
} | |
return result; | |
} | |
} |
This file contains hidden or 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
package klausur; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class PruefungTest { | |
@Test | |
public void testTransposeList() { | |
List<List<String>> list = new ArrayList<List<String>>(); | |
list.add(new ArrayList<String>(Arrays.asList("a","b","c"))); | |
list.add(new ArrayList<String>(Arrays.asList("x","y","z"))); | |
List<List<String>> expected = new ArrayList<List<String>>(); | |
expected.add(new ArrayList<String>(Arrays.asList("a","x"))); | |
expected.add(new ArrayList<String>(Arrays.asList("b","y"))); | |
expected.add(new ArrayList<String>(Arrays.asList("c","z"))); | |
assertEquals(expected, Pruefung.transposeList(list)); | |
} | |
@Test | |
public void testSymmDiff() { | |
Set<String> set1 = new HashSet<String>(Arrays.asList("a","b")); | |
Set<String> set2 = new HashSet<String>(Arrays.asList("b","c")); | |
Set<String> expected = new HashSet<String>(Arrays.asList("a","c")); | |
assertEquals(expected, Pruefung.symmDiff(set1, set2)); | |
} | |
@Test | |
public void testDeepCopyTree() { | |
Set<Object> set = new HashSet<Object>(); | |
Set<Object> subset = new HashSet<Object>(); | |
set.add("a"); | |
set.add(subset); | |
subset.add("d"); | |
subset.add(new HashSet<Object>(Arrays.asList("b","c"))); | |
Set<Object> expected = set; | |
Set<?> actual = Pruefung.deepCopyTree(set); | |
assertFalse(expected == actual); | |
assertTrue(actual.equals(expected)); | |
// assertEquals behauptet die Sets wären ungleich?!? | |
//assertEquals(expected, actual); | |
} | |
@Test | |
public void testSetToMultiMap() { | |
Set<List<String>> set = new HashSet<List<String>>(); | |
set.add(new ArrayList<String>(Arrays.asList("Emil","PR1"))); | |
set.add(new ArrayList<String>(Arrays.asList("Otto","PR2"))); | |
set.add(new ArrayList<String>(Arrays.asList("Emil","AF"))); | |
Map<String, List<String>> expected = new HashMap<String, List<String>>(); | |
expected.put("Emil", new ArrayList<String>(Arrays.asList("AF","PR1"))); | |
expected.put("Otto", new ArrayList<String>(Arrays.asList("PR2"))); | |
assertEquals(expected, Pruefung.setToMultiMap(set)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment