Answered step by step
Verified Expert Solution
Question
1 Approved Answer
code in python . For the following, all functions must have a proper docstring and annotations. Use try / except and in case of an
code in python For the following, all functions must have a proper docstring and annotations. Use tryexcept and in
case of an error print a userfriendly message to the terminal and raise or reraise the exception.
Write a simplified social network data structure using dictionaries, lists and tuples.
The social network is stored in a Python dictionary with information for each social network user
indexed by the username. A
user
is represented by a tuple with this format: fullname, friend
friend where each list element is a string and friend friend are usernames of the current
users friends
An example of a social network showing usernames and the
friend
links is this:
alice maria joe eve
david
This network can be represented by this dictionary:
alice: Alice Smith', maria
'maria': Maria Cortez', alice 'joe', 'david'
'joe': Joseph Adams', maria 'eve'
'eve': Evelyn Cooper', joe
'david': David Benson', maria
a Write a function
adduser
sn username, fullname that adds to the social network stored in
dictionary sn a new user with the given username and full name. The new user has initially no friend
links. The function returns True if the user was added successfully and False, otherwise, ie if the user
existed before.
b Write a function
addfriend
sn user user that takes a dictionary sn with a social network and
adds a mutual friend link between users user and user The function returns True if success, and
False otherwise, eg when at least one the user names given are not found in sn
c Write a function
getfriends
sn user distance that takes a dictionary sn with a social network, a
username in argument user and a positive integer in argument distance and that returns a list with all
friends of user at link distance distance.
Friends at distance of a user are the users in its immediate list of friends:
getfriendssnalice returns maria
Friends at distance of a user are the users at distance and the users in the immediate friend lists of
users at distance :
getfriendssnalice returns mariajoedavid
The function returns the empty list if the are no friends to return or if the given user name is wrong.
Hint: you need to avoid cycles.
d Write a function
savenetwork
filename sn that saves a social network dictionary to a CSV file.
The function takes as parameter the file name and the dictionary.
Let your function throw relevant
exceptions, eg FileNotFoundError.
e Write a function
loadnetwork
filename that reads a social network from a CSV file saved with
savenetwork and that returns the dictionary object. Let your function throw relevant exceptions.
f Write a
main
function that tests all the functions above.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started