Question

In: Computer Science

Please include comments on what you are doing.   Using linked lists, write a Python program that...

Please include comments on what you are doing.  

Using linked lists, write a Python program that performs the following tasks:

  • store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields)
  • allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database
  • allow the user to enter a state's name and display (to the screen) the total count of colleges in that state

If you use the Node and Linked List class definitions from the zyBook, please make sure you cite this reference in your comments.(dont use if not needed but if you need to use this I will reference them myself)

college.csv file sample

Alabama A & M University AL
University of Alabama at Birmingham AL
Amridge University AL
University of Alabama in Huntsville AL
Alabama State University AL
The University of Alabama AL
Central Alabama Community College AL
Athens State University AL
Auburn University at Montgomery AL
Auburn University AL
Birmingham Southern College AL
Chattahoochee Valley Community College AL
Concordia College Alabama AL
South University-Montgomery AL
Enterprise State Community College AL
Coastal Alabama Community College AL
Faulkner University AL
Gadsden State Community College AL
New Beginning College of Cosmetology AL
George C Wallace Community College-Dothan AL
George C Wallace State Community College-Hanceville AL
George C Wallace State Community College-Selma AL
Herzing University-Birmingham AL
Huntingdon College AL
Heritage Christian University AL
J. F. Drake State Community and Technical College AL
J F Ingram State Technical College AL
Jacksonville State University AL
Jefferson Davis Community College AL
Jefferson State Community College AL
John C Calhoun State Community College AL
Judson College AL
Lawson State Community College-Birmingham Campus AL
University of West Alabama AL
Lurleen B Wallace Community College AL
Marion Military Institute AL
Miles College AL
University of Mobile AL
University of Montevallo AL

Solutions

Expert Solution

Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:


Below is the code to copy:

#CODE STARTS HERE----------------
class Node: #Node class
   def __init__(self, college,state):
      self.college = college #college of the node
      self.state = state #state of the node
      self.next = None #Next node in the linked list

def insert(head, college, state):
   new = Node(college,state) #Create a new node with given values
   if head is None: #if head is none, new node becomes the head
      new.next = head
      return new

   #Traverse the list until last node is reached
   current_node = head
   while current_node.next is not None:
      current_node = current_node.next

   #Add new nodes to the end of the list
   new.next = current_node.next #Add 'next' of new node
   current_node.next = new #Update 'next' of current node
   return head

def search_college(head, name): #Search if a college is in the linked list or not
   pointer = head #Pointer is used to hold current position while traversing
   while pointer is not None: #Traverse linked list from the head
      #Check if entered name is in the node's college value
      if name.strip().lower() == pointer.college.strip().lower():
         return True #Return true if present
      pointer = pointer.next #Update pointer
   return False

def search_in_state(head, state_name): #Return the number of colleges in the state
   counter =0
   pointer = head #Pointer is used to hold current position while traversing
   while pointer is not None: #Traverse linked list from the head
      #Check if state entered is equal to node's state
      if state_name.strip() == pointer.state.strip():
         counter+=1 #update counter value
      pointer = pointer.next
   return counter #return count

def main():
   #Inserting values into linked list from .csv
   head = None #Initialize node to None
   with open("colleges.csv") as f:  #open file
      for line in f.readlines():#traverse .csv line by line
         #call insert function with college name and state name
         head = insert(head , " ".join(line.split()[0:-1]),line.split()[-1])

   #Searching college name
   name = input("Please enter a college name to search: ")#Take input
   ret_college = search_college(head, name) #Call function
   if ret_college is True: #print result
      print("The college is in the database!")
   else:
      print("The college is not in the database!")

   #searching number of colleges in a state
   state_name = input("\nPlease enter state: ")#get state input
   count = search_in_state(head, state_name) #Function call
   print("Number of colleges in",state_name,"=",count) #print result

main()
#CODE ENDS HERE-----------------

Related Solutions

Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
Overview: You will write a python program that will work with lists and their methods allowing...
Overview: You will write a python program that will work with lists and their methods allowing you to manipulate the data in the list. Additionally you will get to work with randomization. Instructions: Please carefully read the Instructions and Grading Criteria. Write a python program that will use looping constructs as noted in the assignment requirements. Name your program yourlastname_asgn5.py Assignment Requirements IMPORTANT: Follow the instructions below explicitly. Your program must be structured as I describe below. Write a Python...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
WRITE IN JAVA, please include comments and explain what you did A Book has such properties...
WRITE IN JAVA, please include comments and explain what you did A Book has such properties as title, author, and numberOfPages. A Volume will have properties such as volumeName, numberOfBooks, and an array of book objects (Book [ ]). You are required to develop the Book and Volume classes, then write an application (DemoVolume) to test your classes. The directions below will give assistance. Create a class called Book with the following properties using appropriate data types: Title, Author, numberOfPages,...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Python Program : simplist form and include comments Shopping Data by Zip Code Have you ever...
Python Program : simplist form and include comments Shopping Data by Zip Code Have you ever noticed that some stores ask you for your zip code before “ringing up” your transaction? Suppose you have been given three (3) text files (named kroger.txt, publix.txt, and ingles.txt) from popular grocery stores, containing zip codes of customers at each store. Additionally, you have been given a file named zipDirectory.txt which contains the zip code and city name (each on its own line). This...
PLEASE INCLUDE #FOR EACH LINE EXPLANATION Write a PYTHON PROGRAM that prompts for an integer and...
PLEASE INCLUDE #FOR EACH LINE EXPLANATION Write a PYTHON PROGRAM that prompts for an integer and prints the integer, but if something other than an integer is input, the program keeps asking for an integer. Here is a sample session: Input an integer: abc Error: try again. Input an integer: 4a Error: try again. Input an integer: 2.5 Error: try again. Input an integer: 123 The integer is: 123 Hint: the string isdigit method will be useful to solve this...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT