diff --git a/GutovVM/docs/data/lab_1_data/1.py b/GutovVM/docs/data/lab_1_data/1.py index 51f8943..216e4da 100644 --- a/GutovVM/docs/data/lab_1_data/1.py +++ b/GutovVM/docs/data/lab_1_data/1.py @@ -98,6 +98,8 @@ print('\ntest_end\n\n') +#HashTablePhoneBook + print("\nHash Table\n") size = 8 @@ -109,6 +111,7 @@ def index(name,size): def ht_insert(buckets, name, phone): ind = index(name, size) buckets[ind] = ll_insert(buckets[ind], name, phone) + return buckets def ht_find(buckets, name): ind = index(name, size) @@ -117,6 +120,7 @@ def ht_find(buckets, name): def ht_delete(buckets, name): ind = index(name, size) buckets[ind] = ll_delete(buckets[ind], name) + return buckets def ht_list_all(buckets): res = [] @@ -134,7 +138,7 @@ print("\nht_insert test\n") print(buckets) for i in range(len(test_names)): - ht_insert(buckets, test_names[i], test_phones[i]) + buckets = ht_insert(buckets, test_names[i], test_phones[i]) print(buckets) print("\ntest end\n\n") @@ -153,7 +157,7 @@ print('ht_delete test\n') test_names = ['Nagibator3000','Nagibator3000','Loshped','Ermola'] for name in test_names: - ht_delete(buckets, name) + buckets = ht_delete(buckets, name) print(buckets) print('\ntest_end\n\n') @@ -163,3 +167,129 @@ print('ht_list_all test\n') print(ht_list_all(buckets)) print('\ntest_end\n\n') + + + +#BinarySearchTree + +root = None + +def bst_insert(root, name, phone): + + if root == None: + return {'name': name, 'phone': phone, 'left': None, 'right': None} + + elif name == root['name']: + root['phone'] = phone + return root + elif hash(name) < hash(root['name']): + root['left'] = bst_insert(root['left'], name, phone) + return root + else: + root['right'] = bst_insert(root['right'], name, phone) + return root + + +def bst_find(root, name): + + if root == None: + return None + + elif name == root['name']: + return root['phone'] + + elif hash(name) < hash(root['name']): + return bst_find(root['left'],name) + else: + return bst_find(root['right'],name) + + +def bst_delete(root, name): + + if root is None: + return None + elif name == root['name']: + curr = root['left'] + if curr is not None: + oldcurr = root + while curr['right'] is not None: + oldcurr,curr = curr,curr['right'] + + if oldcurr == root: + root['left'] = curr['left'] + + else: + oldcurr['right'] = curr['right'] + + curr['left'],curr['right'] = root['left'],root['right'] + return curr + + curr = root['right'] + if curr is not None: + oldcurr = root + while curr['left'] is not None: + oldcurr,curr = curr,curr['left'] + + if oldcurr == root: + root['right'] = curr['right'] + + else: + oldcurr['left'] = curr['left'] + + curr['left'],curr['right'] = root['left'],root['right'] + return curr + + return None + + elif hash(name) < hash(root['name']): + root['left'] = bst_delete(root['left'],name) + return root + else: + root['right'] = bst_delete(root['right'],name) + return root + + +def bst_list_all(root): + + if root is None: + return [] + + return bst_list_all(root['left']) + [(root['name'],root['phone'])] + bst_list_all(root['right']) + + +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("\nbst_insert test\n") + +print(root) +for i in range(len(test_names)): + root = bst_insert(root, test_names[i], test_phones[i]) + print(root) + +print("\ntest end\n\n") + +print("bst_find test\n") + +test_names = ["Ivan", "Andrey", "Sberbank", "Nagibator3000","Ermola"] + +for name in test_names: + print(bst_find(root, name)) + +print("\ntest end\n\n") + +print('bst_delete test\n') + +test_names = ['Nagibator3000','Nagibator3000','Andrey','Ermola'] + +for name in test_names: + root = bst_delete(root, name) + print(root) + +print('\ntest_end\n\n') + +print('bst_list_all test\n') + +print(bst_list_all(root)) + +print('\ntest_end\n\n') \ No newline at end of file diff --git a/GutovVM/docs/data/lab_1_data/1backup23041020.py b/GutovVM/docs/data/lab_1_data/1backup23041020.py new file mode 100644 index 0000000..51f8943 --- /dev/null +++ b/GutovVM/docs/data/lab_1_data/1backup23041020.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')