CMU Coding Bootcamp
at main 1.2 kB view raw
1from typing import List, Tuple 2 3 4def topScorer(data: str) -> str | None: 5 """Return the name(s) of the student(s) with the highest total score.""" 6 lines = [line for line in map(lambda line: line.strip(), data.split("\n")) if line] 7 max_score: List[Tuple[str, int]] = [] 8 for line in lines: 9 name, *scores = line.split(",") 10 scores = list(map(int, scores)) 11 if not scores: 12 max_score.append((name, 0)) 13 else: 14 max_score.append((name, sum(scores))) 15 max_score.sort(key=lambda x: x[1], reverse=True) 16 if not max_score: 17 return None 18 names = [name for name, score in max_score if score == max_score[0][1]] 19 return ",".join(names) 20 21 22print("Testing topScorer()...", end="") 23data = """\ 24Joe,10,20,30,40 25Lauren,10,20,30 26Ben,10,20,30,5 27""" 28assert topScorer(data) == "Joe" 29 30data = """\ 31David,11,20,30 32Austin,10,20,30,1 33Lauren,50 34""" 35assert topScorer(data) == "David,Austin" 36 37data = """\ 38Ping-Ya,100,80,90 39""" 40assert topScorer(data) == "Ping-Ya" 41 42data = """\ 43Reyna,20,40,40 44Ema,5,5,5,5,10 45Ketandu,50,20,10,20 46Tanya,80,20 47Kate,70 48""" 49assert topScorer(data) == "Reyna,Ketandu,Tanya" 50 51assert topScorer("") == None 52print("Passed!")