Check-In Week 2 Solutions
Check-In Week 2 Solutions
print(counter)
counter += x
# example 2
counter = x*(step+1)
print(counter)
# example 3
print(x * (step+1))
1. Complete the missing lines of code above so that the program will produce the following output if
the user inputs 3.
3
6
9
12
15
18
21
24
27
30
1
The Fibonacci numbers are the following sequence of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …
The first two numbers are 0 and 1. After that each number can be calculated by adding the previous two
together. For example, the third number is 1 (0+1), the fourth number is 2 (1+1), the fifth number is 3
(2+1), etc.
They are named Fibonacci numbers after the Italian mathematician who first described them in Europe.
Patterns based on the Fibonacci sequence show up in surprisingly many contexts in math, biology, and art.
(See the Wikipedia entry if you want to know more.)
Consider the following (incomplete) Python program to list the first few Fibonacci numbers. The
user gets to determine how many Fibonacci numbers to list.
Questions:
For questions 1-4, assume that the program has been completed and functions correctly.
1. Draw a memory diagram that shows the<variable name>-<value/object>
associations after line 5 has been executed. Assume that the user inputted 10. Also show what
will have been printed to the screen at that point.
limit 10
fib_0 0
fib_1 1
2
2. Draw a memory diagram that shows the <variable name>-<value/object>
associations that should exist after line 11 has been executed for the first time. Also show what
line 11 will print to the screen when it is executed for the first time.
limit 10
fib_0 0
fib_1 1
next_fib 1
limit 10
fib_0 1
fib_1 1
next_fib 2
limit 10
fib_0 21
fib_1 34
next_fib 55
5. Complete the code above so that the program prints the first n Fibonacci numbers, where n is
determined by the user. You may need to add new lines of code.
See completed code above.
3
Consider two Python programs shown on page 8.
Both programs will cause Reeborg to harvest all
of the carrots in the Harvest 1 world:
Questions:
a. move_to_start……………………………………………………………………
b. pick_row……………………………………………………………………………
c. left_180…………………………………………………………………………
d. right_180…………………………………………………………………………
e. move_to_start_and_pick_first……………………………………………………
f. finish_row…………………………………………………………………………
g. take_left_row…………………………………………………………………………
h. take_right_row…………………………………………………………………………
4
2. Now that you’ve come up with better names for the functions, which of the solutions do you think
Both solutions have pros and cons, so a reasonable argument can be made in
favor of either one. This is by design.
Personally, I would argue that the first solution is better, because each of the
functions has a more clearly designed purpose. For example, the functions to
turn around and move to the start of the next row move Reeborg to the next
row and nothing else. In contrast, the functions to do the same in the second
solution also pick up the first carrot from the next row, making their purpose
less clear and making it harder to describe how the program is supposed to
work overall.
On the other hand, a reasonable argument can also be made for the second
solution. If I were to argue in favor of the second solution, I would focus on the
fact that it requires fewer instructions overall when running. Because the
default running speed of Reeborg is quite slow, the increased number of
instructions here really drives home the disadvantage of code that sacrifices
efficiency for improved readability and comprehensibility.
Overall, the ideal outcome for this problem is to weigh the trade-offs that
sometimes occur when trying to make code easier to understand or more
efficient. In some cases, the loss in efficiency is worth making the code easier
to read (and therefore maintain), but in other cases, the efficiency of the code
is absolutely critical, even when that efficiency gain results in code that is very
hard for a human programmer to comprehend and maintain.
5
1 def turn_right(): 1 def turn_right():
2 turn_left() 2 turn_left()
3 turn_left() 3 turn_left()
4 turn_left() 4 turn_left()
5 5
6 def a(): 6 def e():
7 move() 7 move()
8 turn_left() 8 turn_left()
9 move() 9 move()
10 move() 10 move()
11 turn_right() 11 turn_right()
12 move() 12 move()
13 13 take()
14 def b(): 14
15 for carrot in range(0, 6): 15 def f():
16 take() 16 for carrot in range(0, 5):
17 move() 17 move()
18 18 take()
19 def c(): 19
20 turn_left() 20 def g():
21 move() 21 f()
22 turn_left() 22 turn_left()
23 move() 23 move()
24 24 turn_left()
25 def d(): 25 take()
26 turn_right() 26
27 move() 27 def h():
28 turn_right() 28 f()
29 move() 29 turn_right()
30 30 move()
31 a() 31 turn_right()
32 for double_row in range(0, 32 take()
3): 33
33 b() 34 e()
34 c() 35 for double_row in range(0,
35 b() 2):
36 d() 36 g()
37 h()
38 g()
39 f()