Code initial de l’exercice 2
Code:
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}
add_friends("Alex", "Victor")
add_friends("Alex", "Emelyne")
add_friends("Alex", "Emelyne")
add_friends("Emelyne", "Rose")
# Tests pour (b)
# print(friends_of("Emelyne")) # {'Rose', 'Alex'}
# print(friends_of("Rachel")) # set()
# Tests pour (c)
# 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
# Tests pour (d)
# print(f"Consistent before modification? {is_data_consistent()}") # True
# friendships["Alex"].add("Rachel") # Modification asymétrique
# print(f"Consistent after modification? {is_data_consistent()}") # False
# 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}
add_friends("Alex", "Victor")
add_friends("Alex", "Emelyne")
add_friends("Alex", "Emelyne")
add_friends("Emelyne", "Rose")
# Tests pour (b)
# print(friends_of("Emelyne")) # {'Rose', 'Alex'}
# print(friends_of("Rachel")) # set()
# Tests pour (c)
# 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
# Tests pour (d)
# print(f"Consistent before modification? {is_data_consistent()}") # True
# friendships["Alex"].add("Rachel") # Modification asymétrique
# print(f"Consistent after modification? {is_data_consistent()}") # False
Modifié le: mardi, 25 octobre 2022, 18:14