This is the next big thing. We promise!
For our social network, we are going to need a Panda class which behaves like that:
ivo = Panda("Ivo", "[email protected]", "male")
ivo.name() == "Ivo" # True
ivo.email() == "[email protected]" # True
ivo.gender() == "male" # True
ivo.isMale() == True # True
ivo.isFemale() == False # TrueThe Panda class also should be possible to:
- Be turned into a string
- Be hashed and used as a key in a dictionary (
__eq__and__hash__) - Make sure that the
emailis a valid email!
Two Panda instances are equal if they have matching name, email and gender attributes.
Now it is time for our social network!
Implement a class, called PandaSocialNetwork, which has the following public methods:
add_panda(panda)- this method adds apandato the social network. The panda has 0 friends for now. If the panda is already in the network, raise anPandaAlreadyThereerror.has_panda(panda)- returnsTrueorFalseif thepandais in the network or not.make_friends(panda1, panda2)- makes the two pandas friends. RaisePandasAlreadyFriendsif they are already friends. The friendship is two-ways -panda1is a friend withpanda2andpanda2is a friend withpanda1. Ifpanda1orpanda2are not members of the network, add them!are_friends(panda1, panda2)- returnsTrueif the pandas are friends. Otherwise,Falsefriends_of(panda)- returns a list ofPandawith the friends of the givenpanda. ReturnsFalseif thepandais not a member of the network.connection_level(panda1, panda2)- returns the connection level betweenpanda1andpanda2. If they are friends, the level is 1. Otherwise, count the number of friends you need to go through frompandain order to get topanda2. If they are not connected at all, return-1! ReturnFalseif one of the pandas are not member of the network.are_connected(panda1, panda2)- returnTrueif the pandas are connected somehow, between friends, orFalseotherwise.how_many_gender_in_network(level, panda, gender)- returns the number ofgenderpandas (male of female) that in thepandanetwork in that manylevels deep. Iflevel == 2, we will have to look in all friends ofpandaand all of their friends too. And count
network = PandaSocialNetwork()
ivo = Panda("Ivo", "[email protected]", "male")
rado = Panda("Rado", "[email protected]", "male")
tony = Panda("Tony", "[email protected]", "female")
for panda in [ivo, rado, tony]:
network.add(panda)
network.make_friends(ivo, rado)
network.make_friends(rado, tony)
network.connection_level(ivo, rado) == 1 # True
network.connection_level(ivo, tony) == 2 # True
network.how_many_gender_in_network(1, rado, "female") == 1 # TrueThe next thing our social network is going to do is saving to your hard drive.
Write a function that saves the hole social network to a file. The format ot that file is at your choice. You have to be able to load it next time. So all the data in the network must be written down to that file.
Write a function that loads the hole social network from a file. All the pandas and all the relations.