University of Arizona, Department of Computer Science

CSc 120: Friends

The primary purpose of this problem is to continue working with linked lists.

Restrictions

  1. For the long problems, your code should follow the style guidelines for the class.
  2. You may not use concepts and/or short-hand syntax not yet covered in class. The restrictions include the following:

Background

Social media sites often look at what you have in common with your friends (or followers, or contacts) to make suggestions. As an example, Facebook and LinkedIn will point out "mutual friends", i.e., people whose friends have one or more people in common with yours. This problem involves implementing a simple form of an algorithm for identifying such mutual friends.

Expected Behavior

Write a Python program, in two files linked_list.py and friends.py, that behaves as specified below. The file linked_list.py should contain code implementing the LinkedList and associated classes while friends.py should contain code that uses these classes to implement the functionality required of this program. At the top your friends.py file, include the following statement so that you are able to use the classes in your linked list file:
from linked_list import *

Your program should behave as follows.

  1. Read in the name of an input file using input('Input file: '). Read in the contents of this file into your program.

  2. The data in the file specifies friendship relationships between pairs of names. Organize the data into the linked list structure specified under Data structures below.

    NOTE: Friendships are two-way relations (in mathematics, such relations are called symmetric). This means that for each input line of the form

    X   Y
    you should set up your data structures to indicate two things:

  3. Read in two names using the function calls below. You can use other variable names or your choice to store the values read in. Strip off whitespace as necessary.
    name1 = input('Name 1: ')
    name2 = input('Name 2: ')

  4. If there are friends in common, first print the following line:
        Friends in common: 
    Then print out the name of each mutual friend of name1 and name2, i.e., any name3 such that name3 is a friend of both name1 and name2 (see Output format below). Names must be printed in alphabetical order.

    If there are no friends in common, no further output is produced. (That is, if there are no friends in common, the program only produces the input prompts.)

Input format

Each line in the input file consists of two strings separated by whitespace (blanks and tabs). E.g.: the file in04.txt is:
William Ethan
Rebecca Lindsey
William Lindsey
William Patrick

You should not make assumptions about the amount of whitespace around and between names.

Output format

If there are friends in common, first print the following line:
    Friends in common: 
Then print out the name of each mutual friend in alphabetical order.

If there are no friends in common, no further output is produced. (That is, if there are no friends in common, the program only produces the input prompts and does NOT print Friends in common:. )

Data Structures

The names read in should be organized as a linked list with a node per name. Each node should have the following attributes:

For example, for the file in04.txt mentioned above (under Input format), the data structure should be as shown below:

friends-data-structure.png
(click on the image for a larger picture)

Programming Requirements

As you read in a line of the friendship data from the input file, you will strip and split the line. This is the only time that you can use a Python list in your solution. For each name, you must determine if the name is already in the "main" linked list of names and add it if not (as a node). In addition, add the name (as a node) to its friends linked list.

Your program must use linked lists to implement all of the following tasks:

  1. the names read in from the input file (which go into the "main" linked list)
  2. for each such name, the list of that person's friends
  3. the friends in common between two people
  4. sorting the common friends alphabetically for printing

Your code should be organized among your files as follows:

The methods defined in linked_list.py can be accessed in friends.py using an import statement in the latter file.

Errors

You must check for the following error:

Examples

Some examples are shown here.