Lab 1
Intro to Python
Instructions: https://inst.eecs.berkeley.edu/~cs61a/su19/lab/lab01/
Solution: https://github.com/tomthestrom/cs61a/blob/master/lab/lab01/lab01.py
The first 2 questions are non-coding - WWPD (What Would Python Display?)
Q3: Fix the Bug
def both_positive(x, y):
"""Returns True if both x and y are positive.
>>> both_positive(-1, 1)
False
>>> both_positive(1, 1)
True
"""
return x and y > 0 # You can replace this line!
Q3: Solution
def both_positive(x, y): """Returns True if both x and y are positive.
>>> both_positive(-1, 1)
False
>>> both_positive(1, 1)
True
"""
return x > 0 and y > 0 # You can replace this line!
Q4: Sum Digits
def sum_digits(n):
"""Sum all the digits of n.
>>> sum_digits(10) # 1 + 0 = 1
1
>>> sum_digits(4224) # 4 + 2 + 2 + 4 = 12
12
>>> sum_digits(1234567890)
45
>>> x = sum_digits(123) # make sure that you are using return rather than print
>>> x
6
"""
"*** YOUR CODE HERE ***"
Q4: Solution
def sum_digits(n):
"""Sum all the digits of n.
>>> sum_digits(10) # 1 + 0 = 1
1
>>> sum_digits(4224) # 4 + 2 + 2 + 4 = 12
12
>>> sum_digits(1234567890)
45
>>> x = sum_digits(123) # make sure that you are using return rather than print
>>> x
6
"""
"*** YOUR CODE HERE ***"
if n < 10:
return n
last_digit = n % 10
return last_digit + sum_digits(n // 10)
Last updated