Discussion 1: Control, Environment Diagrams
Find your group, and then get started on this worksheet on Berkeley time.
Attendance
Your TA will come around during discussion to check you in. You can start on the worksheet before being checked in; you don't need to wait for your TA to get started.
If you didn't attend for a good reason (such as being sick), fill out this form (within 2 weeks of your discussion): attendance formIce Breaker
What's your favorite kitchen utensil? Why?
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, while,
assignment (=), comparison (<, >, ==, ...), and 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).
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.
For example, in the is_prime problem on the next page, you could:
- Pick
nis 9 as the input andFalseas the output. - Here's a process: 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(you get to do this part with your group). - 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.
Important: It's highly recommended that you 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. You could even draw an environment diagram!
Q1: 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.
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.
Q2: 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.
Q3: Ordered Digits
Implement the function ordered_digits, which takes as input a
positive integer and returns True if its digits, read left to right,
are in non-decreasing order, and False otherwise. For example, the
digits of 5, 11, 127, 1357 are ordered, but not those of 21 or 1375.
If you want to discuss this problem with a TA, just ask.
Q4: Unique Digits
Write a function that returns the number of unique digits in a positive integer.
Hints: 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 numbernhas digitk.
Environment Diagrams
Q5: Double Trouble
Draw the environment diagram on paper or a whiteboard (without having the computer draw it for you)! Then, check your work by stepping through the diagram.
Q6: Dream Work
Draw an environment diagram for the code below. Then, step through the diagram with PythonTutor to check your work.
Optional: Exam Practice
If you all finish early, it's a great idea to get ready for Quest by trying out this slight variant of a Spring 2023 Midterm 1 question.
Q7: 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.
t digits of the rest match the last t digits of n, then remove the
last t digits of rest.