Compare commits

...

4 Commits

Author SHA1 Message Date
9ab957e484 [3] lab_1 + bst functions 2026-04-23 14:46:35 +03:00
68fa9dfbee [2] lab_1 ll and ht + restructured folders 2026-04-23 10:12:16 +03:00
74b9fc8790 [1] 1st lab data create 2026-04-19 16:27:02 +03:00
036c08c2d9 [0] initial commit 2026-02-28 08:25:52 +03:00
3 changed files with 460 additions and 0 deletions

0
GutovVM/428b.md Normal file
View File

View File

@ -0,0 +1,295 @@
#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')
#HashTablePhoneBook
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)
return buckets
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)
return buckets
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)):
buckets = 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:
buckets = 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')
#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')

View File

@ -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')