Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
initial commit, swap argument order
Browse files Browse the repository at this point in the history
  • Loading branch information
jpolitz committed Nov 8, 2022
0 parents commit a72acb9
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions ListExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.ArrayList;
import java.util.List;

interface StringChecker { boolean checkString(String s); }

class ListExamples {

// Returns a new list that has all the elements of the input list for which
// the StringChecker returns true, and not the elements that return false, in
// the same order they appeared in the input list;
static List<String> filter(StringChecker sc, List<String> list) {
List<String> result = new ArrayList<>();
for(String s: list) {
if(sc.checkString(s)) {
result.add(s);
}
}
return result;
}


// Takes two sorted list of strings (so "a" appears before "b" and so on),
// and return a new list that has all the strings in both list in sorted order.
static List<String> merge(List<String> list1, List<String> list2) {
List<String> result = new ArrayList<>();
int index1 = 0, index2 = 0;
while(index1 < list1.size() && index2 < list2.size()) {
if(list1.get(index1).compareTo(list2.get(index2)) < 0) {
result.add(list1.get(index1));
index1 += 1;
}
else {
result.add(list2.get(index2));
index2 += 1;
}
}
while(index1 < list1.size()) {
result.add(list1.get(index1));
index1 += 1;
}
while(index2 < list2.size()) {
result.add(list2.get(index2));
index2 += 1;
}
return result;
}


}

0 comments on commit a72acb9

Please sign in to comment.