D. Watch the video: Sequences, doing the next set of questions while you do so. D. For the next set of questions, consider the following three function definitions:
def foo1(seq):
total = 0
for k in range(len(seq) // 2):
total = total + seq[1 + (2 * k)]
return total


def foo2(seq):
total = 0
for k in range(1, len(seq), 2):
total = total + seq[k]
return total


def foo3(seq):
total = 0
m = 1
for _ in range(len(seq) // 2):
total = total + seq[m]
m = m + 2
return total
1. To what number does foo1([3]) evaluate? a. 0~ b. 1 c. 2 d. 3 e. 4 f. 5 g. 6 h. 7 i. 8 j. 9 k. 10 l. 11 m. 12 n. 13 o. 14 p. 15 q. 16 r. 17 r. 18 r. 19 r. 20 r. 24 r. 33 r. 38 1. To what number does foo1([3, 6]) evaluate? a. 0 b. 1 c. 2 d. 3 e. 4 f. 5 g. 6~ h. 7 i. 8 j. 9 k. 10 l. 11 m. 12 n. 13 o. 14 p. 15 q. 16 r. 17 r. 18 r. 19 r. 20 r. 24 r. 33 r. 38 1. To what number does foo1([3, 6, 1, 4, 9, 5]) evaluate? a. 0 b. 1 c. 2 d. 3 e. 4 f. 5 g. 6 h. 7 i. 8 j. 9 k. 10 l. 11 m. 12 n. 13 o. 14 p. 15~ q. 16 r. 17 r. 18 r. 19 r. 20 r. 24 r. 33 r. 38 1. To what number does foo2([3]) evaluate? a. 0~ b. 1 c. 2 d. 3 e. 4 f. 5 g. 6 h. 7 i. 8 j. 9 k. 10 l. 11 m. 12 n. 13 o. 14 p. 15 q. 16 r. 17 r. 18 r. 19 r. 20 r. 24 r. 33 r. 38 1. To what number does foo2([3, 6]) evaluate? a. 0 b. 1 c. 2 d. 3 e. 4 f. 5 g. 6~ h. 7 i. 8 j. 9 k. 10 l. 11 m. 12 n. 13 o. 14 p. 15 q. 16 r. 17 r. 18 r. 19 r. 20 r. 24 r. 33 r. 38 1. To what number does foo2([3, 6, 1, 4, 9, 5]) evaluate? a. 0 b. 1 c. 2 d. 3 e. 4 f. 5 g. 6 h. 7 i. 8 j. 9 k. 10 l. 11 m. 12 n. 13 o. 14 p. 15~ q. 16 r. 17 r. 18 r. 19 r. 20 r. 24 r. 33 r. 38 1. To what number does foo3([3]) evaluate? a. 0~ b. 1 c. 2 d. 3 e. 4 f. 5 g. 6 h. 7 i. 8 j. 9 k. 10 l. 11 m. 12 n. 13 o. 14 p. 15 q. 16 r. 17 r. 18 r. 19 r. 20 r. 24 r. 33 r. 38 1. To what number does foo3([3, 6]) evaluate? a. 0 b. 1 c. 2 d. 3 e. 4 f. 5 g. 6~ h. 7 i. 8 j. 9 k. 10 l. 11 m. 12 n. 13 o. 14 p. 15 q. 16 r. 17 r. 18 r. 19 r. 20 r. 24 r. 33 r. 38 1. To what number does foo3([3, 6, 1, 4, 9, 5]) evaluate? a. 0 b. 1 c. 2 d. 3 e. 4 f. 5 g. 6 h. 7 i. 8 j. 9 k. 10 l. 11 m. 12 n. 13 o. 14 p. 15~ q. 16 r. 17 r. 18 r. 19 r. 20 r. 24 r. 33 r. 38 D. For the next set of questions, consider the following two attempts at a function that returns the first negative item in the sequence of numbers, or None if the sequence contains no negative numbers. One is correct and the other is wrong.
def find1(numbers):
for k in range(len(numbers)):
if numbers[k] < 0:
return numbers[k]
else:
return None


def find2(numbers):
for k in range(len(numbers)):
if numbers[k] < 0:
return numbers[k]
return None


1. To what number does find1([-3, 6, 1, 4, 9, 5]) evaluate? ANS. -3 1. To what number does find2([-3, 6, 1, 4, 9, 5]) evaluate? ANS. -3 1. To what number does find1([3, 6, 1, 4, 9, 5]) evaluate? ANS. None 1. To what number does find2([3, 6, 1, 4, 9, 5]) evaluate? ANS. None 1. To what number does find1([3, -6, 1, 4, 9, 5]) evaluate? ANS. None 1. To what number does find2([3, -6, 1, 4, 9, 5]) evaluate? ANS. -6 1. Which attempt is correct? a. find1 b. find2~ D. For each of the following problems, indicate which pattern(s) is most suitable for a solution to the problem. 1.
"""
What comes in:
-- a sequence of numbers
What goes out: Returns the INDEX of the first negative number
in the given sequence of numbers, or None if the sequence
contains no negative numbers.
Note: "first" negative number means the negative number
whose index is smallest -- see the examples.
Side effects: None.
Examples: If the argument is:
-- [4, 30, -19, 8, -3, -50, 100], this function returns 2
since the first negative number is -19, which is at index 2

-- [-8, 44, 33], this function returns 0
since the first negative number is -8, which is at index 0

-- [1, 29, 22, 8], this function returns None
since the list contains no negative numbers
"""
a. The FIND pattern~ b. The "Looking two places in the sequence at once" pattern c. The "Looking at two sequences in parallel" pattern d. The MAX/MIN pattern 1.
    """
What comes in:
-- a non-empty sequence of strings
What goes out: Returns the shortest string in the given sequence
of strings. If there is a tie for shortest string, returns the one
(among the ties) whose index is smallest.
Side effects: None.
Examples:
If the argument is:
['all', 'we', 'are saying', 'is', 'give', 'peace', 'a chance']
then this function returns 'we'

If the argument is:
['all we', 'are saying', 'is give', 'peace', 'a chance']
then this function returns 'peace'

If the argument is:
['all we are saying', 'is give', 'peace a chance']
then this function returns 'is give'

If the argument is ['abc'], then this function returns 'abc'.
"""
a. The FIND pattern b. The "Looking two places in the sequence at once" pattern c. The "Looking at two sequences in parallel" pattern d. The MAX/MIN pattern~ 1.

"""
What comes in:
-- a sequence of numbers
-- a positive integer n that is less than or equal to
the length of the given sequence
What goes out: INDEX of the largest number in the first n numbers
of the given sequence of numbers. If there is a tie for largest
number, returns the smallest of the indices of the tied numbers.
Side effects: None.
Examples:
If the first argument is:
[90, 0, 100, 200, -5, 100, -10, 200, 15]
and the second argument n is 3,
then this function returns 2 (because 100, at index 2,
is the largest of the first 3 numbers in the list).

Another example: for the same list as above, but with n = 2,
this function returns 0 (because 90, at index 0,
is the largest of the first 2 numbers in the list).

Yet another example: For the same list as above, but with n = 9,
this function returns 3 (because 200, at indices 3 and 7,
is the largest of the first 9 numbers in the list,
and we break the tie in favor of the smaller index).
"""
a. The FIND pattern b. The "Looking two places in the sequence at once" pattern c. The "Looking at two sequences in parallel" pattern d. The MAX/MIN pattern~ 1.
    """
What comes in:
-- a string s
What goes out: Returns the number of times a letter is repeated
twice-in-a-row in the given string s.
Side effects: None.
Examples:
-- number_of_stutters('xhhbrrs') returns 2
-- number_of_stutters('xxxx') returns 3
-- number_of_stutters('xaxaxa') returns 0
-- number_of_stutters('xxx yyy xxxx') returns 7
-- number_of_stutters('xxxyyyxxxx') returns 7
-- number_of_stutters('') returns 0
"""
a. The FIND pattern b. The "Looking two places in the sequence at once" pattern~ c. The "Looking at two sequences in parallel" pattern d. The MAX/MIN pattern 1.
    """
What comes in:
-- a string s that (in this simple version of the palindrome
problem) contains only lower-case letters
(no spaces, no punctuation, no upper-case characters)
What goes out: Returns True if the given string s is a palindrome,
i.e., reads the same backwards as forwards.
Returns False if the given string s is not a palindrome.
Side effects: None.
Examples:
abba reads backwards as abba so it IS a palindrome
but
abbz reads backwards as zbba so it is NOT a palindrome

Here are two more examples: (Note: I have put spaces into the
strings for readability; the real problem is the string WITHOUT
the spaces.)
a b c d e x x e d c b a reads backwards as
a b c d e x x e d c b a
so it IS a palindrome
but
a b c d e x y e d c b a reads backwards as
a b c d e y x e d c b a
so it is NOT a palindrome
"""
a. The FIND pattern~ b. The "Looking two places in the sequence at once" pattern~ c. The "Looking at two sequences in parallel" pattern d. The MAX/MIN pattern 1.
    """
What comes in:
-- two sequences that have the same length
What goes out: Returns the number of indices at which the two
given sequences have the same item at that index.
Side effects: None.
Examples:
If the sequences are:
(11, 33, 83, 18, 30, 55)
(99, 33, 83, 19, 30, 44)
then this function returns 3
since the two sequences have the same item at:
-- index 1 (both are 33)
-- index 2 (both are 83)
-- index 4 (both are 30)

Another example: if the sequences are:
'how are you today?'
'HOW? r ex u tiday?'
then this function returns 8 since the sequences are the same
at indices 5 (both are 'r'), 10 (both are 'u'), 11 (both are ' '),
12 (both are 't'), 14 (both are 'd'), 15 (both are 'a'),
16 (both are 'y') and 17 (both are '?') -- 8 indices.
"""
a. The FIND pattern b. The "Looking two places in the sequence at once" pattern c. The "Looking at two sequences in parallel" pattern~ d. The MAX/MIN pattern