Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class Child:
- def __init__(self, family):
- self.family = family
- @property
- def brothers(self):
- brothers = 0
- for kid in self.family.kids:
- if type(kid) == Boy and kid != self:
- brothers += 1
- return brothers
- class Girl(Child):
- pass
- class Boy(Child):
- pass
- class Family:
- def __init__(self):
- self.kids = []
- self.haveKids()
- def haveKid(self):
- gender = random.choice(('Girl', 'Boy'))
- if gender == 'Girl':
- self.kids.append(Girl(self))
- else:
- self.kids.append(Boy(self))
- def haveKids(self):
- haveAKid = random.choice((True, False))
- while haveAKid:
- self.haveKid()
- haveAKid = random.choice((True, False))
- families = []
- for i in range(1000000):
- families.append(Family())
- girls = 0
- boys = 0
- girlbrothers = 0
- boybrothers = 0
- for family in families:
- for kid in family.kids:
- if type(kid) == Girl:
- girlbrothers += kid.brothers
- girls += 1
- else:
- boybrothers += kid.brothers
- boys += 1
- print('Out of a sample of', girls, 'girls, and', boys, 'boys:')
- print('The average girl has', girlbrothers/girls, 'brothers.')
- print('The average boy has', boybrothers/boys, 'brothers.')
- print('Ratio:', (girlbrothers/girls)/(boybrothers/boys))
Advertisement
Add Comment
Please, Sign In to add comment