Trace the following recursive call. Show your code tracing work and indicate what is printed as a result of the call printMe(4) ?
public void printMe(int num)
{
if (num > 1)
{
printMe(num-1);
System.out.print(num);
printMe(num-1);
}
}
(Solution)L0 | Looping constructs involve repeating an instruction/command/block or a set of instructions/commands/blocks |
L1 | A loop can run a fixed/pre-determined number of repetitions. This is a simple repeat loop. |
L1a | Commands/constructs that achieve simple repetition (such as “Repeat”) |
L2 | #2. All the commands (or blocks) within a loop repeat as a “unit” (not individually) |
L3 | Loop structures can have code preceding and/or following it |
L4 | Interpreting control flow that includes code before & after |
L5 | Variables can control the number of fixed repetitions |
L6 | One type of loop is a forever loop. With a forever loop, the actions in the body of the loop continue “endlessly”. |
L7 | The sequence of commands within a loop matters |
L8 | Loops can have conditionals within them (ability to trace such execution) |
L9 | Loops can aggregate values (thus having a cumulative effect) |
L10 | Loops can terminate based on an expression (evaluating to true or false) |
L10a | Understanding how different commands/constructs can be used for such looping (for, while, repeat until, …) |
L10b | Such loop-termination expressions can involve Boolean variables (AND, OR, NOT) |
L11 | The loop condition is checked at that one point in the code and not at all times |
L12 | Variables can be aggregated/updated within loops (using expressions) |
L12a | A “loop-controlling” variable being updated in the loop is part of the loop termination expression |
L13 | Loops can be nested (what necessitates such nesting? Examples) |
L13a | How nested loops work (tracing) |
L14 | Using loops to iterate through lists & arrays |
L14a | Using loops to iterate through lists & arrays with variable updates within loops |
L14b | Using loops to iterate through lists & arrays with conditionals within the loop |
L14c | Using loops to iterate through lists & arrays with conditionals within the loop as well as variables updated within loop |
The expressions in a ‘while’ statement are continuously checked; the loop exits the instant the expression becomes false |
One looping construct is better than the other one (e.g., while-loops are better than for- loops) based on their familiarity with one type of loop or the order they learned loop constructs, rather than understanding that each looping construct has benefits for solving different problems |
A conditional statement inside a loop will be executed whenever the condition is true, even if this occurs outside of the loop |
We’ve found it useful to show a series of examples with our students, with each example different in a small, subtle way from the previous one. These all have one recursive call, and make the problem smaller by one at each step, so they’re called linear recursion.
You’re given a sentence (in some languages, that means a string), and asked to compute the length. There’s a built-in utility for that, certainly, but by rewriting it, you can test it.
Snap! |
|
Python |
def my_length(sentence): if sentence == “”: return 0 else: return 1 + my_length(sentence[1:]) |
You’re given a list of grades (e.g., A, C, F, D), and asked to return a new list in which you only keep the As. This is different from the first because it’s a list that stores our data, and because we don’t just do the same thing to every element. This is known as a filtering pattern, and Snap! provides keep and Python provides list comprehensions to do these as higher-order functions (HOFs) without having to write a routine ourselves!
Snap! |
|
Python |
def keep_As(grades): if grades == []: return grades else: if grades[0] == “A”: return grades[0:1] + keep_As(grades[1:]) else: return keep_As(grades[1:]) |
Snap! HOF |
|
Python HOF |
>>> [grade for grade in [“A”,”B”,”C”,”A”] if grade == “A”] [‘A’, ‘A’] |
You’re given a list of grades (e.g., A, C, F, D), and asked to return True if it’s all As.
Snap! |
|
Python |
def All_As(grades): if grades == []: return True else: if grades[0] == “A”: return All_As(grades[1:]) else: return False |
Snap! (alternate) |
|
Python (alternate) |
def All_As(grades): if grades == []: return True else: return grades[0] == “A” and All_As(grades[1:]) |
Snap! (clever) |
|
Python (clever) |
def All_As(grades): return (grades == []) or (grades[0] == “A” and All_As(grades[1:])) |