fixed a bug where this was counting every match, not every name that is a subset
[babynames_answers] / challenge_6.py
1 # 6. How many names are subsets of other names?
2 #
3 # Note: this problem gets really slow when you consider boys and girls names
4 # together. For simplicity, I'm treating them separately.
5
6 import ssadata
7
8 boysNamesSubsets = 0
9 for boysName in ssadata.boys.keys():
10     match = False
11     for otherBoysName in ssadata.boys.keys():
12         if not match and boysName in otherBoysName and otherBoysName != boysName:
13             boysNamesSubsets = boysNamesSubsets + 1
14             match = True
15
16 print(str(boysNamesSubsets) + " boys names are subsets of other boys names.")
17
18 girlsNamesSubsets = 0
19 for girlsName in ssadata.girls.keys():
20     match = False
21     for otherGirlsName in ssadata.girls.keys():
22         if not match and girlsName in otherGirlsName and otherGirlsName != girlsName:
23             girlsNamesSubsets = girlsNamesSubsets + 1
24             match = True
25
26 print(str(girlsNamesSubsets) + " girls names are subsets of other girls names.")
27

Benjamin Mako Hill || Want to submit a patch?