Midterm 2 CS122
Weeks 4-6 Study Guide
●
Week 41234...
○
Recursion:
■
Definition: A function that calls itself.6
■
Components:
●
Base Case(s) / Stopping Condition(s): Condition(s) that stop the recursion.111
●
Recursive Case: The part of the function that calls itself with smaller/bigger input to get to the base case.1
■
Call Stack Management:
●
Functions are managed using a call stack, which operates on a Last-In, First-Out (LIFO) basis.1213
●
Each function call results in a new "stack frame" being created and pushed onto the call stack.12
●
When a function finishes, its frame is popped off the stack, returning control to the calling function using the return address.1
○
Tracing Recursive Functions:
■
Practice tracing the execution of recursive functions manually to understand how they work step-by-step.67
■
Visualize the call stack and the values of variables at each step.67
●
Week 511121314...
○
Call Stacks (Continued):
■
Call stacks are used for all function calls, not just recursive ones.14
■
They ensure that function calls are executed in the correct order and that local variables are managed properly.14
■
Understanding the role of the call stack is crucial for debugging and understanding program flow.1416
○
Mutability vs. Immutability:
■
Mutability: The ability of an object to be changed after it is created.15
■
Immutability: The inability of an object to be changed after it is created.15
■
Consider which data types in Python are mutable (e.g., lists) and which are immutable (e.g., strings).15
○
Python Tutor/Visualizer:
■
Use online Python tutor or visualizer tools to step through code execution and understand how function calls and variable assignments work.1618
■
These tools can be helpful for debugging and comprehending complex code logic.1618
●
Week 619202122...
○
Refactoring:
■
Definition: Restructuring code to improve its readability, maintainability, and/or efficiency without changing its external behavior.2023
■
Importance:
●
Enhanced Code Clarity20
●
Simplified Maintenance20
●
Potential Efficiency Gains20
■
Techniques:
●
Extracting common code into functions2023
●
Simplifying complex conditional statements2023
●
Renaming variables for better clarity2023
○
Docstrings vs. Comments:
■
Docstrings:
●
String literals enclosed in triple quotes ("""Docstring goes here""") used to document modules, classes, functions, and methods.21
●
Serve as formal documentation that can be accessed using the help() function or tools like Sphinx.21
■
Comments:
●
Lines of code that are not executed, denoted by a # symbol.21
●
Used to explain code logic, provide context, or temporarily disable code.21
○
Strings:
■
Methods: Familiarize yourself with common string methods (e.g., isupper(), isdigit(), find(), capitalize()).23
■
In Operator: Practice using the in operator to check if a substring is present in a string.24
■
Iteration: Understand how to iterate through individual characters in a string using loops (e.g., for loop).24
■
Base Conversion: Be prepared to trace and understand code that converts between different number bases (e.g., binary, decimal, hexadecimal).25