From 68fa9dfbee41f089bd60dee2ffae5a3980d447e5 Mon Sep 17 00:00:00 2001 From: gutovvm Date: Thu, 23 Apr 2026 10:12:16 +0300 Subject: [PATCH] [2] lab_1 ll and ht + restructured folders --- GutovVM/docs/data/1.py | 14 --- GutovVM/docs/data/lab_1_data/1.py | 165 ++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 14 deletions(-) delete mode 100644 GutovVM/docs/data/1.py create mode 100644 GutovVM/docs/data/lab_1_data/1.py diff --git a/GutovVM/docs/data/1.py b/GutovVM/docs/data/1.py deleted file mode 100644 index e856c93..0000000 --- a/GutovVM/docs/data/1.py +++ /dev/null @@ -1,14 +0,0 @@ -#LinkedListPhoneBook - -def insert(name, phone): - return 0 - -def find(name): - return 0 - -def delete(name): - return 0 - -def list_all(): - return 0 - diff --git a/GutovVM/docs/data/lab_1_data/1.py b/GutovVM/docs/data/lab_1_data/1.py new file mode 100644 index 0000000..51f8943 --- /dev/null +++ b/GutovVM/docs/data/lab_1_data/1.py @@ -0,0 +1,165 @@ +#LinkedListPhoneBook + +head = None #!!!!!!!!!!!!!! + +print("\nLinked list\n") + +def ll_insert(head, name, phone): + + curr = head + + while curr is not None: + if curr['name'] == name: + curr['phone'] = phone + return head + elif curr['next'] == None: + curr['next'] = {'name' : name, 'phone' : phone, 'next' : None} + return head + curr = curr['next'] + + return {'name' : name, 'phone' : phone, 'next' : None} + + +test_names = ['Andrey', 'Ivan', 'Andrey', 'Igor', 'Nagibator3000', 'Sberbank','Loshped'] +test_phones = ['7-234-246','6-352-095','5-257-098','1-374-098','9-387-098','5-135-357','0-000-000'] + +print("\nll_insert test\n") + +for i in range(len(test_names)): + head = ll_insert(head, test_names[i], test_phones[i]) + print(head) + +print("\ntest end\n\n") + +def ll_find(head, name): + + curr = head + while curr is not None: + if curr['name'] == name: + return curr['phone'] + curr = curr['next'] + + return None + +print('ll_find test\n') + +test_names = ["Ivan", "Andrey", "Sberbank", "Nagibator3000","Ermola"] + +for name in test_names: + print(ll_find(head, name)) + +print('\ntest_end\n\n') + +def ll_delete(head, name): + + if head is not None: + + if head['name'] == name: + return head['next'] + + old_curr = head + curr = head['next'] + + while curr is not None: + if curr['name'] == name: + old_curr['next'] = curr['next'] + break + old_curr, curr = curr, curr['next'] + + return head + +print('ll_delete test\n') + +test_names = ['Nagibator3000','Nagibator3000','Loshped','Ermola'] + +for name in test_names: + head = ll_delete(head, name) + print(head) + + +print('\ntest_end\n\n') + +def ll_list_all(head): + + res = [] + curr = head + + while curr is not None: + res += [(curr['name'],curr['phone'])] + curr = curr['next'] + + return sorted(res) + +print('ll_list_all test\n') + +print(ll_list_all(head)) + +print('\ntest_end\n\n') + + + +print("\nHash Table\n") + +size = 8 +buckets = [None] * size + +def index(name,size): + return hash(name) % size + +def ht_insert(buckets, name, phone): + ind = index(name, size) + buckets[ind] = ll_insert(buckets[ind], name, phone) + +def ht_find(buckets, name): + ind = index(name, size) + return ll_find(buckets[ind], name) + +def ht_delete(buckets, name): + ind = index(name, size) + buckets[ind] = ll_delete(buckets[ind], name) + +def ht_list_all(buckets): + res = [] + for head in buckets: + curr = head + while curr is not None: + res += [(curr['name'],curr['phone'])] + curr = curr['next'] + return sorted(res) + +test_names = ['Andrey', 'Ivan', 'Andrey', 'Igor', 'Nagibator3000', 'Sberbank','Loshped'] +test_phones = ['7-234-246','6-352-095','5-257-098','1-374-098','9-387-098','5-135-357','0-000-000'] + +print("\nht_insert test\n") + +print(buckets) +for i in range(len(test_names)): + ht_insert(buckets, test_names[i], test_phones[i]) + print(buckets) + +print("\ntest end\n\n") + +print("ht_find test\n") + +test_names = ["Ivan", "Andrey", "Sberbank", "Nagibator3000","Ermola"] + +for name in test_names: + print(ht_find(buckets, name)) + +print("\ntest end\n\n") + +print('ht_delete test\n') + +test_names = ['Nagibator3000','Nagibator3000','Loshped','Ermola'] + +for name in test_names: + ht_delete(buckets, name) + print(buckets) + +print('\ntest_end\n\n') + +print('ht_list_all test\n') + +print(ht_list_all(buckets)) + +print('\ntest_end\n\n')