Lesson 12 of 21 · 52 min · UG / professional
Recursion, tiny functions, map and filter
A loop is usually clearer than recursion for campus totals. lambda is a nameless one-line def. map and filter are loops with extra syntax — you will still write the loop.
Roll-number search
Scan a list; sort a copy.
You will be able to
- ✓ Prefer a loop for campus totals; know recursion as a function that calls itself
- ✓ See lambda as a nameless one-line def you can replace with a tiny named function
- ✓ Write map and filter as ordinary loops (the laptop names are extra syntax)
A loop is usually clearer than recursion for hostel totals
Summing hall counts 1 through n is a while with i = i + 1. Recursion is the same job as a function that calls itself with a smaller n until a stop test. Both can be correct. Recursion on a campus total is how internships hit a recursion limit and then “fix” it by raising the limit. Use a loop unless the data is actually a tree (a folder of folders, a JSON of nested fees).
When you do recurse, the first line is the stop: if n <= 0: return 0. Then return n + sum_upto(n - 1). Practise a tiny countdown in this box so the call is not a mystery when a later course walks a menu tree.
lambda, map, filter are loops with extra hats
lambda amount: amount * 118 / 100 is a nameless one-line def. The moment you need a name, an if, or a docstring, write def add_gst. map(add_gst, bases) on a laptop walks bases and collects results — you already wrote that as a new list plus append. filter(passed, marks) keeps items that pass a test — also a loop. You will still write the loop here. The laptop names are not faster; they are shorter.
A lambda inside map is how 1 a.m. code becomes unreadable. Name the function. Keep map/filter for when the team already uses them, not as a personality.
Laptop: recursion, lambda, map, filter
def sum_upto(n):
if n <= 0:
return 0
return n + sum_upto(n - 1)
add_gst = lambda amount: amount * 118 / 100
print(list(map(add_gst, [100, 200])))
print(list(filter(lambda m: m >= 40, [32, 41, 55])))
Words that matter
- Recursion
- A function that calls itself with a smaller problem, plus a stop test.
- lambda
- A nameless one-line function — replace with def when it grows.
- map / filter
- Laptop names for a transform loop and a keepers loop.
Common mistakes
Avoid: Recursing on a 40,000-row gate log “because it looks mathematical”.
Do this: A while or for for flat totals; recursion for real trees.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — the same total as a loop
1+2+3+4 = 10. No self-call.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — filter and map as two lists
Keep marks >= 40, then GST-map 100 and 200. 41 and 118 appear.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — A tiny recursive countdown (stop first)
Prints 2 then 1 then Done. The stop is n <= 0.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Keep passes (filter loop)
marks = [32, 41, 55]. Collect values >= 40. Print so 41 and 55 appear.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.