function find(elements, compareFct) {
let len = elements.length
let i = 0
while (i < len) {
let nextElem = elements[i]
if (compareFct(nextElem))
return i
i = i + 1
}
return -1
}
function findLast(elements, compareFct) {
let len = elements.length
let i = len - 1
while (i >= 0) {
let nextElem = elements[i]
if (compareFct(nextElem))
return i
i = i - 1
}
return -1
}
function findClosestToEnd(elements, compareFct) {
var left, right, firstIndex, lastIndex, leftDistance, rightDistance;
left = 0;
right = elements.length - 1;
firstIndex = find(elements, compareFct);
lastIndex = findLast(elements, compareFct);
leftDistance = firstIndex - left;
rightDistance = right - lastIndex;
return (leftDistance <= rightDistance)
? firstIndex
: lastIndex;
}
//Try It
let nums, matchFct, matchingIndex, closestToEndIndex;
The context for each of these three challenge problems is:
The Examples in this chapter
The other challenge problems in this chapter (e.g. Challenge #2 could use Challenge #1, etc)
This means that anytime we are working on an algorithm, if we "see" another algo in our "context" that will help, we can "reuse" it -- i.e. "call" it -- like we called "make toast" from "Make Tomato Sandwiches" in the previous chapter.
Naturally we want our algorithms to produce the correct result.
But 50% of the scoring on these three challenges will also be based on simplicity -- it may take you multiple iterations (tries) to evolve your algorithm to simplicity.
Algorithm name and input parameters: findLast(elements, compareFct)
findLast(elements, compareFct)
Problem Statement:
Write an algorithm named "findLast" to find the last matching element in a collection named "elements" (provided). Use "compareFct" (provided) to determine if we have a match. Return the index of the match, or "-1" if no match.
Given:
Variable 'elements' that holds a collection of objects
Variable 'compareFct', when evaluated like this "compareFct(anElem)", returns true if "anElem" is a "match"
Sample 1:
Assume we have been provided elements [5, 10, 0, 10, 8, 3]
Assume we are looking for a match of "10" using our new algorithm "findLast"
Then the result (index) should be 3
Index 3 holds the last "10" in the collection (there are two "10" elements in this collection)
Sample 2:
Assume we have been provided elements [5, 10, 0, 1, 8, 3]
Assume we are looking for a match of "10" using our new algorithm "findLast"
Then the result (index) should be 1
Index 1 holds the last "1" in the collection (there is only one "10" in this collection)
Human Language
Write the human language that solves the problem statement.
Note: this step is optional. You may jump directly to the pseudocode if desired. However, it is good practice.
Because it is optional, you may do it in any human language.
findLast(elements, compareFct) {
//SOLUTION HERE
}
Pseudocode
Write the pseudocode that solves the problem statement.
Algorithm name and input parameters: findClosestToEnd(elements, compareFct)
findClosestToEnd(elements, compareFct)
You must call two other algorithms (sub-algorithms) for this algorithm. You will then use the results from those calls. Hint -- select one algo from "Problems" and one algo from challenge #1 or #2
Problem Statement: Write an algorithm named "findClosestToEnd" to find the matching element that is closest to either end (as described in two examples below). Return the matching index.
If we have elements [5, 10, 0, 10, 8, 3]
And if we look for a match of "10"
Then the result (index) would be 1
(left match is closer to left end then right match is to right end)
left match is 1 from end
right match is 2 from end
If we have elements [5, 0, 10, 3, 8, 10]
And if we look for a match of "10"
Then the result (index) would be 5
(right match is closer to right end then left match is to left end)
left match is 2 from end
right match is 0 from end
If a tie, then "left" index wins, return it.
Given:
Variable 'elements' that holds a collection of objects
Variable 'compareFct', when evaluated like this "compareFct(anElem)", returns true if "anElem" is a "match"
Your working directory should be named "intro-pseudocode" -- as is provided in the archive "intro-pseudocode.zip"
Create a ZIP archive file that contains the "intro-pseudocode" directory (with all contents)
Name your zip file in the usual way ("Assignment XX -- FirstName LastName.zip" where "XX" is the assignment D2L drop box name and FirstName and LastName are your corresponding names in D2L). E.g., "A2 -- Suni Lee.zip"
Create a "ZIP" archive type and not other archive types
Do not nest .zip files -- do not put other .zip files into the one ZIP archive file
Your submit should be one ZIP file
Algorithms
(Chapter 102 - Introduction to Pseudocode)