Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import random def create_network(file_name): '''(str)->list of tuples where each tuple has 2 elements the first is int and the second is list of int Precondition:

import random

def create_network(file_name): '''(str)->list of tuples where each tuple has 2 elements the first is int and the second is list of int

Precondition: file_name has data on social netowrk. In particular: The first line in the file contains the number of users in the social network Each line that follows has two numbers. The first is a user ID (int) in the social network, the second is the ID of his/her friend. The friendship is only listed once with the user ID always being smaller than friend ID. For example, if 7 and 50 are friends there is a line in the file with 7 50 entry, but there is line 50 7. There is no user without a friend Users sorted by ID, friends of each user are sorted by ID Returns the 2D list representing the frendship nework as described above where the network is sorted by the ID and each list of int (in a tuple) is sorted (i.e. each list of friens is sorted). ''' friends = open(file_name).read().splitlines() network=[]

### YOUR CODE GOES HERE### return network

you cannot user: dictionaries, sets, deque, bisect module. You can though, and in fact should, use .sort or sorted functions. Using global variables inside of functions is not allowed. In particular, inside of your functions you can only use variables that are created in that function.

The Python function needs to read a file and return a list of tuples representing the social network from the file. In particular the function returns a list of tuples where each tuple has 2 elements: the first is an integer representing an ID of a user and the second is the list of integers representing his/her friends. In addition the 2D-list for friendship network that must create_network function returns must be sorted by the ID and a list of friends in each tuple also must be sorted. So for the example above, this function should return the following 2D-list for 2D-list for friendship network: [(0, [1]), (1, [0,2,8]), (2,[1,3]), (3,[2]), (8,[1])]

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

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions