Question
Part 1: The Birthday Class The key for our hash table will be Birthday objects. Create a class called Birthday that has the following: Three
Part 1: The Birthday Class
The key for our hash table will be Birthday objects. Create a class called Birthday that has the following:
- Three attributes for birth day, birth month, birth year
- A constructor method that accepts the day, month, and year as parameters.
- A __str__ method to provide a string representation of the Birthday object.
- A __hash__ method that returns an integer as the sum of of the day, month, and year, mod 12. So if day=1, month=11, year = 1990, then this method would return (1+11+1990)%12, which is 10.
- An __eq__ method to test if two Birthday objects have the same attribute value.
Place your implementation in a module called Birthday.py and create a __main__ section where you test instantiating Birthday objects, printing them, and calling the __hash__ method.
Part 2: Main Application
Create a lab8.py usage file. In this file you will:
- Create an empty hash table
- Read in a list of birthdays from the supplied bdaylist.txt file.
- For each birthday, create a Birthday object, and add the tuple (Birthday,i) to the appropriate list in the hash table, where i is the line number from the input file.
- Output the total length of the list at each of the hash locations.
Here's a few things to help you get started:
- Since our Birthday object's hash function hashes to integers in the range [0,12), we need to create a hash table with 12 empty lists in it. Here's some code to do this:
hashtable = []
for i in range(12):
hashtable.append([])
- Recall how easy reading all the lines from a file is in Python!
- Open the file, via the open function.
- Read in the lines via the readlines() method to a list
- Iterate over this list
- If an object implements the __hash__ method (as you were asked to do for the Birthday class), then you can call the hash(obj) function on the object to get the hash location (it basically calls obj.__hash__())
Sample Output
Hash location 0 has 10 elements in it Hash location 1 has 12 elements in it Hash location 2 has 8 elements in it Hash location 3 has 10 elements in it Hash location 4 has 5 elements in it Hash location 5 has 7 elements in it Hash location 6 has 7 elements in it Hash location 7 has 11 elements in it Hash location 8 has 4 elements in it Hash location 9 has 8 elements in it Hash location 10 has 8 elements in it Hash location 11 has 10 elements in it
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started