Index
Overview


This section contains examples with solutions.

Note that you will use this a lot: -- MyArray Method Summary...

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.

Start Here


Before we start, these are good topics to quickly review:


Examples Format


Each example write-up on this page has four parts:


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

indexOf(anElem)


Description (Of Algorithm)
Return index of first elem in our elements that matches
arg "anElem". If no match then return -1.
Expected Output (Running Algorithm)
var elements, a
elements = [10, 20, 30, 40]
a = MyArray.from(elements);
println(a)
println('indexOf(30): ' + a.indexOf(30));
println('indexOf(99): ' + a.indexOf(99));
// elements: 10,20,30,40
// indexOf(30): 2
// indexOf(99): -1
Solution (The Algorithm)
Solve the algorithm best you can before clicking "Show".


// indexOf(anElem)
let i = 0
while (i < this.size()) {
	if (this.get(i).equals(anElem))
		return i
	i++
}
return -1


join(separator)


Description (Of Algorithm)
Return our elements joined
into a string where each element is
separated by arg "separator"
Expected Output (Running Algorithm)
var elements, a
elements = [10, 20, 30]
a = MyArray.from(elements)
println(a)
println('join("|"): ' + a.join("|"))
// elements: 10,20,30
// join("|"): 10|20|30
Solution (The Algorithm)
Solve the algorithm best you can before clicking "Show".



Detailed solution...
// join(separator)
var i, result
i = 0
result = "";
while (i < this.size()) {
	let nextElem = this.get(i)
	if (i > 0)
		result += separator;
	result += nextElem.toString();
	i++
}
return result;


findIndex(searchFct)


Description (Of Algorithm)
Write an algorithm named "findIndex" to find
the *first* element that satisfies searchFct.
Arg "searchFct" returns true if we have
found an element satisfying the search.
Return the index of the matching element.
If not match is found, return -1.
Expected Output (Running Algorithm)
var a, searchFct
a = new MyArray([10, 20, 30, 40, 50, 60, 70])
searchFct = anElem => anElem > 50
println(a.findIndex(searchFct))
// outputs 5
a = new MyArray([10, 50, 10, 40, 50, 60, 10])
searchFct = anElem => anElem == 50
println('----')
println(a.findIndex(searchFct))
// outputs 1
Solution (The Algorithm)
Solve the algorithm best you can before clicking "Show".


// findIndex(searchFct)
var i
i = 0
while (i < this.size()) {
	if (searchFct(this.get(i)))
		return i;
	i++
}
return -1;


findLastIndex(searchFct)


Description (Of Algorithm)
Write an algorithm named "findLastIndex" to find
the *last* matching element and return its index.
Arg "searchFct" returns true if we have
found an element satisfying the search.
Return the index of the matching element.
If not match is found, return -1.
Expected Output (Running Algorithm)
var a, searchFct
a = new MyArray([10, 20, 30, 40, 50, 60, 70])
searchFct = anElem => anElem < 50
println(a.findLastIndex(searchFct))
// outputs 3
a = new MyArray([10, 50, 10, 40, 50, 60, 10])
searchFct = anElem => anElem == 50
println('----')
println(a.findLastIndex(searchFct))
// outputs 4
Solution (The Algorithm)
Solve the algorithm best you can before clicking "Show".


// findLastIndex(searchFct)
var i
i = this.size() - 1;
while (i >= 0) {
	if (searchFct(this.get(i)))
		return i;
	i--
}
return -1;


occurrences(matchFct)


Description (Of Algorithm)
occurrences(matchFct)
Return a count (number) of the number of occurrences where
matchFct returns true over our elements.
Expected Output (Running Algorithm)
var a, count, matchFct
a = new MyArray([10, 50, 10, 40, 50, 60, 10])
count = 0
//match if between 20 and 50 (inclusive)
matchFct = anElem => anElem >= 20 && anElem <= 50
println(a)
println('occurrences between 20 and 50 (inclusive)')
println('occurrences(matchFct): ' + a.occurrences(matchFct));
Solution (The Algorithm)
Solve the algorithm best you can before clicking "Show".



Detailed solution...
// occurrences(matchFct)
var i, count
i = 0
count = 0
while (i < this.size()) {
	var nextElem, match
	nextElem = this.get(i)
	match = matchFct(nextElem)
	if (match)
		count++
	i++
}
return count;