An algorithm will be used to decide whether every number in a list is either a 4 or a 5.
There are two proposals for the algorithm:
Algorithm 1: Set only45 to false. Iterate through each number in the list. If the number is not equal to 4
and not equal to 5, set only45 to false. Otherwise, set only45 to true.
Algorithm 2: Set only45 to true. Iterate through each number in the list. If the number is not equal to 4
and not equal to 5, set only45 to false.
Which of these statements are true about these algorithms?
I. Algorithm 1 does not work on lists where a number other than 4 and 5 occurs before the last number.
II. Algorithm 2 does not work on lists which contain entirely 4s or entirely 5s.An algorithm will be used to
decide whether every number in a list is either a 4 or a 5.
There are two proposals for the algorithm:
Algorithm 1: Set only45 to false. Iterate through each number in the list. If the number is not eq -
AnswersI only
he goal of the procedure computeAverage is to calculate the average of a list of numbers, ignoring any 0
values. For example, if given the list [0, 1, 3, 5], it should return the number 3.
PROCEDURE computeAverage(numbers) { total ← 0 count ← 0 FOR num IN numbers { IF (num ≠ 0)
{ total ← total + num count ← count + 1 } ELSE { RETURN total/count } } RETURN total/count }
A programmer tests the procedure with various inputs and finds multiple cases where it does not
produce the expected output.
Which calls to computeAverage() return an incorrect average? - AnswerscomputeAverage([22, 0, 49, 8])
computeAverage([22, 9, 0, 17])
The two algorithms below are both intended to calculate the product of squares from 1 to n, where n is
any positive integer.
For example, if n is 333, the algorithms should calculate a product of 363636, from 1^2 \times 2^2
\times 3^212×22×321, squared, times, 2, squared, times, 3, squared.
Algorithm 1:
i ← n product ← 0 REPEAT n TIMES { product ← product * (i * i) i ← i - 1 }
Algorithm 2:
, i ← 0 product ← 1 REPEAT n TIMES { product ← product * (i * i) i ← i + 1 }
Which statements best describe the behavior of the algorithms? - AnswersNeither algorithm calculates
the correct product.
A programmer develops the procedure makeNumsList() to generate lists of numbers starting at start
and ending with end. For example, makeNumsList(4, 7) should return [4, 5, 6, 7].
PROCEDURE makeNumsList(start, end) { numsList ← [] numTimes ← (end - start) + 1 num ← 1 REPEAT
numTimes TIMES { APPEND(numsList, num) num ← num + 1 } RETURN numsList }
The programmer tests it with makeNumsList(1, 4) and sees a return value of [1, 2, 3, 4].
Can the programmer conclude that the procedure works correctly for all inputs? - AnswersNo, they've
only verified that it works for that test case. Additional analysis and reasoning is necessary.
An algorithm will be used to calculate the difference between the smallest and largest values in a list.
For the list of [10, 3, 5, 6], it should calculate a difference of 7.
There are two proposals for the algorithm:
Algorithm 1: Set minVal to the first value in the list and maxVal to the last value in the list. Iterate
through each number in the list. If the number is greater than maxVal, store it in maxVal. If the number
is less than minVal, store it in minVal. After loop, set maxDiff to the difference between maxVal and
minVal.
Algorithm 2: Set minVal to 1000 and maxVal to 0. Iterate through each number in the list. If the number
is greater than maxVal, store it in maxVal. If the number is less than minVal, store it in minVal. After loop,
set maxDiff to the difference between maxVal and minVal. - AnswersII only
The goal of the procedure longestWord is to return the longest word in a given list of words. For
example, if it's given the list ["superhero", "captain", "marvel"], it should return "superhero" as the
longest word.
PROCEDURE longestWord(words) { maxWordLen ← 0 maxWord ← "" FOR word IN words { IF
(LENGTH(word) > maxWordLen) { maxWord ← word maxWordLen ← LENGTH(word) RETURN
maxWord } } RETURN maxWord }
A programmer tests the procedure with various inputs and finds that there are multiple cases where it
does not produce the expected output.
Which calls to longestWord() do not actually return the longest word? - AnswerslongestWord(["crown",
"diamond", "castle"])
longestWord(["frog", "seagull", "mermaid"])