CS61A Lab 1
Lab 1: Variables & Functions, Control lab01.zip
What Would Python Display? (WWPD)
Q1: WWPD: Control
Use Ok to test your knowledge with the following “What Would Python Display?” questions:
1 python3 ok -q control -uHint: Make sure your
while
loop conditions eventually evaluate to a false value, or they’ll never stop! TypingCtrl-C
will stop infinite loops in the interpreter.
1 | >>> def xk(c, d): |
Hint: Make sure your
while
loop conditions eventually evaluate to a false value, or they’ll never stop! TypingCtrl-C
will stop infinite loops in the interpreter.
1 | >>> positive = 28 |
Q2: WWPD: Veritasiness
Use Ok to test your knowledge with the following “What Would Python Display?” questions:
1 python3 ok -q short-circuit -u✂️
1 | >>> True and 13 |
Q3: 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.
Use Ok to test your understanding:
1 | python3 ok -q debugging-quiz -u✂️ |
Parsons Problems
To work on these problems, open the Parsons editor:
1 | python3 parsons |
Q4: Add in Range
Complete add_in_range
, which returns the sum of all integers between start
and stop
(inclusive).
1 | def add_in_range(start, stop): |
Q5: Digit Position Match
A number has a digit-position match if the i
th-to-last digit is i
. For example, 980
has the 0
th-to-last digit as 0
. Or 98276
has the 2
nd-to-last digit as a 2
.
Write a function that determine if a number n
has a digit-position match at a k
th-to-last digit k
.
1 | def digit_pos_match(n, k): |
Code Writing Questions
Q6: 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 | def falling(n, k): |
Use Ok to test your code:
1 | python3 ok -q falling✂️ |
Q7: 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 | def sum_digits(y): |
Use Ok to test your code:
1 | python3 ok -q sum_digits✂️ |
Submit
Make sure to submit this assignment by running:
1 | python3 ok --submit |
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.
Q8: 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:
return
) does not cause the function to exit.
1 | >>> def ab(c, d): |
Q9: K-Occurrence
Complete k_occurrence
, a function which returns the number of times the digit k
appears in num
. 0 is considered to have no digits.
1 | def k_occurrence(k, num): |
To work on this problem, open the Parsons editor:
1 | python3 parsons |
Q10: Double Eights
Write a function that takes in a number and determines if the digits contain two adjacent 8s.
1 | def double_eights(n): |
Use Ok to test your code:
1 | python3 ok -q double_eights |