Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON LAB: Will give a thumbs up PLEASE HELP! The only Classes that need to be edited to make the code run are Date.py, Person.py,

PYTHON LAB: Will give a thumbs up PLEASE HELP! The only Classes that need to be edited to make the code run are Date.py, Person.py, PersonList.py, sort_search_func.py as directed in the comments under the classes. Appendix of the code Structure is listed at the bottom of this. Here is the provided code for the assignment i need to fill in the code where comments instruct. There is a csv file as well it is called persons.csv the list is seperated by commas so it is a comma delimited list which will be called for the list and sorted. //comparable.py from abc import ABC, abstractmethod class Comparable(): """ This class uses a class variable to count the number of times the compare method is called. It is designed to be inherited from classes that implement the compare method, who want to keep a count of the number of compares performed in the program. The subclass should first call this base class compare method, and then do the comparison between itself and another object of its same type. """ __num_compares = 0 @abstractmethod def compare(self, object): Comparable.__num_compares += 1 @classmethod def get_num_compares(cls): return Comparable.__num_compares @classmethod def clear_compares(cls): Comparable.__num_compares = 0

//date.py

from comparable import Comparable class Date(Comparable): """  This class is immutable and inherits from Comparable. Please code this using private instance variables. Each instance variable should have a getter, but no setters. Code the compare method, but do not call the base class compare. Code a __str__ method. """ def __int__(self, year, month, day): self.year = year self.month = month self.day = day def get_month(self): def get_day(self): def get_year(self): def compare(self, object): def __str__(self): 

//person.py

from comparable import Comparable from date import Date class Person(Comparable): """ This class is immutable and inherits from Comparable. It uses composition by having a Date object for the birthday. Please code this using private instance variables. Each instance variable should have a getter, but no setters. Code the compare method, and call the base class compare at the top of the method: super().compare(other_person) Code a __str__ method. """ def __init__(self, name, year, month, day): self.name = name self.year = year self.month = month self.day = day def get_name(self): def get_birthday(self): def compare(self, object): def __str__(self): 

//person_main.py

from comparable import Comparable from personList import PersonList from person import Person from sort_search_funcs import * def main(): """ This main function creates a PersonList object and populates it with the contents of a data file containing 30 records each containing the first name and birthdate as month day year separated by commas: Merli,9,10,1998 Next, a request is made to search for a certain Person. A Person object is created from the input. Then a linear search is performed with the Person object If the Person is located in the list, its index is returned Otherwise, -1 is returned. The number of compares for the linear search is displayed Next, a bubble sort is executed to sort the PersonList. The sorted list is displayed, along with the number of compares it took to sort it Next, a request is made to search for another Person. A Person object is created from the input. Then a binary search is performed with the Person object If the Person is located in the sorted list, its index is returned Otherwise, -1 is returned. The number of compares for the binary search is displayed """ # Create a PersonList object and add the data p_list = PersonList() p_list.populate("persons.csv") # Request a person to search print("List searched using linear search") print() name = input("Who do you want to search for?") bdate = input("What is their birthdate? (mm-dd-yyyy)") print() # Create a person object from the input month, day, year = bdate.split('-') p_obj = Person(name, int(year), int(month), int(day)) # Do a linear search index = p_list.search(linear_search, p_obj) if index == -1: print(name, "is not in the list") else: print(name, "is at position", index, "in the list") # Display the number of compares print() print("The number of compares are", Comparable.get_num_compares()) print() # Reset the compare count Comparable.clear_compares() # Sort the list using bubble sort p_list.sort(bubble_sort) print("List sorted by bubble sort") # for p in p_list: # print(p) print(p_list) # Display the number of compares print() print("The number of compares are", Comparable.get_num_compares()) print() # Reset the compare count Comparable.clear_compares() # Request a person to search print("List searched using binary search") print() name = input("Who do you want to search for?") bdate = input("What is their birthdate? (mm-dd-yyyy)") print() # Create a person object from the input month, day, year = bdate.split('-') p_obj = Person(name, int(year), int(month), int(day)) # Do a binary search index = p_list.search(binary_search, p_obj) if index == -1: print(name, "is not in the list") else: print(name, "is at position", index, "in the list") # Display the number of compares print() print("The number of compares are", Comparable.get_num_compares()) main() 

//personList.py

import csv from person import Person from sortable import Sortable from searchable import Searchable class PersonList(Sortable, Searchable, list): """ This class has no instance variables. The list data is held in the parent list class object. The constructor must call the list constructor: See how this was done in the Tower class. Code a populate method, which reads the CSV file. It must use: try / except, csv.reader, and with open code constructs. Code the sort method: Must accept a function object and call the function. Code the search method: Must accept a function object and a search item Person object and call the function. Code a __str__ method: Look at the Tower class for help You may want to code a person_at method for debug purposes. This takes an index and returns the Person at that location. """ def __init__(self): super().__init__() def person_at(self): def populate(self): def sort(self, obj_list): def search(self, obj_list, object): def __str__(self):

//searchable.py

from abc import ABC, abstractmethod class Searchable(ABC): """ This class only has an abstract method: search. The search method takes a function object and a search object. It uses pass for its implementation. """ @abstractmethod def search(self, obj_list, object): pass 

//sort_search_funcs.py

def bubble_sort(obj_list): """ Code this function. The PersonList is the obj_list parameter. Nothing is returned. """ def binary_search(obj_list, obj):  """ Code this function. The PersonList is the obj_list parameter. A Person is the obj to be searched. Return an integer: the index of found object or -1. """ def linear_search(obj_list, obj): """ Code this function. The PersonList is the obj_list parameter A Person is the obj to be searched. Return an integer: the index of found object or -1. """ 

//sortable.py

from abc import ABC, abstractmethod class Sortable(ABC): """ This class only has an abstract method: sort The sort method takes a function object. It uses pass for its implementation """ @abstractmethod def sort(self, obj_list): pass 

Appendix:

Comparable:

-num_compares: int Class variable

+__init__()

+compare(obj: Comparable object): int

+get_num_compares(): int

+clear_compares():

Abstract Class: abstract method is compare

Sortable(ABC):

+sort(object)

Abstract Class: abstract method is sort

Searchable(ABC):

+search(object, search_obj: Comparable object)

Abstract Class: abstract method is search

PersonList(Sortable, Searchable, list)

+__init__()

+person_at(index: int): Person

+populate(filename: String)

+sort(func: func_object)

+search(func: func_object, person_obj: Person)

+__str__(): string

Subclass: inherits from list, Sortable, and Searchable

Person(Comparable)

-name: string

-birthday: Date

+__init__(name: string, year: int, month: int, day: int)

+get_name(): string

+get_birthday(): Date

+compare(other_person: Person): int

+__str__(): string

Subclass: inherits from Comparable

Date(Comparable)

-month: int

-day: int

-year: int

+__init__(year: int, month: int, day: int)

+get_month(): int

+get_day(): int

+get_year(): int

+compare(other_date: Date): int

+__str__(): string

Subclass: inherits from Comparable

sort_search_funcs module:

functions:

  • linear_search
  • binary_search
  • bubble_sort

main function:

  1. This main function creates a PersonList object and populates it with the contents of a data file containing 30 records each containing the first name and birthdate as month day year separated by commas: Merli, 9, 10, 1998
  2. Next, a request is made to search for a certain Person. A Person object is created from the input. Then a linear search is performed with the Person object. If the Person is located in the list, its index is returned. Otherwise, -1 is returned. The number of compares for the linear search is displaye
  3. Next, a bubble sort is executed to sort the PersonList. The sorted list is displayed, along with the number of compares it took to sort it.
  4. Next, a request is made to search for another Person. A Person object is created from the input. Then a binary search is performed with the Person object. If the Person is located in the sorted list, its index is returned. Otherwise, -1 is returned. The number of compares for the binary search is displayed.

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

Managing Your Information How To Design And Create A Textual Database On Your Microcomputer

Authors: Tenopir, Carol, Lundeen, Gerald

1st Edition

1555700233, 9781555700232

More Books

Students also viewed these Databases questions

Question

f. Did they change their names? For what reasons?

Answered: 1 week ago