forked from UNN/2026-rff_mp
Merge pull request '[1] FINISH 1-st-exercise' (#148) from IvanBud123/2026-rff_mp:1-st-exercise into develop
Reviewed-on: UNN/2026-rff_mp#148
This commit is contained in:
commit
f5b0fec46f
457
BudakovIS/docs/data/1-st-exercize/LinkedListPhoneBook.py
Normal file
457
BudakovIS/docs/data/1-st-exercize/LinkedListPhoneBook.py
Normal file
|
|
@ -0,0 +1,457 @@
|
|||
head = None
|
||||
|
||||
#node1 = {'name' : 'Ivan', 'phone' : '123-456', 'next' : None}
|
||||
#head = node1
|
||||
|
||||
#node2 = {'name' : 'Dima', 'phone' : '789-123', 'next' : None}
|
||||
#node1['next'] = node2
|
||||
|
||||
def ll_insert(head, name, phone):
|
||||
|
||||
curent = head
|
||||
while curent is not None:
|
||||
if curent['name'] == name:
|
||||
curent['phone'] = phone
|
||||
return head
|
||||
curent = curent['next']
|
||||
|
||||
|
||||
n_node = {'name' : name, 'phone' : phone, 'next' : None}
|
||||
|
||||
if head is None:
|
||||
return n_node
|
||||
|
||||
curent = head
|
||||
while curent['next'] is not None:
|
||||
curent = curent['next']
|
||||
curent['next'] = n_node
|
||||
return head
|
||||
|
||||
|
||||
|
||||
print("====== TESTING ll_insert FUNC ========")
|
||||
head = ll_insert(head,'Ivan','123-456')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Boris', '123-456')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Ivan', '321-654')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Dima', '345-678')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Boris', '111-222')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Methody', '221-112')
|
||||
|
||||
head = ll_insert(head, 'Kiril', '112-221')
|
||||
|
||||
print(f"======= END TEST =======\n\n\n")
|
||||
|
||||
|
||||
def ll_find(head, name):
|
||||
curent = head
|
||||
while curent is not None:
|
||||
if curent['name'] == name:
|
||||
return curent['phone']
|
||||
curent = curent['next']
|
||||
return None
|
||||
|
||||
print("====== TESTING ll_find FUNC ======")
|
||||
|
||||
print("Ivan`s phone: "+ ll_find(head, 'Ivan'))
|
||||
|
||||
print("Dima`s phone: "+ ll_find(head, 'Dima'))
|
||||
|
||||
print("Boris phone: "+ ll_find(head, 'Boris'))
|
||||
|
||||
print(f"====== END TEST ======\n\n\n")
|
||||
|
||||
|
||||
def ll_delete(head, name):
|
||||
if head is None:
|
||||
return None
|
||||
|
||||
if head['name'] == name:
|
||||
return head['next']
|
||||
|
||||
prev = head
|
||||
curent = head['next']
|
||||
while curent is not None:
|
||||
if curent['name'] == name:
|
||||
prev['next'] = curent['next']
|
||||
return head
|
||||
prev = curent
|
||||
curent = curent['next']
|
||||
return head
|
||||
|
||||
|
||||
print("====== TEST ll_delete FUNC ======")
|
||||
|
||||
print("Del of Dima:", ll_delete(head, 'Dima'))
|
||||
|
||||
print("====== END TEST ======")
|
||||
|
||||
|
||||
def ll_list_all(head):
|
||||
records = []
|
||||
curent = head
|
||||
while curent is not None:
|
||||
records.append((curent['name'],curent['phone']))
|
||||
curent = curent['next']
|
||||
records.sort(key=lambda pair: pair[0])
|
||||
return records
|
||||
|
||||
print(f"\n\n\n\n")
|
||||
|
||||
print("====== TESTING ll_list_all FUNC ======")
|
||||
|
||||
print(ll_list_all(head))
|
||||
|
||||
print("====== END ======")
|
||||
|
||||
|
||||
#============================== HASH FUNCTIONS =========================
|
||||
SIZE = 5
|
||||
buckets = [None] * SIZE
|
||||
|
||||
|
||||
|
||||
def hash_function(name, size):
|
||||
return hash(name) % size
|
||||
|
||||
|
||||
def ht_insert(buckets, name, phone):
|
||||
index = hash_function(name, len(buckets))
|
||||
head = buckets[index]
|
||||
new_head = ll_insert(head, name, phone)
|
||||
buckets[index] = new_head
|
||||
return buckets
|
||||
|
||||
print(f"\n\n\n ====== TEST INSERT HASH ======")
|
||||
print(buckets)
|
||||
ht_insert(buckets, "Ivan", "123-456")
|
||||
print(buckets)
|
||||
ht_insert(buckets, "Dima", "789-123")
|
||||
print(buckets)
|
||||
ht_insert(buckets, "Boris", "456-789")
|
||||
print(buckets)
|
||||
print("====== END TEST ======\n\n\n")
|
||||
|
||||
|
||||
def ht_find(buckets, name):
|
||||
index = hash_function(name, len(buckets))
|
||||
head = buckets[index]
|
||||
return ll_find(head, name)
|
||||
|
||||
print("====== TEST FIND HASH FUN ======")
|
||||
print("find by name Ivan: ",ht_find(buckets, "Ivan"))
|
||||
print("find by name Dima: ",ht_find(buckets, "Dima"))
|
||||
print("find by name Boris: ", ht_find(buckets, "Boris"))
|
||||
print("====== END TEST ======\n\n\n")
|
||||
|
||||
def ht_list_all(buckets):
|
||||
all_records = []
|
||||
for head in buckets:
|
||||
current = head
|
||||
while current is not None:
|
||||
all_records.append((current['name'], current['phone']))
|
||||
current = current['next']
|
||||
all_records.sort(key=lambda x: x[0])
|
||||
return all_records
|
||||
|
||||
|
||||
print("====== TEST FUNC LIST ALL ======")
|
||||
print(ht_list_all(buckets))
|
||||
print("====== END TEST ======\n\n\n")
|
||||
|
||||
def ht_delete(buckets, name):
|
||||
index = hash_function(name, len(buckets))
|
||||
head = buckets[index]
|
||||
new_head = ll_delete(head, name)
|
||||
buckets[index] = new_head
|
||||
return buckets
|
||||
|
||||
|
||||
print("====== GLOBAL TEST FOR HASH BASED FUN ======")
|
||||
buckets = [None] * 10
|
||||
|
||||
ht_insert(buckets, "Ivan", "123-456")
|
||||
print(buckets)
|
||||
ht_insert(buckets, "Boris", "789-012")
|
||||
print(buckets)
|
||||
ht_insert(buckets, "Anna", "345-678")
|
||||
print(buckets)
|
||||
ht_insert(buckets, "Ivan", "111-222") # update
|
||||
print(buckets)
|
||||
|
||||
print("Find Ivan`s phone: ",ht_find(buckets, "Ivan")) # 111-222
|
||||
print("Find Petr`s phone: ",ht_find(buckets, "Petr")) # None
|
||||
|
||||
# Удаляем
|
||||
print("delite Boris from buckets")
|
||||
ht_delete(buckets, "Boris")
|
||||
print("search Boris = ",ht_find(buckets, "Boris")) # None
|
||||
|
||||
# Все записи
|
||||
print("list all records: ",ht_list_all(buckets))
|
||||
print("====== END GLOBAL TEST ======\n\n\n")
|
||||
|
||||
|
||||
|
||||
# ======================== TREE FUNC ====================
|
||||
|
||||
def create_node(name,phone):
|
||||
return {'name': name, 'phone': phone, 'left': None, 'right': None}
|
||||
|
||||
print("====== START TREE FUNC CHAPTER ======\n\n")
|
||||
print("====== TEST CREATE NODE FUNC ======")
|
||||
root = create_node('Ivan', '123-456')
|
||||
print("Create Ivan node: ",root)
|
||||
print("====== END TEST ====== \n\n\n")
|
||||
|
||||
def bst_insert(root, name, phone):
|
||||
if root is None:
|
||||
return create_node(name, phone)
|
||||
|
||||
if name == root['name']:
|
||||
root['phone'] = phone
|
||||
elif name < root['name']:
|
||||
root['left'] = bst_insert(root['left'], name, phone)
|
||||
else:
|
||||
root['right'] = bst_insert(root['right'], name , phone)
|
||||
return root
|
||||
|
||||
print("====== TEST INSERT FUNC ======")
|
||||
root = bst_insert(root, 'Dima', '456-789')
|
||||
print("add Dima: ", root)
|
||||
root = bst_insert(root, 'Boris', '789-123')
|
||||
print("add Boris: ", root)
|
||||
root = bst_insert(root, 'Eva', '321-123')
|
||||
print("add Eva: ", root)
|
||||
print("====== END TEST =======\n\n\n")
|
||||
|
||||
|
||||
def bst_find(root, name):
|
||||
if root is None:
|
||||
return None
|
||||
if name == root['name']:
|
||||
return root['phone']
|
||||
elif name<root['name']:
|
||||
return bst_find(root['left'], name)
|
||||
else:
|
||||
return bst_find(root['right'], name)
|
||||
|
||||
|
||||
print("====== START FIND TEST ======")
|
||||
print("search by Ivan`s phone: ", bst_find(root, 'Ivan'))
|
||||
print("search by Eva`s phone: ", bst_find(root,'Eva'))
|
||||
print("====== END TEST ====== \n\n\n")
|
||||
|
||||
|
||||
|
||||
def find_min(node):
|
||||
while node['left'] is not None:
|
||||
node = node['left']
|
||||
return node
|
||||
|
||||
|
||||
def bst_delete(root,name):
|
||||
if root is None:
|
||||
return None
|
||||
|
||||
if name< root['name']:
|
||||
root['left'] = bst_delete(root['left'], name)
|
||||
elif name > root['name']:
|
||||
root['right'] = bst_delete(root['right'], name)
|
||||
|
||||
else:
|
||||
if root['left'] is None:
|
||||
return root['right']
|
||||
if root['right'] is None:
|
||||
return root['left']
|
||||
|
||||
min_node = find_min(root['right'])
|
||||
root['name'] = min_node['name']
|
||||
root['phone'] = min_node['phone']
|
||||
|
||||
root['right'] = bst_delete(root['right'], min_node['name'])
|
||||
return root
|
||||
|
||||
|
||||
|
||||
def bst_list_all(root):
|
||||
result = []
|
||||
def inorder(node):
|
||||
if node is None:
|
||||
return
|
||||
inorder(node['left'])
|
||||
result.append((node['name'], node['phone']))
|
||||
inorder(node['right'])
|
||||
inorder(root)
|
||||
return result
|
||||
|
||||
|
||||
print("====== GLOBAL TEST TREES ======")
|
||||
root = None
|
||||
|
||||
root = bst_insert(root, "Ivan", "123-456")
|
||||
print("add Ivan: ", root)
|
||||
root = bst_insert(root, "Boris", "789-012")
|
||||
print("add Boris: ", root)
|
||||
root = bst_insert(root, "Anna", "345-678")
|
||||
print("add Anna: ", root)
|
||||
root = bst_insert(root, "Ivan", "111-222") # обновление
|
||||
print("update Ivan: ", root)
|
||||
|
||||
print("Find Ivan`s phone: ",bst_find(root, "Ivan")) # 111-222
|
||||
print("Find Peter`s phone: ",bst_find(root, "Petr")) # None
|
||||
|
||||
root = bst_delete(root, "Boris")
|
||||
print("Del Boris")
|
||||
print("Find Boris: ",bst_find(root, "Boris")) # None
|
||||
|
||||
print("Find ALL: ",bst_list_all(root)) # [('Anna','345-678'), ('Ivan','111-222')]
|
||||
|
||||
|
||||
print("====== END TEST ======")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# ======================== EXPEREMENT CHAPTER ========================
|
||||
import random
|
||||
import time
|
||||
import csv
|
||||
import sys
|
||||
sys.setrecursionlimit(20000)
|
||||
|
||||
def generate_records(n, seed=42):
|
||||
random.seed(seed)
|
||||
records = []
|
||||
for i in range(1, n+1):
|
||||
name = f"User_{i:05d}"
|
||||
phone = f"{random.randint(100,999)}-{random.randint(1000,9999)}"
|
||||
records.append((name, phone))
|
||||
return records
|
||||
|
||||
def prepare_datasets(base_records):
|
||||
shuffled = base_records.copy()
|
||||
random.shuffle(shuffled)
|
||||
sorted_records = sorted(base_records, key=lambda x: x[0])
|
||||
return shuffled, sorted_records
|
||||
|
||||
def run_experiment(struct_funcs, records, mode_name, repeats=5):
|
||||
results = []
|
||||
for rep in range(repeats):
|
||||
struct = struct_funcs['create']()
|
||||
|
||||
# enter all records
|
||||
start = time.perf_counter()
|
||||
for name, phone in records:
|
||||
struct = struct_funcs['insert'](struct, name, phone)
|
||||
end = time.perf_counter()
|
||||
insert_time = end - start
|
||||
|
||||
# search for 110 records (100 real + 10 None)
|
||||
existing_names = [name for name, _ in records]
|
||||
sample_existing = random.sample(existing_names, 100)
|
||||
nonexistent = [f"None_{i}" for i in range(10)]
|
||||
search_names = sample_existing + nonexistent
|
||||
random.shuffle(search_names)
|
||||
|
||||
start = time.perf_counter()
|
||||
for name in search_names:
|
||||
_ = struct_funcs['find'](struct, name)
|
||||
end = time.perf_counter()
|
||||
find_time = end - start
|
||||
|
||||
# delete 10 random records
|
||||
to_delete = random.sample(existing_names, 10)
|
||||
start = time.perf_counter()
|
||||
for name in to_delete:
|
||||
struct = struct_funcs['delete'](struct, name)
|
||||
end = time.perf_counter()
|
||||
delete_time = end - start
|
||||
|
||||
results.append({
|
||||
'structure': struct_funcs['name'],
|
||||
'mode': mode_name,
|
||||
'repetition': rep+1,
|
||||
'insert_time': insert_time,
|
||||
'find_time': find_time,
|
||||
'delete_time': delete_time
|
||||
})
|
||||
return results
|
||||
|
||||
def main():
|
||||
N = 1000
|
||||
base_records = generate_records(N)
|
||||
shuffled, sorted_records = prepare_datasets(base_records)
|
||||
|
||||
structures = {
|
||||
'LinkedList': {
|
||||
'name': 'LinkedList',
|
||||
'create': lambda: None,
|
||||
'insert': ll_insert,
|
||||
'find': ll_find,
|
||||
'delete': ll_delete,
|
||||
'list_all': ll_list_all
|
||||
},
|
||||
'HashTable': {
|
||||
'name': 'HashTable',
|
||||
'create': lambda: [None] * 10,
|
||||
'insert': ht_insert,
|
||||
'find': ht_find,
|
||||
'delete': ht_delete,
|
||||
'list_all': ht_list_all
|
||||
},
|
||||
'BST': {
|
||||
'name': 'BST',
|
||||
'create': lambda: None,
|
||||
'insert': bst_insert,
|
||||
'find': bst_find,
|
||||
'delete': bst_delete,
|
||||
'list_all': bst_list_all
|
||||
}
|
||||
}
|
||||
|
||||
all_results = []
|
||||
repeats = 5
|
||||
|
||||
for struct_name, funcs in structures.items():
|
||||
print(f"Testing {struct_name} on random order...")
|
||||
res = run_experiment(funcs, shuffled, 'random', repeats)
|
||||
all_results.extend(res)
|
||||
|
||||
print(f"Testing {struct_name} in sorted order...")
|
||||
res = run_experiment(funcs, sorted_records, 'sorted', repeats)
|
||||
all_results.extend(res)
|
||||
|
||||
with open('experiment_results.csv', 'w', newline='', encoding='utf-8') as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerow(['Structure', 'Mode', 'Repeat', 'Insert (sec)', 'Search (sec)', 'Delete (sec)'])
|
||||
for r in all_results:
|
||||
writer.writerow([
|
||||
r['structure'],
|
||||
r['mode'],
|
||||
r['repetition'],
|
||||
f"{r['insert_time']:.6f}",
|
||||
f"{r['find_time']:.6f}",
|
||||
f"{r['delete_time']:.6f}"
|
||||
])
|
||||
|
||||
print("The experiment is complete. The results are saved in experiment_results.csv.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
31
BudakovIS/docs/data/1-st-exercize/experiment_results.csv
Normal file
31
BudakovIS/docs/data/1-st-exercize/experiment_results.csv
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
Structure,Mode,Repeat,Insert (sec),Search (sec),Delete (sec)
|
||||
LinkedList,random,1,0.140358,0.007040,0.000844
|
||||
LinkedList,random,2,0.138009,0.009197,0.000413
|
||||
LinkedList,random,3,0.114717,0.009266,0.000744
|
||||
LinkedList,random,4,0.117224,0.006914,0.000531
|
||||
LinkedList,random,5,0.136302,0.010432,0.000582
|
||||
LinkedList,sorted,1,0.106921,0.007845,0.000566
|
||||
LinkedList,sorted,2,0.116404,0.015005,0.004900
|
||||
LinkedList,sorted,3,0.125122,0.006956,0.000708
|
||||
LinkedList,sorted,4,0.122401,0.004220,0.000474
|
||||
LinkedList,sorted,5,0.111422,0.008343,0.000551
|
||||
HashTable,random,1,0.025442,0.004652,0.000078
|
||||
HashTable,random,2,0.035477,0.000985,0.000091
|
||||
HashTable,random,3,0.015387,0.001249,0.000298
|
||||
HashTable,random,4,0.014196,0.001167,0.000096
|
||||
HashTable,random,5,0.013819,0.000910,0.000094
|
||||
HashTable,sorted,1,0.013713,0.000897,0.000060
|
||||
HashTable,sorted,2,0.016816,0.001013,0.000116
|
||||
HashTable,sorted,3,0.018408,0.001019,0.000084
|
||||
HashTable,sorted,4,0.014490,0.000886,0.000093
|
||||
HashTable,sorted,5,0.012493,0.000867,0.000075
|
||||
BST,random,1,0.006755,0.000468,0.000065
|
||||
BST,random,2,0.006454,0.000380,0.000052
|
||||
BST,random,3,0.003348,0.000266,0.000033
|
||||
BST,random,4,0.004785,0.000379,0.000053
|
||||
BST,random,5,0.005253,0.000438,0.000083
|
||||
BST,sorted,1,0.331066,0.028260,0.002915
|
||||
BST,sorted,2,0.342009,0.025769,0.003155
|
||||
BST,sorted,3,0.282425,0.031293,0.002984
|
||||
BST,sorted,4,0.313816,0.022712,0.002957
|
||||
BST,sorted,5,0.287008,0.032645,0.002415
|
||||
|
Gitea Version: 1.22.0 |
