-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
chap03.py
43 lines (32 loc) · 854 Bytes
/
chap03.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from modsim import *
def step(state, p1, p2):
"""Simulate one time step.
state: bikeshare State object
p1: probability of an Olin->Wellesley ride
p2: probability of a Wellesley->Olin ride
"""
if flip(p1):
bike_to_wellesley(state)
if flip(p2):
bike_to_olin(state)
from modsim import *
def bike_to_olin(state):
"""Move one bike from Wellesley to Olin.
state: bikeshare State object
"""
if state.wellesley == 0:
state.wellesley_empty += 1
return
state.wellesley -= 1
state.olin += 1
from modsim import *
# Solution
def bike_to_wellesley(state):
"""Move one bike from Olin to Wellesley.
state: bikeshare State object
"""
if state.olin == 0:
state.olin_empty += 1
return
state.olin -= 1
state.wellesley += 1