Skip to content

Instantly share code, notes, and snippets.

@salzig
Created July 6, 2011 17:43
Show Gist options
  • Save salzig/1067852 to your computer and use it in GitHub Desktop.
Save salzig/1067852 to your computer and use it in GitHub Desktop.
HAW - PRP2 - WS10 - Example Solution
package ws10;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Application {
public static boolean isSorted(List<String> list) {
boolean result = true;
Iterator<String> iter = list.iterator();
if (iter.hasNext()) {
String current = iter.next();
String next = null;
while (iter.hasNext()) {
next = iter.next();
result = current.compareTo(next) < 0 ? result : false;
current = next;
}
}
return result;
}
public static int noOfAllNodes(Set<?> set) {
int result = 1;
for (Object o : set) {
if (o instanceof Set) {
result += noOfAllNodes((Set<?>)o);
}
}
return result;
}
public static Map<String,Integer> addFreqMaps(Map<String,Integer> map1, Map<String,Integer> map2) {
Map<String, Integer> result = new HashMap<String,Integer>(map1);
for (Map.Entry<String, Integer> current : map2.entrySet()) {
if (result.containsKey(current.getKey())) {
result.put(current.getKey(), result.get(current.getKey()) + current.getValue());
}
else {
result.put(current.getKey(), current.getValue());
}
}
return result;
}
public static List<String> interleave(List<String> list1, List<String> list2) {
List<String> result = new ArrayList<String>();
Iterator<String> i1 = list1.iterator();
Iterator<String> i2 = list2.iterator();
while (i1.hasNext() || i2.hasNext()) {
if (i1.hasNext()) {
result.add(i1.next());
}
if (i2.hasNext()) {
result.add(i2.next());
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment