0% found this document useful (0 votes)
12 views6 pages

Check-In Week 2 Solutions

The document presents a Python programming exercise involving user input and Fibonacci numbers, requiring the completion of code snippets to achieve specific outputs. It also discusses function naming and efficiency in two different solutions for a carrot harvesting task in a programming context. The document includes questions related to memory diagrams and code completion, emphasizing understanding of programming concepts.

Uploaded by

assismyfavfood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Check-In Week 2 Solutions

The document presents a Python programming exercise involving user input and Fibonacci numbers, requiring the completion of code snippets to achieve specific outputs. It also discusses function naming and efficiency in two different solutions for a carrot harvesting task in a programming context. The document includes questions related to memory diagrams and code completion, emphasizing understanding of programming concepts.

Uploaded by

assismyfavfood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Can Computers Think?

(CSC 106) Checking your understanding

Consider the following (incomplete) Python program:

x = int(input("Please input a positive integer: "))


counter = x
for step in range(0, 10):
# example 1: I would argue this is the best of the three examples
# because the creation of counter implies we will want to use it elsewhere,
# and that we don’t want to change x

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

Notes: You do not need to introduce any additional variables.


There is more than one way to achieve this result.

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.

1. print (“How many Fibonacci numbers would you like to see?”)


2. limit = int (input(“Please input a number greater than 2: ”))
3.
4. fib_0 = 0
5. fib_1 = 1
6.
7. print(fib_0)
8. print(fib_1)
9. for count in range(1, limit):
10. next_fib = fib_0 + fib_1
11. print(next_fib)
12. fib_0 = fib_1
13. fib_1 = next_fib
This is a clever way to calculate Fibonacci numbers very quickly with each number
building on the previous one. Trying to calculate them going the opposite direction
usually ends up with a VASTLY slower program.

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

3. What should the <variable name>-<value/object> associations be when line 11 has


been executed for the second time? Draw a memory diagram.

limit 10

fib_0 1

fib_1 1

next_fib 2

4. Draw a memory diagram that shows the <variable name>-<value/object>


associations after the last number has been printed (i.e. at the end of the program).

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:

1. Both programs define a number of functions.


Unfortunately, the program designers were
transported from the bad old days when names
needed to be as short as possible, so the names
they chose for their functions are essentially
meaningless. Please try to come up with better
names for each of the functions that they
defined.

current name better name

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

is better and why?

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()

You might also like