generated from mwc/lab_dice
completed checkpoint 1
From what i've understood, classes are supposed to make it easier to call on a group of objects/methods all at once. I think a good simulation to use classes for would be an epidemic. Person would be one class and the methods would be susceptible, infected, recovered. Virus and Interactions would be two more classes.
This commit is contained in:
@@ -18,6 +18,16 @@ class FiveDice:
|
||||
if face != 1:
|
||||
return False
|
||||
return True
|
||||
def is_three_of_a_kind(self):
|
||||
count={}
|
||||
for face in self.faces():
|
||||
count[face]=count.get(face, 0)+1
|
||||
return any(count >= 3 for count in count.values())
|
||||
def is_four_of_a_kind(self):
|
||||
count={}
|
||||
for face in self.faces():
|
||||
count[face]=count.get(face, 0)+1
|
||||
return any(count >= 4 for count in count.values())
|
||||
|
||||
dice = FiveDice()
|
||||
successes = 0
|
||||
@@ -26,8 +36,24 @@ for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.all_ones():
|
||||
successes += 1
|
||||
print("all ones", successes/trials)
|
||||
|
||||
print(successes/trials)
|
||||
|
||||
dice = FiveDice()
|
||||
successes = 0
|
||||
trials = 1000000
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_three_of_a_kind():
|
||||
successes += 1
|
||||
print("three of a kind", successes/trials)
|
||||
|
||||
dice = FiveDice()
|
||||
successes = 0
|
||||
trials = 1000000
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_four_of_a_kind():
|
||||
successes += 1
|
||||
print("four of a kind", successes/trials)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user