Discussion 2: Control🖨️
While and If
Learning to use if and while is essential. During this discussion,
focus on what we've studied in the first three lectures: if, while,
assignment (=), comparison (<, >, ==, ...), and arithmetic. Please don't
use any 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, but
now is the time to practice if (textbook
1.5.4)
and while (textbook
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
iis divisible by both 3 and 5, printfizzbuzz. - If
iis divisible by 3 (but not 5), printfizz. - If
iis divisible by 5 (but not 3), printbuzz. - Otherwise, print the number
i.
Try to make your implementation of fizzbuzz concise.
def fizzbuzz(n):
"""
>>> result = fizzbuzz(16)
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
>>> print(result)
None
"""
"*** YOUR CODE HERE ***"
Problem Solving
A useful approach to implementing a function is to:
- Pick an example input and corresponding output.
- Describe a process (in English) that computes the output from the input using simple steps.
- Figure out what additional names you'll need to carry out this process.
- Implement the process in code using those additional names.
- Determine whether the implementation really works on your original example.
- Determine whether the implementation really works on other examples. (If not, you might need to revise step 2.)
Importantly, this approach doesn't go straight from reading a question to writing code.
Important: Don't check your work using a computer right away. Instead, talk to your group and think to try to figure out if an answer is correct. On exams, you won't be able to guess and check because you won't have a Python interpreter. Now is a great time to practice checking your work by thinking through examples. You could even draw an environment diagram!
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.
The approach to implementing functions described above would go:
- Pick
nis 9 as the input andFalseas the output. - Invent a process to determine whether 9 is prime, such as: Check that
9(n) is not a multiple of any integers between 1 and9(n). - Introduce
ito represent each number between 1 and 9 (n). - Implement
is_prime - Check that
is_prime(9)will returnFalseby thinking through the execution of the code. - Check that
is_prime(3)will returnTrueandis_prime(1)will returnFalse.
def is_prime(n):
"""
>>> is_prime(10)
False
>>> is_prime(7)
True
>>> is_prime(1) # one is not a prime number!!
False
"""
"*** YOUR CODE HERE ***"
Hint
Here's a while statement that goes through all numbers above 1 and below n:
i = 2
while i < n:
...
i = i + 1
You can use n % i == 0 to check whether i is a factor of n. If it is,
return False.
Description Time: Come up with a one sentence description of the
process you implemented to solve is_prime that you think someone could
understand without looking at your code. Try not to just read your code, but
instead describe the process it carries out.
Recommended, but more challenging (if there's time)
Q3: Repeating
Definition: A positive integer n is a repeating sequence of positive
integer m if n is written by repeating the digits of m one or more times.
For example, 616161 is a repeating sequence of 61, but 61616 is not.
Implement repeating which takes positive integers t and n. It returns
whether n is a repeating sequence of some t-digit integer.
Tip: You can use
//and%to separate a positive integer into its last few digits and all the ones before those.
def repeating(t, n):
"""Return whether t digits repeat to form positive integer n.
>>> repeating(1, 6161)
False
>>> repeating(2, 6161) # repeats 61 (2 digits)
True
>>> repeating(3, 6161)
False
>>> repeating(4, 6161) # repeats 6161 (4 digits)
True
>>> repeating(5, 6161) # there are only 4 digits
False
"""
if pow(10, t-1) > n: # make sure n has at least t digits
return False
end = _____
while n:
if n % pow(10, t) != end:
return _____
_____
return True
Hint
The iterative process needed to implement this function is to repeatedly check
that the last t digits of the current n match the last t digits of the
original n, then remove the last t digits of the current n.
Q4: Unique Digits
Write a function that returns the number of unique digits in a positive integer. Also implement has_digit, which
determines whether a number n contains digit k, and call it in your
implementation of unique_digits.
Tip: You can use
// 10and% 10to separate a positive integer into its one's digit and the rest of its digits.
def unique_digits(n):
"""Return the number of unique digits in positive integer n.
>>> unique_digits(8675309) # All are unique
7
>>> unique_digits(13173131) # 1, 3, and 7
3
>>> unique_digits(101) # 0 and 1
2
"""
"*** YOUR CODE HERE ***"
def has_digit(n, k):
"""Returns whether k is a digit in n.
>>> has_digit(10, 1)
True
>>> has_digit(12, 7)
False
"""
assert k >= 0 and k < 10
"*** YOUR CODE HERE ***"
Hint
One approach is to loop through every digit from 0 to 9 and check whether n
has the digit. Count up the ones it has.