-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW 7 HASH.py
116 lines (97 loc) · 3.37 KB
/
HW 7 HASH.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# use 8009 for hash size
hash = 8009
list = []
for i in range(hash):
list.append([])
ended = "no"
print("""
Welcome to My Homework 7 Hash Table Code.
*Now With An Improved Menu!
""")
while ended == "no":
# menu
print("")
choice = input("""
Please press 1 to add something to the Hash Table.
Please press 2 to print out the Hash Table.
Please press 3 to find something in the Hash Table.
Please press 4 to delete something from the Hash Table.
Please press 5 to end the program.
""")
print("")
# adding stuff into the Hash Table
if choice == "1":
worked = "no"
while worked == "no":
try:
userInput = int(input("please type in the student number you want to add: "))
worked = "yes"
except:
print("")
print("Invalid input returning to the previous section.")
print("")
worked = "no"
print("")
hashCode = userInput % hash
list[hashCode].append(userInput)
print(f"{userInput} successfully added into the Hash Table")
# printing the Hash Table
elif choice == "2":
print("Location| Student Number")
print("--------|---------------")
for i in range(hash):
print(f"{i} \t| {list[i]}")
print("")
# finding stuff in the Hash Table
elif choice == "3":
found = "no"
worked = "no"
while worked == "no":
try:
userInputFind = int(input("please type in the student number you want to find: "))
worked = "yes"
except:
print("")
print("Invalid input returning to the previous section.")
print("")
worked = "no"
hashCode = userInputFind % hash
print("")
for i in list[hashCode]:
if i == userInputFind:
print("FOUND IT!")
found = "yes"
if found == "no":
print("Sorry the number you are trying to reach has not been found.")
# deleting stuff from the Hash Table
elif choice == "4":
found2 = "no"
worked2 = "no"
while worked2 == "no":
try:
userInputDelete = int(input("please type in the student number you want to delete: "))
worked2 = "yes"
except:
print("")
print("Invalid input returning to the previous section.")
print("")
worked2 = "no"
hashCode = userInputDelete % hash
print("")
for i in list[hashCode]:
if i == userInputDelete:
list[hashCode].remove(i)
print(f"{userInputDelete} has been removed from the Hash Table")
found2 = "yes"
if found2 == "no":
print("The number you tried to delete wasn't in the Hash Table to begin with, did you delete it already?")
# ending the program
elif choice == "5":
ended = "yes"
print("Bye, have a great day!")
print("")
print("Also, please consider giving this program an A+")
print("")
# just in case the user types in the wrong input
else:
print("Invalid input returning to the previous section.")