Lab 2 Solutions

Solution Files

Required Questions


Getting Started Videos

These videos may provide some helpful direction for tackling the coding problems on this assignment.

To see these videos, you should be logged into your berkeley.edu email.

YouTube link

What Would Python Display? (WWPD)

Q1: WWPD: Control

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

python3 ok -q control -u

>>> def xk(c, d):
...     if c == 4:
...         return 6
...     elif d >= 4:
...         return 6 + 7 + c
...     else:
...         return 25
>>> xk(10, 10)
______
23
>>> xk(10, 6)
______
23
>>> xk(4, 6)
______
6
>>> xk(0, 0)
______
25
>>> def how_big(x):
...     if x > 10:
...         print('huge')
...     elif x > 5:
...         return 'big'
...     if x > 0:
...         print('positive')
...     else:
...         print(0)
>>> how_big(7)         # A returned string is displayed with single quotes
______
'big'
>>> print(how_big(7)) # A printed string has no quotes
______
big
>>> how_big(12)
______
huge positive
>>> print(how_big(12))
______
huge positive None
>>> print(how_big(1), how_big(0))
______
positive 0 None None
>>> n = 3
>>> while n >= 0:
...     n -= 1
...     print(n)
______
2 1 0 -1
>>> negative = -12
>>> while negative:  # All numbers are true values except 0
...    if negative + 6:
...        print(negative)
...    negative += 3
______
-12 -9 -3

Q2: WWPD: What If?

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

python3 ok -q if-statements -u

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

>>> def ab(c, d):
...     if c > 5:
...         print(c)
...     elif c > 7:
...         print(d)
...     print('foo')
>>> ab(10, 20)
______
10 foo
>>> 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)
______
1 29 29
>>> bake(1, "mashed potatoes")
______
mashed potatoes 'mashed potatoes'

Write Code

Q3: 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.

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
    """
total, stop = 1, n-k while n > stop: total, n = total*n, n-1 return total

Use Ok to test your code:

python3 ok -q falling

Q4: Divisible By k

Write a function divisible_by_k that takes positive integers n and k. It prints all positive integers less than or equal to n that are divisible by k from smallest to largest. Then, it returns how many numbers were printed.

def divisible_by_k(n, k):
    """
    >>> a = divisible_by_k(10, 2)  # 2, 4, 6, 8, and 10 are divisible by 2
    2
    4
    6
    8
    10
    >>> a
    5
    >>> b = divisible_by_k(3, 1)  # 1, 2, and 3 are divisible by 1
    1
    2
    3
    >>> b
    3
    >>> c = divisible_by_k(6, 7)  # There are no integers up to 6 divisible by 7
    >>> c
    0
    """
count = 0 i = 1 while i <= n: if i % k == 0: print(i) count += 1 i += 1 return count

Use Ok to test your code:

python3 ok -q divisible_by_k

Q5: Sum Digits

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

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
    """
total = 0 while y > 0: total, y = total + y % 10, y // 10 return total

Use Ok to test your code:

python3 ok -q sum_digits

Q6: Double Eights

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

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
    """
prev_eight = False while n > 0: last_digit = n % 10 if last_digit == 8 and prev_eight: return True elif last_digit == 8: prev_eight = True else: prev_eight = False n = n // 10 return False # Alternate solution def double_eights_alt(n): while n: if n % 10 == 8 and n // 10 % 10 == 8: return True n //= 10 return False

Use Ok to test your code:

python3 ok -q double_eights

Check Your Score Locally

You can locally check your score on each question of this assignment by running

python3 ok --score

This does NOT submit the assignment! When you are satisfied with your score, submit the assignment to Gradescope to receive credit for it.

Submit Assignment

Submit this assignment by uploading any files you've edited to the appropriate Gradescope assignment. Lab 00 has detailed instructions.