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 setMIT problem set
01The Knapsack Search Tree: Every Combination, 2ⁿ Nodes02Recursive maxVal: An Exhaustive Search That Beats Greedy03Memoization: Turning Slow Fibonacci into fastFib04Overlapping Subproblems and the Memoized KnapsackProblem setMIT problem set
Graph Models
01Graphs as Models: Nodes, Edges and Euler's Seven Bridges02Coding a Graph in Python: Node, Edge, Digraph and Graph03Depth-First Search for the Shortest Path in a Graph04Breadth-First Search: A Queue of Paths, Fewest Hops FirstProblem setMIT problem set
Stochastic Thinking
01Predictive Nondeterminism and the Stochastic Die Roll02Probability as Counting, and When Multiplying Fails03Simulating Rare Events: Random Seeds and Many Trials04The Birthday Problem: Formula Versus SimulationProblem setMIT problem set
01The Drunkard's Walk: Location, Drunk and Field Classes02Sanity Checks: Catching a Plausible Random-Walk Bug03Plotting Walk Distance: Square Root versus Straight Line04Wormhole Fields: Overriding a Method and Styling PlotsProblem setMIT problem set
01Monte Carlo Simulation: Populations, Samples and Trust02Simulating Roulette and the Law of Large Numbers03The Gambler's Fallacy and Regression to the Mean04The House Edge, Variance and Standard Deviation05Confidence Intervals for a Roulette Simulation06The Normal Curve, PDFs and the Empirical RuleProblem setMIT problem set

Coding challenges on monte carlo simulation

Problem 1Monte Carlo Simulation: Populations, Samples and Trust

Estimate a Card Game's Chance of Winning

Like Ulam's solitaire, some card games are easy to play and hard to count. In this one you lay the cards numbered 0 to numCards - 1 out in shuffled order and win if no card lands in the position equal to its own number. Write estimateNoMatch(numCards, numDeals) that plays numDeals random deals and returns the fraction that were wins.

print(estimateNoMatch(1, 50))   # 0.0

One card is always in its place, so it never wins; with 10 cards the estimate from 5,000 deals comes out near 0.37.

1
2
3
4
5
6
7
8
import random

def estimateNoMatch(numCards, numDeals):
    """Return the fraction of numDeals random deals of the cards
    0..numCards-1 in which no card sits at the position of its number."""
    pass

Scratchpad— run any Python to test ideas
1
2
3
4
5
# Scratchpad — run any Python here to test ideas.
# Anything you write below stays separate from the problem's tests.

print("hello, world")

Python runtime ready in a moment
Python runtime ready in a moment
Visible tests · 2Examples — click to expand
?
one card never wins
?
returns a fraction
Hidden tests · 3Bodies hidden — pass/fail only
?
Hidden test 1
?
Hidden test 2
?
Hidden test 3