Quick Index
Problem 1 - select


Write a method named "select" that returns a new QueryArray containing all elements in the passed (old) collection where the passed select function outputs true when invoked.

Note the return type: QueryArray.

Let's say we have a collection of five Mathematician objects:

Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel  Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)


And that two of them have a country of Ghana. We could select (keep) these objects like this:

let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let selectFct = (aMathematician) => aMathematician.getCountry().equals("Ghana");
selected = qarray.select(selectFct);
selected.printAll();


Expected results:

Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)


Problem 2 - reject


Write a function named reject that returns a new QueryArray containing all elements in the passed (old) collection. The new collection rejects elements in the old collection using the passed reject function.

The method "reject" is the inverse of the method "select."

Note the return type: QueryArray.

Let's say we have a collection of five Mathematician objects:

Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel  Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)


And that two of them have a country of Ghana. We could reject these objects like this:

let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let rejectFct = (aMathematician) => aMathematician.getCountry().equals("Ghana");
newQarray = qarray.reject(rejectFct);
newQarray.printAll();


Expected results:

Isabel  Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)


Problem 3 - findFirst


Write a method named findFirst that returns the index of the last match, or -1 if no match. by "last match" we mean the match closes to the end of the input array. Hint: start the search at the end and work backwards.

Method header: "findFirst(matchFct)"

The passed matchFct is invoked with input that is an old element (from the passed elements array) and it outputs true if the given element is a match.

Let's say we have a collection of five Mathematician objects:

Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel  Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)


Let's say we want to find the last match for the country of Ghana. We could do it like this:

let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let matchFct = (aMathematician) => aMathematician.getCountry().equals("Ghana");
let matchIndex = qarray.findFirst(matchFct);
println("Match Index: " + matchIndex);


Expected results:

Match Index: 0


Problem 4 - findLast


Write a method named findLast that returns the index of the last match, or -1 if no match. by "last match" we mean the match closes to the end of the input array. Hint: start the search at the end and work backwards.

Method header: "findLast(matchFct)"

The passed matchFct is invoked with input that is an old element (from the passed elements array) and it outputs true if the given element is a match.

Let's say we have a collection of five Mathematician objects:

Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel  Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)


Let's say we want to find the last match for the country of Ghana. We could do it like this:

let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let matchFct = (aMathematician) => aMathematician.getCountry().equals("Ghana");
let matchIndex = qarray.findLast(matchFct);
println("Match Index: " + matchIndex);


Expected results:

Match Index: 1


Problem 5 - subList


Write a method subList(start, stop) that returns a new QueryArray containing the elements of this list between the given index start (inclusive) and the given index stop (exclusive).

Method header: "subList(start, stop)"

The passed parameters are integers.

Let's say we have a collection of five Mathematician objects:

Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel  Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)


Let's say we want to get a sub list from index 2 to 3. We could do it like this:

let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
//Note the stop parameter is exclusive so we use index=4 (so we include index=3)
let subList = qarray.subList(2, 4);
subList.printAll();


Expected results:

Isabel  Hubard (Mexico)
Cecilia Mwathi (Kenya)


Problem 6 - get


Write a method get(index) that returns the element at the passed zero-based index.

Method header: "get(index)"

The passed parameter is an integer.

If param "index" is invalid, throws an exception that says: "(get) index %d is out of bounds". -- where %d is replaced with the invalid index.

Assume, in your pseudocode that you can throw an exception like this:

throw aString;		//replacing aString with any exception error message
 

Let's say we have a collection of five Mathematician objects:

Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel  Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)


Let's say we want to get the element at index=1 (second element). We could do it like this:

let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
println(qarray.get(1));


Expected results:

Francis Allotey (Ghana)


Problem 7 - map


Write a method map that returns a new QueryArray which is the target (this) QueryArray mapped (converted) to new elements using the passed mapping function.

Note the return type: QueryArray.

Let's say we have a collection of five Mathematician objects:

Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel  Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)


Let's say we want to map the objects in country names.

let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let mapFct = (aMathematician) => aMathematician.getCountry();
mapped = qarray.map(mapFct);
mapped.printAll();


Expected results:

Ghana
Ghana
Mexico
Kenya
Kenya
...


Problem 8 - accumulate


The "accumulate" method is a powerful method.

It allows an object user to "run the list" accumulating during the run.

E.g.:


The method returns the type of object being accumulated (e.g., a number/sum, a new list, etc).

Method Header:

	/** Accumulate a value by iterating over the collection
	  * and accumulating the next value using the fct */
	accumulate(accumulatorFct, initialValue)