Jun-07-2024, 08:06 PM
(This post was last modified: Jun-07-2024, 08:06 PM by deanhystad.)
What do you want as a result? Just the win/loss/longest streak info for each participant?
A start:
Neither code above handles player names that contain a space. It also doesn't handle different players having the same name. I was looking at a list of MLB players and saw there have been 10 Abreus, three playing right now. If Jose Abreu was playing Bryan Abreu, the players dictionary would only have one Abreu who played a lot of games and had a 500 average.
A start:
from collections import defaultdict players = defaultdict(list) results = [] with open("data.csv", "r") as file: for line in file: if line.startswith("Game #"): next(file) # Skip blank next(file) # Skip --- year, p1, *p1_results = next(file).split() year, p2, *p2_results = next(file).split() p1_results = int(p1_results[-1]) p2_results = int(p2_results[-1]) players[p1].append(1 if p1_results > p2_results else 0) players[p2].append(1 if p2_results > p1_results else 0)Another way (more robust way) to get the player results is do some pattern matching. This code assume all lines containing game results starts with a 4 digit year. It tolerates additional lines between the "Game #" line and the first player.
from collections import defaultdict from itertools import islice import re # Added to itertools in python 3.12 def batched(iterable, n): """batched('ABCDEFG', 3) → ABC DEF G""" if n < 1: raise ValueError("n must be at least one") iterator = iter(iterable) while batch := tuple(islice(iterator, n)): yield batch players = defaultdict(list) lines = [] with open("data.csv", "r") as file: for line in file: if re.match(r"\d{4} ", line): lines.append(line.split()) for p1, p2 in batched(lines, n=2): p1_total = int(p1[-1]) p2_total = int(p2[-1]) players[p1[1]].append(int(p1_total > p2_total)) players[p2[1]].append(int(p2_total > p1_total))Now you have a list of game results for each player, with 1 when they win and a 0 when they lose. Sum the game results to get the number of wins. The length of the game results is how many games are played. The trickiest part remaining is computing the longest winning streak.
Neither code above handles player names that contain a space. It also doesn't handle different players having the same name. I was looking at a list of MLB players and saw there have been 10 Abreus, three playing right now. If Jose Abreu was playing Bryan Abreu, the players dictionary would only have one Abreu who played a lot of games and had a 500 average.