15
DSAN 5500: Data Structures, Objects, and Algorithms in Python
Thursday, April 9, 2026
Today’s Planned Schedule:
| Start | End | Topic | |
|---|---|---|---|
| Lecture | 6:30pm | 6:45pm | Final Projects → |
| 6:45pm | 7:00pm | HW4 on Jetstream → | |
| 7:00pm | 8:00pm | Beyond Embarrassingly Parallel → | |
| Break! | 8:00pm | 8:10pm | |
| 8:10pm | 9:00pm | Data Mining in Parallel → |
Hofstadter’s Law (Paraphrase)
The pieces of your DSAN final project will take longer than you expect, even if you take Hofstadter’s Law into account
ThingContainer.| Linear | Logarithmic | Constant | ||
|---|---|---|---|---|
LinkedList\(O(n)\) |
\(\rightarrow\) | BST\(O(\log_2(n))\) |
\(\rightarrow\) | HashTable\(O(1 + \varepsilon)\) |
| Linear | Logarithmic | Constant | ||
|---|---|---|---|---|
| Insertion-Sort \(O(n^2)\) |
\(\rightarrow\) | Merge-Sort \(O(n\log_2(n))\) |
\(\rightarrow\) | ??? |
0 \(\leadsto\) def make_zero(): return 0 🤯)map() and reduce() are “meta-functions”: functions that take other functions as inputslambda):When a program doesn’t work, each function is an interface point where you can check that the data are correct. You can look at the intermediate inputs and outputs to quickly isolate the function that’s responsible for a bug.
(from Python’s “Functional Programming HowTo”)
# Convert to lowercaseEasy case: found typo in punctuation removal code. Fix the error, add comment like # Remove punctuation
First “application” of FP thinking: transform these comments into function names (why?)
Hard case: Something in load_text() modifies a variable that later on breaks remove_punct() (Called a side-effect)
Second “application” of FP thinking: Get rid of these side effects! (why?)
remove_punct()!!! 😎 ⏱️ = 💰From Leskovec et al. (2014)
Pro-FP argument: “80% of outcomes are the result of 20% of inputs”
Specifically: one argument made for FP is that imperative and/or Object-Oriented* code leads to
20% of cases (corner cases) causing 80% of issues with code
Corner cases = cases where we have to “open up” a function and “trace” an issue line-by-line:
FP goal: Function signatures tell their full story (“success” in FP \(\propto\) never have to look “inside” a function!)
Sounds good to me!
So far so good…
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[6], line 1 ----> 1 get_first_character("") Cell In[4], line 2, in get_first_character(s) 1 def get_first_character(s): ----> 2 return s[0] IndexError: string index out of range
Ok, I’m a little scared after the last slide, but I’ll try to trust this function… 🙈
Perhaps I can trust this function after all? 🥹
[In FP] we focus on functions that don’t lie. We want their signatures to tell the whole story about the body (Plachta 2022)
An Object-Oriented Tip Calculator:
class TipCalculator:
def __init__(self):
self.names = []
self.tip_percentage = 0
def add_person(self, name: str):
self.names.append(name)
if(len(self.names) > 5):
self.tip_percentage = 20
else:
self.tip_percentage = 10
def get_names(self) -> list[str]:
return self.names
def get_tip_percentage(self) -> int:
return self.tip_percentage
my_calc = TipCalculator()
my_calc.add_person("Jeff")
my_calc.get_tip_percentage()10
A Functional Tip Calculator:
class TipCalculatorFP:
def add_person(
names: list[str], name: str
) -> list[str]:
updated = names.copy()
updated.append(name)
return updated
def get_tip_percentage(
names: list[str]
) -> int:
if len(names) > 5:
return 20
elif len(names) > 0:
return 10
else:
return 0
names = []
names = TipCalculatorFP.add_person(names, "Jeff")
TipCalculatorFP.get_tip_percentage(names)10
10
Do we need to name a whole new function each time we want to add a name? No!
[1, 4, 9]
compute_powers()) commit code and go to sleep 😴exponent argument would tell user they need to explicitly specify exponent before using your function!)exponent environment variable)1000000.0
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[20], line 1 ----> 1 parse_money_amount("1'000'000") Cell In[19], line 2, in parse_money_amount(money_amount) 1 def parse_money_amount(money_amount: str) -> float: ----> 2 return float(money_amount.replace(",","")) ValueError: could not convert string to float: "1'000'000"
From Leskovec et al. (2014), which is (legally) free online!
From Cornell Virtual Workshop, “Understanding GPU Architecture”
DSAN 5500 Week 12: Final Projects, Functional Programming, Map-Reduce