Index
Overview


This section contains challenge problems

For each listed method, we are to write an algorithm to solve the problem given statement using this pseudocode syntax.

The trick is to try to solve each example before looking at the solution.

You will use this a lot: -- MyArray Method Summary...

Challenge Problems Format


Each problem write-up on this page has three parts:


For each problem write-up below, write an algorithm using the provided description and expected output.

The problems follow. Note that the "println" function prints to the output or console.

reversed()


Problem Statement
Return a new MyArray object
that contains our elements reversed.
Expected Output (Running Algorithm)
var elements, a
elements = [10, 20, 30]
a = MyArray.from(elements);
println(a)
println('reversed(): ' + a.reversed());
// elements: 10,20,30
// reversed(): elements: 30,20,10


map(fct)


Problem Statement
Return a new MyArray object where elements are
"mapped" (converted) to new elements using
argument "fct".

More examples here...
Expected Output (Running Algorithm)
var elements, a, fct
elements = [10, 20, 30]
a = MyArray.from(elements);
fct = (elem) => 3 * elem
println(a)
println('map(fct): ' + a.map(fct))
// elements: 10,20,30
// map(fct): elements: 30,60,90


includes(anElem)


Problem Statement
Return true if "anElem" is included among our elements otherwise return false
Expected Output (Running Algorithm)
var elements, a
elements = [10, 20, 30, 40]
a = MyArray.from(elements)
println(a)
println('includes(30): ' + a.includes(30))
println('includes(35): ' + a.includes(35))
// elements: 10,20,30,40
// includes(30): true
// includes(35): false


findClosestToEnd(compareFct)


Problem Statement
Write an algorithm named "findClosestToEnd" to find
the matching element that is closest to either end.
If we do not find match, return -1
Return the index for the closest match.

If the first and last matches are equal, return the first index (i.e. first wins a tie).

Hint: Are there any sister/helper methods we could call in MyArray to help us with this logic?

Also see this...
Expected Output (Running Algorithm)
var elements, a, compareFct
elements = [5, 10, 0, 10, 8, 3];
a = MyArray.from(elements);
println(a);
println('----')
compareFct = elem => elem.equals(10)
println('findClosestToEnd(compareFct for 10) (expecting 1)')
println(a.findClosestToEnd(compareFct))
println('----')
compareFct = elem => elem.equals(8)
println('findClosestToEnd(compareFct for 8) (expecting 4)')
println(a.findClosestToEnd(compareFct ))
// elements: 5,10,0,10,8,3
// ----
// findClosestToEnd(compareFct for 10) (expecting 1)
// 1
// ----
// findClosestToEnd(compareFct for 8) (expecting 4)
// 4


lastIndexOf(anElem)


Problem Statement
Return index of last elem in our elements that matches
arg "anElem". If no match then return -1.
Expected Output (Running Algorithm)
var elements, a
elements = [30, 10, 20, 30, 40]
a = MyArray.from(elements);
println(a)
println('lastIndexOf(30): ' + a.lastIndexOf(30));
println('lastIndexOf(99): ' + a.lastIndexOf(99));
// elements: 30,10,20,30,40
// lastIndexOf(30): 3
// lastIndexOf(99): -1


filter(selectFct)


Problem Statement
filter(fct)
Return a new MyArray object where elements are "filtered"
(kept). By "selectFct" we mean that if the function
returns true, for a given element, then that elem
is kept as part of the result.
Note: MyArray do [not] support adding elements, so
this algo may take some special thought.
Expected Output (Running Algorithm)
var a, selectFct;
a = new MyArray([10, 50, 10, 40, 50, 60, 10]);
//filter (keep) all elements less or equal to 20
selectFct = anElem => anElem <= 20;
println(a)
println(a.filter(selectFct))
//select all elements greater than 100
//should result in empty array
selectFct = anElem => anElem > 100
println('----')
println(a)
println(a.filter(select))


reject(rejectFct)


Problem Statement
reject(fct)
Return a new MyArray object where elements are "filtered out"
(rejected). By "rejectFct" we mean that if the function
returns true, for a given element, then that elem
is rejected as part of the result (otherwise it's kept)
Note: MyArray do [not] support adding elements, so
this algo may take some special thought.
Expected Output (Running Algorithm)
var a, rejectFct;
a = new MyArray([10, 50, 10, 40, 50, 60, 10]);
//reject all elements less or equal to 20 (keep others)
rejectFct = anElem => anElem <= 20;
println(a)
println(a.filter(rejectFct))
//reject all elements greater than 100 (keep others)
//should result in keeping all
rejectFct = anElem => anElem > 100
println('----')
println(a)
println(a.filter(rejectFct))