Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import collections
- player_re = re.compile(r'^(?P<first>\S+)\s(?P<last>\S+)(?P<hof>\+?)\s(?P<pos>\([A-Z\-,/]*\))\s(?P<years>[\d\-]+)$', re.MULTILINE)
- players = collections.defaultdict(list)
- class Player(object):
- def __init__(self, first, last, hof='', pos='', years=''):
- self.first = first
- self.last = last
- self.hof = hof
- self.pos = pos
- self.years = years
- def __str__(self):
- return f"{self.first} {self.last}{self.hof} {self.pos} {self.years}"
- def load_players():
- global players
- fn = "foosball.txt"
- with open(fn) as pf:
- raw_players = pf.readlines()
- # filter out players who would break regex
- raw_players = [p.strip() for p in raw_players if p.count(' ') == 3] # First Last (Pos) Years
- for pstr in raw_players:
- pm = player_re.match(pstr)
- if not pm:
- print("Regex failed on {!r}".format(pstr))
- continue
- P = Player(**pm.groupdict())
- players[P.first].append(P)
- def print_chain(chain):
- for i, n in enumerate(chain):
- print(f'{i+1:03}. {n}')
- def traverse(name, chain):
- if name not in players:
- yield chain
- for p in players[name]: # todo could sort more greedily
- if p not in chain:
- for res in traverse(p.last, chain + [p]):
- yield res
- yield chain
- def main():
- load_players()
- longest_chain = []
- i = 0
- for fn in sorted(players, key=lambda n: len(players[n]), reverse=True): # start with most common names
- chain = []
- print(i)
- for res in traverse(fn, chain):
- if len(res) > len(longest_chain):
- print("\n{}. new longest chain ({}):".format(i, len(res)))
- print_chain(res)
- longest_chain = res
- i += 1
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement