Ludium
Sign In
Introduction to Computational Thinking and Data Science
Optimization
01Optimization Models and the Knapsack Problem02Brute-Force Knapsack and the Exponential Power Set03Greedy Algorithms: One Function, Any Definition of Best04Lambda Keys and Why Greedy Can Miss the OptimumProblem set0/10MIT problem set0/2

Lambda Keys and Why Greedy Can Miss the Optimum

Why do greedy by value, cost and density pick different meals, and what does hill climbing reveal about their limits?


Finger exercises

Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.

0 / 5 passed
Exercise 1 of 5

A two-parameter lambda

A lambda takes a sequence of identifiers, a colon, then an expression whose value is returned. Using lambda (not def), build an anonymous function of two arguments, value and calories, that returns the value per calorie, and assign it to density.

1
2
3
4
density = ...  # your code here

Python runs in your browser
⌘/Ctrl + Enter runs
?
is a function
?
was made with lambda
?
returns value per calorie for wine
?
works for another food
←Previous Greedy Algorithms: One Function, Any Definition of Best