Feedback — Week 5 Exercise


Top of Form

Question 2

Consider the code:

def mystery(s):

i = 0

result = »

while not s[i].isdigit():

result = result + s[i]

i = i + 1

return result

Select the function call(s) that result in an error.

Your Answer Score Explanation
mystery(’123′) 0.25
mystery(‘abc’) 0.25
mystery(‘abc123′) 0.25
mystery(’123abc’) 0.25
Total 1.00 / 1.00

Question Explanation

Run the code. To understand why the error occurs (or does not), trace the code in the visualizer.

Question 3

Consider the code:

def example(L):

»’ (list) -> list

»’

i = 0

result = []

while i < len(L):

result.append(L[i])

i = i + 3

return result

Which is the best docstring description for function example?

Your Answer Score Explanation
Return a list containing every third item from L starting at index 0. 1.00
Total 1.00 / 1.00

Question Explanation

Run the code several times, passing various lists as arguments, to gain a better understanding of what it does.

Question 4

def compress_list(L):

»’ (list of str) -> list of str

Return a new list with adjacent pairs of string elements from L

concatenated together, starting with indices 0 and 1, 2 and 3,

and so on.

Precondition: len(L) >= 2 and len(L) % 2 == 0

>>> compress_list(['a', 'b', 'c', 'd'])

['ab', 'cd']

»’

compressed_list = []

i = 0

while i < len(L):

compressed_list.append(L[i] + L[i + 1])

# MISSING CODE HERE

return compressed_list

Select the missing line of code.

Your Answer Score Explanation
i = i + 2 1.00
Total 1.00 / 1.00

Question Explanation

Run the code to gain a better understanding of what it does and what effect each choice has.

Question 6

Consider the code:

def while_version(L):

i = 0

sum = 0

while i < len(L) and L[i] % 2 != 0:

sum = sum + L[i]

i = i + 1

return sum

The while loop stops as soon as an even number is found, and the sum of all the previous numbers is returned. The four functions below use a for loop to try to accomplish the same task, although they keep iterating through all of the numbers in L regardless of whether the numbers are even or odd. Only one of them returns the same value as function while_version. Which one is it?

Your Answer Score Explanation

def for_version(L):

found_even = False

sum = 0

for num in L:

if num % 2 != 0:

sum = sum + num

elif not found_even:

found_even = True

return sum

0.00
Total 0.00 / 1.00

Question Explanation

Pay close attention to the variables involved in the loop condition: i and L. Read through the code or trace it in the visualizer to determine what that while loop does and when it exits. You should also run each of the for_version functions.

Question 7

Consider the code:

>>> letters = ['b', 'd', 'a']

>>> # MISSING CODE HERE

>>> print(letters)

['a', 'b', 'd']

Which of the following code fragments(s) could be the missing code in the program above?

Your Answer Score Explanation
letters.sort() 1.00
Total 1.00 / 1.00

Question Explanation

Run the code and try each code fragment.

Question 8

Consider the code:

veggies = ['carrot', 'broccoli', 'potato', 'asparagus']

veggies.insert(veggies.index(‘broccoli’), ‘celery’)

print(veggies)

What is displayed by the code above?

Your Answer Score Explanation
['carrot', 'celery', 'broccoli', 'potato', 'asparagus'] 1.00
Total 1.00 / 1.00

Question Explanation

Run the code and read the help for list.index and list.insert.

Question 9

Your younger sibling has just discovered music from the 1970′s. They have put together a playlist of the same 5 songs repeated again and again. Here are the songs:

  • ABC by The Jackson 5
  • Venus by Shocking Blue
  • Lola by the Kinks
  • Let It Be by the Beatles
  • Cecilia by Simon and Garfunkel


    Here is an example of their playlist:
    ['Lola', 'Venus', 'Lola', 'Lola', 'Let It Be', 'Lola', 'ABC', 'Cecilia', 'Lola', 'Lola']
    You want to make sure that Lola only gets played 3 times, so you want to complete this function that edits the playlist:

def cap_song_repetition(playlist, song):

»’(list of str, str) -> NoneType

Make sure there are no more than 3 occurrences of song in playlist.

»’

Select the loop(s) that accomplish this.

Your Answer Score Explanation

while playlist.count(song) > 3:

playlist.pop(playlist.index(song))

0.25

while playlist.count(song) > 3:

playlist.pop(song)

0.25

while playlist.count(song) > 3:

playlist.remove(playlist.index(song))

0.25

while playlist.count(song) > 3:

playlist.remove(song)

0.25
Total 1.00 / 1.00

Question 10

Consider the code:

>>> a = [1, 2, 3]

>>> b = a

>>> # MISSING CODE HERE

>>> print(a, b)

[1, 'A', 3] [1, 'A', 3]

Which of the following code fragments(s) could be the missing code in the program above?

Your Answer Score Explanation

b[1] = ‘AB’

a[1] = a[1][0]

0.00

a[1] = ‘A’

0.25

b[-2] = ‘A’

0.25

a = [1, 'A', 3]

b = [1, 'A', 3]

0.25
Total 0.75 / 1.00

Question Explanation

Run the code, substituting each code fragment in for the missing code.

Question 11

Consider the code:

>>> a = [1, 2, 3]

>>> b = [1, 2, 3]

>>> # MISSING CODE HERE

>>> print(a, b)

[1, 'A', 3] [1, 'A', 3]

Which of the following code fragments(s) could be the missing code in the program above?

Your Answer Score Explanation

b[-2] = ‘A’

0.25

a = [1, 'A', 3]

b = [1, 'A', 3]

0.25

a[1] = ‘A’

0.25

b[1] = ‘AB’

a[1] = a[1][0]

0.25
Total 1.00 / 1.00

Question Explanation

Run the code, substituting each code fragment in for the missing code.

Question 12

Consider the code:

def increment_items(L, increment):

i = 0

while i < len(L):

L[i] = L[i] + increment

i = i + 1

values = [1, 2, 3]

print(increment_items(values, 2))

print(values)

What is printed by the program above?

Your Answer Score Explanation

None

[1, 2, 3]

0.00
Total 0.00 / 1.00

Question Explanation

Run the code.

Question 13

Select the code fragment(s) that print [3, 6, 9].

Your Answer Score Explanation

values = []

for num in range(1, 3):

values.append(num * 3)

print(values)

0.25

values = []

for num in range(3, 10, 3):

values.append(num)

print(values)

0.25

values = []

for num in range(1, 4):

values.append(num * 3)

print(values)

0.25

values = []

for num in range(3, 9, 3):

values.append(num)

print(values)

0.25
Total 1.00 / 1.00

Question Explanation

Call help(range) to learn about the function range and its two optional arguments.

Question 14

Select the function calls to range that, when used to fill in the blank, cause the code to produce the results below.

for num in __________________:

print(num)

The loop should print this:

3

11

19

Your Answer Score Explanation
range(3, 19, 8) 0.25
range(3, 20, 8) 0.25
range(3, 23, 8) 0.25
range(3, 8, 20) 0.25
Total 1.00 / 1.00

Question Explanation

Call help(range) to learn about the function range and it’s two optional arguments.Bottom of Form

About these ads

Feedback — Week 5 Exercise: Один комментарий

Добавить комментарий

Заполните поля или щелкните по значку, чтобы оставить свой комментарий:

Логотип WordPress.com

Вы комментируете при помощи своего аккаунта WordPress.com. Выход / Изменить )

Фотография Twitter

Вы комментируете при помощи своего аккаунта Twitter. Выход / Изменить )

Фотография Facebook

Вы комментируете при помощи своего аккаунта Facebook. Выход / Изменить )

Google+ photo

Вы комментируете при помощи своего аккаунта Google+. Выход / Изменить )

Connecting to %s