Level 1 Users, follows and snapshots
Build SocialNetwork, a directed "who follows whom" graph, with snapshots that freeze its state.
add_user(user): raiseValueErrorif the user already exists.follow(follower, followee): raiseValueErrorif either user doesn't exist. Following yourself, or someone you already follow, does nothing.unfollow(follower, followee): raiseValueErrorif either user doesn't exist; does nothing if there's no such follow.is_following(follower, followee) -> bool: the current state;Falsefor unknown users.create_snapshot() -> Snapshot: a frozen copy of the graph right now.
Snapshot has is_following(follower, followee) -> bool (False for users it doesn't know). A snapshot never sees follows, unfollows or users that happen after it was created.
net = SocialNetwork()
for u in ("ann", "bob", "cy"):
net.add_user(u)
net.follow("ann", "bob")
snap = net.create_snapshot()
net.follow("ann", "cy")
net.unfollow("ann", "bob")
snap.is_following("ann", "bob") # True
snap.is_following("ann", "cy") # False
net.is_following("ann", "bob") # False
Show hint
Watch out for storing a reference to the live data instead of a copy: a shallow dict.copy() still shares the inner sets.