CS110A Week 14 Day 1 lesson on Tuesday 11/17/2026
Overview
Indika Walimuni teaches CS110A students to plan and implement FizzBuzz, debug overlapping conditions, and improve the both-divisible check by using the least common multiple of 3 and 5, which is 15. The lesson then introduces Python for loops over strings, lists, tuples, dictionaries, and ranges, including range boundaries and lazy generation, before outlining assignments on sentinel loops, binary conversion, population growth, and estimating pi.
Key takeaways
- In an if/elif chain, test overlapping, specific cases first: FizzBuzz must check divisibility by both 3 and 5 before checking either divisor alone.
- A number is divisible by both 3 and 5 exactly when it is divisible by their least common multiple, 15, reducing the combined test to one modulo operation.
- A while loop needs an update that can change its condition; regenerating the random number prevents the FizzBuzz midpoint loop from remaining stuck on a nonmatching value.
- Python's for variable takes each successive item from an iterable, including string characters, list elements, tuple elements, and dictionary keys.
- Python range includes its start and excludes its stop, defaults to a step of 1, and generates values on demand rather than storing the entire sequence.
Chapters
- The FizzBuzz function accepts an integer and returns a result based on divisibility by 3, 5, or both.
- Indika Walimuni tests the expected outcomes with 12 for Fizz, 20 for Buzz, 15 for FizzBuzz, and 16 for neither.
- The problem practices if, elif, and else branches, with modulo as the divisibility test.
- The plan starts by defining the integer input as a function parameter.
- Modulo checks whether a number is divisible by 3 or 5 by testing whether the remainder equals 0.
- The first draft lists separate conditions for Fizz, Buzz, FizzBuzz, and the fallback result.
- The function is named for the FizzBuzz problem and receives a number parameter.
- A docstring describes what the function returns and refers to its input parameter.
- The lesson emphasizes that documentation tools can use docstrings, while a simple description is sufficient for the course.
- The function call is wrapped in print so its returned value appears in the output.
- Indika demonstrates Compiler Explorer's stdin box for entering keyboard input and notes that it must be enabled.
- Testing 21 returns Fizz, establishing a working single-condition example.
- The first implementation returns Fizz for 15 because the check for divisibility by 3 runs before the FizzBuzz check.
- An if/elif chain stops at the first true condition, so the more specific case must be tested before the less specific cases.
- Moving the check for divisibility by both 3 and 5 to the top fixes the result for 15.
- The corrected function is tested with 15, 20, and 22 to verify FizzBuzz, Buzz, and the fallback outcome.
- Overlapping conditions should be ordered from the most specific case to the more general cases.
- The same ordering issue can affect letter-grade logic, where a broad B condition could incorrectly capture scores that qualify for an A.
- The second function accepts the start and end of an integer range.
- It repeatedly chooses a random integer from that range, calls the FizzBuzz function, and continues until the chosen number equals the range midpoint.
- The word “until” signals a while loop because the number of iterations is not known in advance.
- The midpoint is calculated once as the floor division of the sum of start and end by 2.
- The loop's stopping value is the midpoint, and its condition continues while the random number is not equal to that value.
- The plan separates initialization, the loop condition, the FizzBuzz call, and the update.
- The loop must generate a new random number on each iteration; otherwise, a nonmatching initial value never changes.
- The random number is updated while the start, end, and midpoint remain fixed inputs or derived values.
- The lesson contrasts the need for a changing loop value with the fixed midpoint sentinel.
- The code imports Python's random module and uses random.randint(start, end) to choose an integer.
- The while loop prints the FizzBuzz result for each random number and regenerates the number before checking again.
- For a range from 10 to 100, the midpoint is 55, which is eventually selected as the stopping value.
- The evaluation step asks whether a working solution can use less memory, run faster, or be written more clearly.
- Checking divisibility by both 3 and 5 can be reduced from two modulo operations to one check using 15.
- Fifteen is the least common multiple of 3 and 5, so a number divisible by both is divisible by 15.
- After the FizzBuzz optimization discussion, Indika introduces the day's focus on for loops.
- The class watches a supplementary loop lesson in a language other than Python.
- The clip provides a visual introduction to iteration before the Python examples.
- The embedded lesson defines iteration as repeating a process and surveys for, while, do-while, and other loop forms.
- Its JavaScript App Lab example uses a counter loop to repeat commands, with the loop checking its counter to decide whether to continue.
- A turtle example shows how a loop can call a square-drawing function repeatedly instead of duplicating ten function calls.
- A while loop repeats as long as a condition remains true, while a for loop can process a known sequence.
- The Python pattern is for variable in container, with the loop variable representing an item in the sequence.
- A string example uses the letters in “Python” to demonstrate iteration through a fixed set of characters.
- The loop visits each of the six characters in “Python” in order.
- Printing a constant such as p inside the loop prints it six times, once per character.
- Printing the loop variable displays the actual letters; print's end argument can keep output on one line.
- A class exercise updates the color at index 2 of a color list using a new value read from input.
- The incomplete loop should use a variable such as value in the sequence color_list.
- The loop prints each list value on the same line, reinforcing that the loop variable represents each item.
- Python for loops can iterate over lists and tuples as well as strings.
- The number example loops through values 1 to 5 and performs an operation such as squaring each value.
- Iterating directly over a dictionary visits its keys, demonstrated with a dictionary mapping 1 to Jack and 2 to Jill.
- range(start, stop, step) creates a numeric sequence; the start is included, the stop is excluded, and the default step is 1.
- Converting range(0, 10) to a list reveals values 0 through 9, while a step of 2 produces 0, 2, 4, 6, and 8.
- A negative step supports descending sequences, such as counting down from 100 toward 0 in increments of 5.
- A range stores the information needed to generate values rather than materializing every number in memory, making it useful for large iteration counts.
- The graded discussion asks students to read numbers until a sentinel and determine the smallest input; the individual discussion uses -1 to terminate input.
- A binary-conversion task applies repeated division by 2 and prints remainders in reverse order, so decimal 6 is displayed as 011.
- The population assignment runs a for loop across generations, adding births and subtracting deaths each iteration.
- The day's pi assignment implements a historical infinite series in a loop, with more iterations producing a more accurate approximation.
Transcript excerpt
0:00 Good morning, guys. This is our agenda today. Okay. So we're going to solve this problem. So let's go to that one. Okay. This is also an interview question, right? Fizz Buzz problem. So there's a specific criteria given. So this is like, you know, a bunch of numbers and then if it is divisible by three, print this. By five, print this. Both three and five, print this. And then if not, and
0:47 then So you can see that this this is classic if else problem, right? So an if itself is sitting in there and the problem set statement, okay? So there's one indicator. So that's one vocabulary. Okay. So okay. So that that number, right? So what are the inputs here in this problem? Yeah, so integer number, right? So that's number, right? So that's basically a number. It's an input, okay?
1:36 And you can write like, you know, the the data type also like int, okay? And on the output So basically this criteria, right? So like, you know, if it is divisible by three, fizz likewise, right? So I would say like fizz fizz and then um fizz buzz and then not um fizz or buzz, right? Um something like that, okay? So we have to print something like that based on the number, right? Okay?
2:27 All right. So so let's work out a couple of examples, right? So basically you can take I mean I think we have we are given uh bunch of examples, right? Um um no, okay? So we'll have to actually create the one, okay? So let's take the number uh 12, okay? What should be the so this guy is 12. What should be the outcome? Should return yeah, fizz, right? So this should return fizz. And then if it is uh um
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, indika walimuni.