From ddaef4333a3750d0b58d4dac2628bd059a6d6e33 Mon Sep 17 00:00:00 2001 From: lukovnikovde Date: Fri, 1 May 2026 15:22:46 +0000 Subject: [PATCH] [7] adding ht_find() and ht_list_all --- lukovnikovde/docs/data/DataStructure.py | 42 ++++++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/lukovnikovde/docs/data/DataStructure.py b/lukovnikovde/docs/data/DataStructure.py index 4a1d15b..f07d238 100644 --- a/lukovnikovde/docs/data/DataStructure.py +++ b/lukovnikovde/docs/data/DataStructure.py @@ -6,7 +6,7 @@ def sort_list(name_list): for i in range(l - 1): for j in range(l - i - 1): if name_list[j][0] > name_list[j + 1][0]: - name_list[j][0], name_list[j + 1][0] = name_list[j + 1][0], name_list[j][0] + name_list[j], name_list[j + 1] = name_list[j + 1], name_list[j] return name_list @@ -62,7 +62,7 @@ def ll_list_all(head): name_list = [] running = head while running is not None: - name_list.append([running['name'], running['phone']]) + name_list.append((running['name'], running['phone'])) running = running['next'] return name_list @@ -117,9 +117,27 @@ def ht_insert(buckest, name, phone): buckest[index].append((name, phone)) return buckest +def ht_find(buckest, name): + index = sum(ord(ch) for ch in name) % 10 + for (Name, Phone) in buckest[index]: + if Name == name: + return Phone + return None + +def ht_list_all(buckest): + + name_list = [] + + for index in range(len(buckest)): + for i, (name, phone) in enumerate(buckest[index]): + name_list.append((name, phone)) + + name_list = sort_list(name_list) + + return name_list #################################################################################################### -def HashTable(head): +def HashTable(buckest): print('=========== TESTING HT_INSERT =============') Name = ['Dima', 'Alex', 'Ivan', 'Maxim', 'Olga', 'Lena'] @@ -130,18 +148,26 @@ def HashTable(head): str(rnd.randint(0,9)) + str(rnd.randint(0,9)) + str(rnd.randint(0,9)) print(name, phone) - head = ht_insert(head, name, phone) - print(head) + buckest = ht_insert(buckest, name, phone) + print(buckest) print('-----------------------------------------------------\n') print('============= END TESTING =====================\n\n') -""" Name.append('Masha') + + print('============== TESTING HT_FIND =====================') + + Name.append('Masha') for i in range(len(Name)): name = Name[i] - print(name, ":", ht_find(head, name)) + print(name, ":", ht_find(buckest, name)) print("======== END TESTING =============\n\n") -""" + + print("================ TESTING TH_LIST_ALL ====================") + + print(*ht_list_all(buckest)) + + print() ################################################################################################# def main():