CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
AllenDowney

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: AllenDowney/ModSimPy
Path: blob/master/chap03.py
Views: 531
1
from modsim import *
2
3
def step(state, p1, p2):
4
"""Simulate one time step.
5
6
state: bikeshare State object
7
p1: probability of an Olin->Wellesley ride
8
p2: probability of a Wellesley->Olin ride
9
"""
10
if flip(p1):
11
bike_to_wellesley(state)
12
13
if flip(p2):
14
bike_to_olin(state)
15
16
from modsim import *
17
18
def bike_to_olin(state):
19
"""Move one bike from Wellesley to Olin.
20
21
state: bikeshare State object
22
"""
23
if state.wellesley == 0:
24
state.wellesley_empty += 1
25
return
26
state.wellesley -= 1
27
state.olin += 1
28
29
from modsim import *
30
31
# Solution
32
33
def bike_to_wellesley(state):
34
"""Move one bike from Olin to Wellesley.
35
36
state: bikeshare State object
37
"""
38
if state.olin == 0:
39
state.olin_empty += 1
40
return
41
state.olin -= 1
42
state.wellesley += 1
43
44
45