CS61A Lab 1
easterling

Lab 1: Functions, Control lab01.zip

Due by 11:59pm on Wednesday, August 31.

Syllabus Quiz

Q1: Syllabus Quiz

Please fill out the Syllabus Quiz, which is based off of our policies found on the course syllabus.

What Would Python Display? (WWPD)

Q2: WWPD: Control

Use Ok to test your knowledge with the following “What Would Python Display?” questions:

1
python3 ok -q control -u✂️
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
>>> def xk(c, d):
... if c == 4:
... return 6
... elif d >= 4:
... return 6 + 7 + c
... else:
... return 25
>>> xk(10, 10)
______
>>> xk(10, 6)
______
>>> xk(4, 6)
______
>>> xk(0, 0)
______
>>> def how_big(x):
... if x > 10:
... print('huge')
... elif x > 5:
... return 'big'
... elif x > 0:
... print('small')
... else:
... print("nothing")
>>> how_big(7)
______
>>> how_big(12)
______
>>> how_big(1)
______
>>> how_big(-1)
______

Hint: Make sure your while loop conditions eventually evaluate to a false value, or they’ll never stop! Typing Ctrl-C will stop infinite loops in the interpreter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> n = 3
>>> while n >= 0:
... n -= 1
... print(n)
______
>>> positive = 28
>>> while positive:
... print("positive?")
... positive -= 3
______
>>> positive = -9
>>> negative = -12
>>> while negative:
... if positive:
... print(negative)
... positive += 3
... negative += 3
______

Q3: WWPD: Veritasiness

Use Ok to test your knowledge with the following “What Would Python Display?” questions:

1
python3 ok -q short-circuit -u✂️
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
>>> True and 13
______
>>> False or 0
______
>>> not 10
______
>>> not None
______
>>> True and 1 / 0 and False
______
>>> True or 1 / 0 or False
______
>>> True and 0
______
>>> False or 1
______
>>> 1 and 3 and 6 and 10 and 15
______
>>> -1 and 1 > 0
______
>>> 0 or False or 2 or 1 / 0
______
>>> not 0
______
>>> (1 + 1) and 1
______
>>> 1/0 or True
______
>>> (True or False) and False
______

Q4: Debugging Quiz

The following is a quick quiz on different debugging techniques that will be helpful for you to use in this class. You can refer to the debugging article to answer the questions.

Code Writing Questions

Q5: Falling Factorial

Let’s write a function falling, which is a “falling” factorial that takes two arguments, n and k, and returns the product of k consecutive numbers, starting from n and working downwards. When k is 0, the function should return 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def falling(n, k):
"""Compute the falling factorial of n to depth k.

>>> falling(6, 3) # 6 * 5 * 4
120
>>> falling(4, 3) # 4 * 3 * 2
24
>>> falling(4, 1) # 4
4
>>> falling(4, 0)
1
"""
"*** YOUR CODE HERE ***"
i = 0
res = 1
while i < k:
res *= (n-i)
i += 1
return res

Q6: Sum Digits

Write a function that takes in a nonnegative integer and sums its digits. (Using floor division and modulo might be helpful here!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def sum_digits(y):
"""Sum all the digits of y.

>>> sum_digits(10) # 1 + 0 = 1
1
>>> sum_digits(4224) # 4 + 2 + 2 + 4 = 12
12
>>> sum_digits(1234567890)
45
>>> a = sum_digits(123) # make sure that you are using return rather than print
>>> a
6
"""
"*** YOUR CODE HERE ***"
count = 0
while y > 0:
count += y % 10
y = y//10
return count

Extra Practice

These questions are optional and will not affect your score on this assignment. However, they are great practice for future assignments, projects, and exams. Attempting these questions can be valuable in helping cement your knowledge of course concepts.

Q7: WWPD: What If?

Use Ok to test your knowledge with the following “What Would Python Display?” questions:

1
python3 ok -q if-statements -u✂️

Hint: print (unlike return) does not cause the function to exit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> def ab(c, d):
... if c > 5:
... print(c)
... elif c > 7:
... print(d)
... print('foo')
>>> ab(10, 20)
______
>>> def bake(cake, make):
... if cake == 0:
... cake = cake + 1
... print(cake)
... if cake == 1:
... print(make)
... else:
... return cake
... return make
>>> bake(0, 29)
______
>>> bake(1, "mashed potatoes")
______

Q8: Double Eights

Write a function that takes in a number and determines if the digits contain two adjacent 8s.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def double_eights(n):
"""Return true if n has two eights in a row.
>>> double_eights(8)
False
>>> double_eights(88)
True
>>> double_eights(2882)
True
>>> double_eights(880088)
True
>>> double_eights(12345)
False
>>> double_eights(80808080)
False
"""
"*** YOUR CODE HERE ***"
count = 0
while n > 0:
if n % 10==8:
count += 1
n = n//10
return count==2
 Comments
Comment plugin failed to load
Loading comment plugin