Discussion 2: Control, Python Lists
While and If
Learning to use if
and while
is an essential skill. During this discussion, focus on what we've studied in the first three lectures:
if
: runs code only when a condition is truewhile
: repeats code as long as a condition is true- assignment (
=
): stores a value in a variable - comparison (
<
,>
,==
, ...): checks relationships between values - arithmetic:
+
,-
,*
,/
Please don't use features of Python that we haven't discussed in class yet, such as for
, range
, and lists. We'll have plenty of time for those later in the course, but
now is the time to practice the use of if
(textbook section
1.5.4)
and while
(textbook section
1.5.5).
Q1: Fizzbuzz
Implement the classic Fizz Buzz
sequence. The fizzbuzz
function
takes a positive integer n
and prints out a single line for each integer
from 1 to n
. For each i
:
- If
i
is divisible by both 3 and 5, printfizzbuzz
. - If
i
is divisible by 3 (but not 5), printfizz
. - If
i
is divisible by 5 (but not 3), printbuzz
. - Otherwise, print the number
i
.
Try to make your implementation of fizzbuzz
concise.
Problem Solving
A useful approach to implementing a function is to work step by step— for example, we’ll walk through the is_prime
problem to see how this looks in practice:
- Pick an example input and corresponding output.
Pick
n
is 9 as the input andFalse
as the output. - Describe a process in English that computes the output from the input.
Here's a process: Check that
9
(n
) is not a multiple of any integers between 1 and9
(n
). - Figure out what additional variables you'll need.
Introduce
i
to represent each number between 1 and 9 (n
). - Implement the process in code.
Implement
is_prime
. - Test that the implementation works on your original example.
Check that
is_prime(9)
will returnFalse
by thinking through the execution of the code. - Test that the implementation really works on other examples. (If not, you might need to revise step 2.)
Check that
is_prime(3)
will returnTrue
andis_prime(1)
will returnFalse
.
Important: It's highly recommended that you don't check your work using a computer right away.
- Instead, talk to people around you and reason it out.
- On exams, you won’t have access to Python, so practice thinking through examples.
- Drawing an environment diagram can help!
This approach doesn't go straight from reading a question to writing code. Try it out on the next two problems. If you're not sure about how something works or get stuck, ask for help from the course staff.
Q2: Is Prime?
Write a function that returns True
if a positive integer n
is a prime
number and False
otherwise.
A prime number n is a number that is not divisible by any numbers other than 1 and n itself. For example, 13 is prime, since it is only divisible by 1 and 13, but 14 is not, since it is divisible by 1, 2, 7, and 14.
Use the %
operator: x % y
returns the remainder of x
when divided by y
.
Q3: Unique Digits
Write a function that returns the number of unique digits in a positive integer.
Run in 61A CodeHints: You can use
//
and%
to separate a positive integer into its one's digit and the rest of its digits.You may find it helpful to first define a function
has_digit(n, k)
, which determines whether a numbern
has digitk
.