CS110A Week 15 Day 1 lesson on Tuesday 11/24/2026
Overview
Indika Walimuni uses a list-reversal problem to teach algorithm planning, Python indexing, list mutation, and the tradeoffs between returning a new list and reversing in place. The lesson then covers nested function calls and stack frames, functions as objects, common return-value errors, a turtle polygon exercise, module 11 assignments, and final-exam expectations.
Key takeaways
- Building a reversed copy takes O(n) extra storage, while swapping matching elements in the original list can reverse it in place with O(1) auxiliary storage.
- A reverse-index loop can visit list elements from -1 toward the beginning, but Python range excludes its stop value, so the endpoint must be chosen carefully.
- A list modifier changes the object supplied by the caller and typically returns None; call it as a statement instead of assigning its result.
- Breaking a large computation into focused functions improves readability, makes each part easier to test, and allows work such as environment adjustment to be reused.
- Python functions are objects: evaluating len without parentheses refers to the function itself, while assigning a value to the name len hides the built-in and makes it unavailable under that name.
- The stated final exam format is three coding questions and seven short-answer questions in two hours, with a practice exam intended to guide preparation.
Chapters
- The input is a Python list, and the desired result contains the same elements in reverse order.
- The prompt's in-place requirement means changing the supplied list rather than necessarily returning a separate copy.
- Python has built-in reversal options, but the exercise requires implementing the operation manually.
- A for loop with range can visit list indexes in reverse order.
- The example list contains 3, 4, 5, 6, and 7, which should print backward as 7, 6, 5, 4, 3.
- Printing elements backward demonstrates traversal, but it does not yet create a reversed list.
- The loop begins at index -1 and uses a step of -1 to move left through the list.
- Because Python's range excludes its stop value, the stop must extend one position beyond the final negative index.
- Converting range to a list provides a quick way to inspect the generated indexes before using them.
- Create an empty list, then append each original element encountered by the reverse-index loop.
- The same traversal that printed values backward can construct a separate reversed list.
- Append modifies the destination list, so the operation is called as a statement rather than assigned to a variable.
- The four-step plan is to accept a list, initialize an empty result, append elements in reverse index order, and return the result.
- Writing the plan in English makes the conversion from the problem to Python syntax explicit.
- Walimuni emphasizes planning as a programming skill that remains important even when AI can generate code from a clear specification.
- The function takes a list parameter, documents its purpose with a docstring, builds the reversed list, and returns it.
- The loop uses negative indexes to read from the end of the input toward the beginning.
- A printed function call confirms the result, while the original list remains separate from the returned list.
- A new-list solution uses additional storage proportional to the list size, which matters for large inputs such as 10,000 text elements.
- Reversing in place avoids storing a second full list when the original list itself is the desired output.
- The initial approach visits every element, prompting a search for a method that performs fewer iterations.
- Pair the first and last elements, then swap them; the example changes the endpoints 3 and 7.
- Move inward and swap the next pair, such as 4 and 6, while leaving the middle element in place.
- For a five-element list, this example needs two swaps rather than visiting all five positions.
- Save one endpoint in a temporary variable before overwriting its original position.
- Assign the opposite endpoint into the saved element's position, then place the temporary value at the other endpoint.
- The worked example checks that the outer elements change while the middle values remain intact.
- The second swap moves inward from the endpoints, using the next pair of indexes rather than repeating the first swap.
- An initially confusing printout is resolved by explicitly printing the list after the assignment.
- Testing the intermediate state helps verify that the five-element example becomes 7, 6, 5, 4, 3.
- The swap positions must change systematically on each iteration, moving from the outside of the list toward its center.
- Walimuni works through the relationship between a negative index and the corresponding position measured from the opposite end.
- The index pattern is developed from examples before being translated into a loop.
- The in-place algorithm needs a temporary value, two indexed assignments, and a loop that processes only half the list.
- Continuing past the midpoint would swap the elements back and restore the original order.
- The instructor says the completed code will be posted, while the class focuses on the loop's stopping point and swap logic.
- A student presents an alternative implementation using a saved value and a positive range; Walimuni confirms it follows the same swapping strategy.
- Another solution uses list operations such as append and pop, showing that indexes are not the only way to reverse a list.
- Lists are mutable, so a function can modify the caller's list in place; strings are immutable and cannot be changed that way.
- An in-place modifier does not need to return the list, and assigning its call produces None.
- A supplied lesson explains that each function call creates a new stack frame containing that call's local variables and parameters.
- When an inner function returns, its frame is removed and execution resumes in the still-active outer function.
- Function arguments can pass information through multiple nested calls.
- A game example calculates footstep volume from movement type and environment, then compares the volume with a detection threshold.
- Separating movement adjustment, environment adjustment, and detection into smaller functions makes each task easier to read and test.
- A reusable environment-adjustment function can also support a later feature that calculates whether a player's voice gives them away.
- Passing the built-in name len without parentheses refers to the function object rather than calling it.
- The type function identifies the object as a built-in function or method.
- The lesson connects Python's object model to both built-in and user-defined functions.
- Assigning the integer 10 to the name len replaces access to the built-in length function in that scope.
- Calling len on a string after that reassignment raises a TypeError because an integer is not callable.
- Choosing variable names that do not collide with built-ins prevents accidental hiding of useful functions.
- A function that reaches its end without an explicit return produces None, even if a caller assigns its result to a variable.
- A modifier such as a function that changes a list's last element to zero can correctly return nothing.
- Call a modifier as a standalone statement, then inspect the original list to see the in-place change.
- The next exercise generalizes a previously drawn equilateral hexagon into a function for drawing an equilateral polygon.
- For loops make it possible to repeat turtle movement and turning commands according to the polygon's sides.
- The exercise is published in module 11.
- For the functions discussion, students choose two questions, publish their responses, and reply to a classmate.
- A feet-to-steps converter assignment requires a function plan, clear parameter names, and a docstring.
- A code-check assignment asks students to implement four functions for rhombus area and perimeter and stadium area and perimeter.
- Docstrings support automated documentation tools that generate function reference material.
- The course pathways include CS110B for C++, CS111B for Java, and CS131B for further Python study; CS20 is described as Python application programming with data science topics.
- Walimuni says the final exam will include three coding questions and seven short-answer questions, with two hours provided.
- A practice exam will serve as a study guide, and students are encouraged to use it to rehearse the exam format.
- The class closes with a Thanksgiving break reminder and a note that the next meeting will address the final exam.
Transcript excerpt
0:00 Good morning guys. So this is our agenda today. So we're going to go over this uh problem. Uh reverse a list. Okay. All right. So um so we are given a list. We need to um reverse it. So there's already a a method for this one as a built-in function. I think reverse built-in function. So um but we cannot use it. Okay. So so let's let's uh create an example. So I'm going to quickly get this one. All right. Um,
1:17 okay. Um, so one and two. Okay. All right. All right. So, what is the input here? Input a list, right? And then what is the vocabulary that identifying it as the input? Yeah. given. Okay. So this is the word. Okay. So um that's the um the list and then then the the output. Yeah. So reversed list of uh the outcome. Right. So um
2:08 it's list. Okay. All right. And then well you can see that in the improvement it asks okay do it in place. Right. And then um all right so um that means basically our output going to be change input as well. Okay so that basically indicates we don't have to return it because already there the list is already there we pass into the function and inside the function we're going to change it. Okay. [clears throat] All right. So um but let's let's try this as an example first. Okay. So um so let's say uh a list equal uh let's create like a
2:57 like 3 comma 4a 5. So this our list maybe you can get more um six and seven. So what should be the output? Yeah. So return right. So basically return um seven three right so that that should be the case. Okay and then the thing the the thing we need
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.