forked from UNN/2026-rff_mp
[2] lab_1 ll and ht + restructured folders
This commit is contained in:
parent
74b9fc8790
commit
68fa9dfbee
|
|
@ -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
|
||||
|
||||
165
GutovVM/docs/data/lab_1_data/1.py
Normal file
165
GutovVM/docs/data/lab_1_data/1.py
Normal 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')
|
||||
Loading…
Reference in New Issue
Block a user