Skip to content

Instantly share code, notes, and snippets.

@PatWg
Created November 27, 2023 22:40
Show Gist options
  • Save PatWg/05a476f8efd6323afc94f6e7808d9d53 to your computer and use it in GitHub Desktop.
Save PatWg/05a476f8efd6323afc94f6e7808d9d53 to your computer and use it in GitHub Desktop.
ICC23-24: Correction de la série 11
from dataclasses import dataclass
from typing import List, Tuple, Dict
@dataclass
class DataEntry:
gender: str
name: str
birth_year: int
count: int
def most_popular_name(entries: List[DataEntry]) -> Tuple[str, int]:
name: str = ""
count: int = 0
for entry in entries:
if entry.birth_year == 1983 and entry.count > count:
name = entry.name
count = entry.count
return (name, count)
def most_popular_name_in_year(entries: List[DataEntry], year: int) -> Tuple[str, int]:
name: str = ""
count: int = 0
for entry in entries:
if entry.birth_year == year and entry.count > count:
name = entry.name
count = entry.count
return (name, count)
def find_camilles(boys: List[DataEntry], girls: List[DataEntry]) -> int:
boys_camille: Dict[int, int] = {boy.birth_year: boy.count for boy in boys if boy.name == 'CAMILLE'}
girls_camille: Dict[int, int] = {girl.birth_year: girl.count for girl in girls if girl.name == 'CAMILLE'}
for birth_year in sorted(boys_camille.keys()):
if birth_year in girls_camille.keys():
if boys_camille[birth_year] < girls_camille[birth_year]:
return birth_year
def most_birth(entries: List[DataEntry]) -> Tuple[int, int]:
births_in_year: Dict[int, int] = {}
for entry in entries:
if entry.birth_year not in births_in_year.keys():
births_in_year[entry.birth_year] = entry.count
else:
births_in_year[entry.birth_year] += entry.count
year: int = 0
count: int = 0
for key in births_in_year.keys():
if births_in_year[key] > count:
count = births_in_year[key]
year = key
return (year, count)
boys: List[DataEntry] = []
girls: List[DataEntry] = []
with open('nat2018.csv', 'r', encoding='utf=8') as f:
lines = [line for line in f.read().splitlines() if 'XXXX' not in line and '_PRENOMS_RARES' not in line]
for line in lines[1:]:
gender, name, birth_year, count = line.split(';')
if gender == '1':
boys.append(DataEntry('M', name, int(birth_year), int(count)))
else:
girls.append(DataEntry('F', name, int(birth_year), int(count)))
print(most_popular_name(boys))
print(most_popular_name(girls))
print(most_popular_name_in_year(boys, 1990))
print(most_popular_name_in_year(girls, 1990))
print(find_camilles(boys, girls))
print(most_birth(boys + girls))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment