Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабы по АиСД 2023 / Baraeva_Elizaveta_cw.doc
Скачиваний:
0
Добавлен:
09.09.2026
Размер:
140 Кб
Скачать

Приложение а иСходный код программы

Название файла: main.py

class Node:

def __init__(self, key, value):

self.key = key

self.value = value

self.next = None

class HashTable:

def __init__(self, capacity):

self.capacity = capacity

self.size = 0

self.table = [None] * capacity

def _hash(self, key):

return hash(key) % self.capacity

def insert(self, key, value):

index = self._hash(key)

if self.table[index] is None:

self.table[index] = Node(key, value)

self.size += 1

else:

new_node = Node(key, value)

current = self.table[index]

while current.next:

if current.key == key:

current.value = value

return

current = current.next

if current.key == key:

current.value = value

return

current.next = new_node

self.size += 1

def search(self, key):

index = self._hash(key)

current = self.table[index]

while current:

if current.key == key:

return current.value

current = current.next

raise KeyError(key)

def remove(self, key):

index = self._hash(key)

previous = None

current = self.table[index]

while current:

if current.key == key:

if previous:

previous.next = current.next

else:

self.table[index] = current.next

self.size -= 1

return

previous = current

current = current.next

raise KeyError(key)

def __str__(self):

elements = []

for i in range(self.capacity):

elements_str = ''

current = self.table[i]

if current is None:

continue

while current.next:

elements_str += f'({current.key}, {current.value}) -> '

current = current.next

elements_str += f'({current.key}, {current.value})'

elements.append(elements_str)

return str(elements)

def __len__(self):

return self.size

Приложение б тестирование

Название файла: test.py

import main

def test_insert():

ht = main.HashTable(4)

ht.insert(0, 23)

ht.insert(1, 45)

ht.insert(2, 7)

ht.insert(3, 99)

ht.insert(4, 178)

ht.insert(9, 10)

ht.insert(17, 64)

ht.insert(23, 67)

assert ht.__str__() == "['(0, 23) -> (4, 178)', '(1, 45) -> (9, 10) -> (17, 64)', '(2, 7)', '(3, 99) -> (23, 67)']"

def test_search():

ht = main.HashTable(4)

ht.insert(0, 23)

ht.insert(1, 45)

ht.insert(2, 7)

ht.insert(3, 99)

ht.insert(4, 178)

ht.insert(9, 10)

ht.insert(17, 64)

ht.insert(23, 67)

assert ht.search(0) == 23

assert ht.search(1) == 45

assert ht.search(2) == 7

assert ht.search(3) == 99

def test_remove():

ht = main.HashTable(4)

ht.insert(0, 23)

ht.insert(1, 45)

ht.insert(2, 7)

ht.insert(3, 99)

ht.insert(4, 178)

ht.insert(9, 10)

ht.insert(17, 64)

ht.insert(23, 67)

ht.remove(0)

assert ht.__str__() == "['(4, 178)', '(1, 45) -> (9, 10) -> (17, 64)', '(2, 7)', '(3, 99) -> (23, 67)']"

ht.remove(4)

assert ht.__str__() == "['(1, 45) -> (9, 10) -> (17, 64)', '(2, 7)', '(3, 99) -> (23, 67)']"

ht.remove(23)

assert ht.__str__() == "['(1, 45) -> (9, 10) -> (17, 64)', '(2, 7)', '(3, 99)']"

ht.remove(2)

assert ht.__str__() == "['(1, 45) -> (9, 10) -> (17, 64)', '(3, 99)']"

18

Соседние файлы в папке Лабы по АиСД 2023