Information, calcul, communication

CS-119(k)

ICC-P Corrigé 6

This page is part of the content downloaded from ICC-P Corrigé 6 on Sunday, 29 June 2025, 20:43. Note that some content and any files larger than 50 MB are not downloaded.

Page content

Exercice 1:
from typing import * # MyLittleFacebook friendships: dict[str, set[str]] = {} def add_friends(name1: str, name2: str) -> None: if name1 in friendships: friendships[name1].add(name2) else: friendships[name1] = {name2} if name2 in friendships: friendships[name2].add(name1) else: friendships[name2] = {name1} print(friendships) add_friends("Alex", "Victor") print(friendships) add_friends("Alex", "Emelyne") print(friendships) add_friends("Alex", "Emelyne") print(friendships) add_friends("Emelyne", "Rose") print(friendships) def known_people() -> List[str]: return sorted(friendships.keys()) print(known_people()) def friends_of1(name: str) -> set[str]: if name in friendships: return friendships[name] else: return set() def friends_of2(name: str) -> set[str]: return friendships.get(name) or set() friends_of = friends_of2 print(friends_of("Emelyne")) # {'Rose', 'Alex'} print(friends_of("Rachel")) # set() def are_friends(name1: str, name2: str) -> bool: return name1 in friendships and name2 in friendships[name1] print(are_friends("Alex", "Victor")) # True print(are_friends("Victor", "Alex")) # True print(are_friends("Alex", "Rose")) # False print(are_friends("Alex", "Rachel")) # False print(are_friends("Rachel", "Alex")) # False def is_data_consistent() -> bool: for name1, friends in friendships.items(): for name2 in friends: if name2 not in friendships or name1 not in friendships[name2]: return False return True print(f"Consistent? {is_data_consistent()}") # True friendships["Alex"].add("Rachel") # Modification asymétrique print(f"Consistent? {is_data_consistent()}") # False

Exercice 2:
from dataclasses import dataclass import math @dataclass class Parallelogram: a: float b: float theta: float def perimeter(self) -> float: return 2 * (self.a + self.b) def height(self) -> float: return math.sin(self.theta / 180 * math.pi) * self.b def area(self) -> float: return self.a * self.height() p = Parallelogram(a=20, b=10, theta=45) print(p) print(p.perimeter()) print(p.height()) print(p.area())

Exercice 3:
from oilspills import all_events print(f"Il y a {len(all_events)} événements.") for event in all_events: print(event)

Exercice 4:
from oilspills import all_events, OilSpillEvent def print_statistics(events: list[OilSpillEvent]) -> None: sum_min = 0 sum_max = 0 date_min = events[0].date date_max = events[0].date for event in events: sum_min += event.min_tonnage sum_max += event.max_tonnage if event.date < date_min: date_min = event.date if event.date > date_max: date_max = event.date formatted_date_min = date_min.strftime("%d.%m.%Y") formatted_date_max = date_max.strftime("%d.%m.%Y") print(f"Entre le {formatted_date_min} et le {formatted_date_max}, {len(events)} événements ont causé le déversement d’entre {sum_min} et {sum_max} barils de brut.") print_statistics(all_events) def filter_by_country(country: str, events: list[OilSpillEvent]) -> list[OilSpillEvent]: filtered_events: list[OilSpillEvent] = [] for event in events: if event.country == country: filtered_events.append(event) return filtered_events print_statistics(filter_by_country("United States", all_events)) print_statistics(filter_by_country("Australia", all_events))