Practice Problems

Practice Problems

The main chapters of this book teach by guided invention: you derive each algorithm yourself, one small step at a time, before the concept is named. These 47 exercises are a different mode — drill practice. Each notebook gives you one concrete formula or algorithm and asks you to implement it in Python from scratch. There is no arc or narrative; every notebook stands alone.

When to use these. Work through the five groups in order — Expressions, If/Else, Loops, Functions, Recursion — as you finish the corresponding LBI chapter, or as warm-ups before the ML chapters. The ML themes (gradient descent, entropy, neural networks) preview concepts you will invent in full later; if a problem feels too hard, come back after the relevant chapter.

How each notebook works. Read the problem statement. Fill in the starter cell wherever you see # TODO. Run the test cell at the bottom — if it prints All tests passed!, you’re done.


01 — Expressions

Best done after: Chapter 3 (Expressions & Functions)

Each problem is a single Python expression. No loops, no conditionals — just arithmetic, math.exp, and one or two variables.

# Difficulty Concept Open in Colab
1 Easy Mean of three numbers Colab
2 Easy Z-score for one value Colab
3 Easy Min-max scale one value Colab
4 Medium Euclidean distance (2-D) Colab
5 Medium Probability of two independent events Colab
6 Medium Squared error for one prediction Colab
7 Medium Sigmoid activation function Colab
8 Hard One term of a covariance sum Colab
9 Hard One gradient descent update step Colab

02 — If / Else

Best done after: Chapter 4 (If/Else)

Each problem requires a conditional choice — "A" vs "B", "pure" vs "mixed", "train" vs "val" vs "test". Threshold-based decisions are everywhere in ML.

# Difficulty Concept Open in Colab
1 Easy Flag a single outlier by z-score Colab
2 Easy Classify a probability as likely or unlikely Colab
3 Easy Turn a sigmoid output into a class label Colab
4 Medium Classify one prediction as TP / FP / TN / FN Colab
5 Medium Assign a point to its nearest centroid Colab
6 Medium Classify correlation strength and direction Colab
7 Medium Min-max normalize without dividing by zero Colab
8 Hard Assign a sample to train / val / test by position Colab
9 Hard Decide whether a tree node is pure Colab

03 — Loops

Best done after: Chapter 8 (Loops & Arrays)

Each problem asks you to accumulate a result by looping over a list. The themes run from basic statistics (mean, std dev) up to gradient descent and entropy — you will invent the full versions of those ideas in Chapters 15 and 21.

# Difficulty Concept Open in Colab
1 Easy Compute the mean with a loop Colab
2 Easy Sum of squared deviations Colab
3 Medium Compute the standard deviation Colab
4 Medium Find all outliers in a dataset Colab
5 Medium Min-max normalize an entire list Colab
6 Medium Distances from a query point to a training set Colab
7 Hard Build a confusion matrix over a dataset Colab
8 Hard Compute covariance between two lists Colab
9 Hard Run several iterations of gradient descent Colab
10 Hard Compute the entropy of a list of labels Colab

04 — Functions

Best done after: Chapter 3 (Expressions & Functions) and Chapter 8 (Loops & Arrays)

These problems ask you to compose smaller functions into larger ones — the same building-block pattern the book uses throughout. The capstone notebooks (regression pipeline, entropy + information gain, sigmoid + perceptron) are mini-versions of full LBI chapters.

# Difficulty Concept Open in Colab
1 Easy mean() function Colab
2 Easy variance() using mean() Colab
3 Easy std_dev() using variance() Colab
4 Medium find_outliers() from stats functions Colab
5 Medium Simple k-nearest-neighbors classifier Colab
6 Medium precision(), recall(), and f1_score() Colab
7 Medium correlation() from covariance and std dev Colab
8 Hard Linear regression pipeline (predict, mse, gradient_descent_fit) Colab
9 Hard entropy() and information_gain() Colab
10 Hard sigmoid() and perceptron() Colab

05 — Recursion

Best done after: Chapter 5 (Recursion)

Each problem has a natural recursive structure: a base case and a case that reduces to a smaller version of itself. The ML themes (learning-rate decay, tree depth) appear in the Gradient Descent and Decision Tree chapters.

# Difficulty Concept Open in Colab
1 Easy Recursive sum of a list Colab
2 Easy Recursive product of a list (geometric mean) Colab
3 Medium Recursive sum of squared deviations Colab
4 Medium Recursive factorial (for counting outcomes) Colab
5 Medium Binomial coefficient via Pascal’s rule Colab
6 Medium Binary search over sorted distances Colab
7 Hard Recursive merge sort (to find the median) Colab
8 Hard Recursive learning-rate decay schedule Colab
9 Hard Recursive depth of a decision tree Colab