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

Coding challenges on stochastic thinking

Problem 1Predictive Nondeterminism and the Stochastic Die Roll

Roll a Loaded Die

Write the function rollLoaded() that simulates a loaded six-sided die: it returns an int from 1 to 6, where a 6 is three times as likely as each of the other faces, and the faces 1–5 are equally likely. Use the random module; every call must be a fresh random roll.

Over many rolls, about 3 in 8 should be 6's and about 1 in 8 should be 1's.

1
2
3
4
5
6
7
8
import random

def rollLoaded():
    """Return a random int from 1 to 6. A 6 is three times as likely as
    any other face; faces 1-5 are equally likely."""
    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
?
returns ints from 1 to 6
?
a 6 comes up about 3/8 of the time
Hidden tests · 3Bodies hidden — pass/fail only
?
Hidden test 1
?
Hidden test 2
?
Hidden test 3