friends

.py

School

University Of Arizona *

*We aren’t endorsed by this school

Course

120

Subject

Computer Science

Date

Apr 3, 2024

Type

py

Pages

2

Uploaded by MateClover22302

Report
''' File: friends.py Author: Nick Brobeck Course: CSC 120, Spring Semester Purpose: Handles social network operations. It reads network data, constructs a network using linked lists, and finds mutual friends. It's all about managing social connections. ''' from linked_list import * def build_network(filename): """ Constructs a social network from data stored in a file. Parameters: filename: A string representing the name of the file containing the network data. Returns: LinkedList: A linked list representing the social network, where each node corresponds to a person. """ network = LinkedList() file = open(filename, 'r') for line in file: names = line.strip().split() # Extract the two names from the line. name1, name2 = names[0], names[1] # Ensure both individuals are in the network; # add them if they're not already present. node_1 = network.find(name1) if not node_1: network.add(name1) node_1 = network.find(name1) node_2 = network.find(name2) if not node_2: network.add(name2) node_2 = network.find(name2) # Create a friendship between the two nodes. add_friendship(node_1, node_2) file.close() return network def find_mutual_friends(network, name1, name2): """ Finds mutual friends between two people in the network. Parameters: network: A linked list representing the social network. name1: String representing the name of the first person. name2: String representing the name of the second person. """ # Find the nodes in the network corresponding to the given names.
node_1 = network.find(name1) node_2 = network.find(name2) # If either person is not found in the network, # print an error message and return. if not node_1 or not node_2: if not node_1: print("ERROR: Unknown person", name1) if not node_2: print("ERROR: Unknown person", name2) return # Use set intersection to find mutual friends. common_friends = set(node_1.friends) & set(node_2.friends) if common_friends: print("Friends in common:") # Sort the Friends in common before printing. for friend in sorted(common_friends): print(friend) def main(): input_file = input("Input file: ") name1 = input("Name 1: ") name2 = input("Name 2: ") network = build_network(input_file) find_mutual_friends(network, name1, name2) main()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help