Answered step by step
Verified Expert Solution
Link Copied!

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 try/except and in
case of an error print a user-friendly message to the terminal and raise or re-raise 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: (full_name, [friend1,
friend2,....]), where each list element is a string and friend1, friend2 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
add_user
(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, i.e. if the user
existed before.
b) Write a function
add_friend
(sn, user1, user2) that takes a dictionary sn with a social network and
adds a mutual friend link between users user1 and user2. The function returns True if success, and
False otherwise, e.g. when at least one the user names given are not found in sn.
c) Write a function
get_friends
(sn, user1, distance) that takes a dictionary sn with a social network, a
username in argument user1, and a positive integer in argument distance and that returns a list with all
friends of user1 at link distance 1,2,..., distance.
Friends at distance=1 of a user are the users in its immediate list of friends:
get_friends(sn,alice,1) returns [maria]
Friends at distance=2 of a user are the users at distance 1 and the users in the immediate friend lists of
users at distance 1:
get_friends(sn,alice,2) returns [maria,joe,david]
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
save_network
(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, e.g. FileNotFoundError.
e) Write a function
load_network
(filename) that reads a social network from a .CSV file saved with
save_network(...) 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions