Skip to content

Lab 2 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
# Lab-1
# Lab 2 - HashTable

## Task
Implement a sorting algorithm - HeapSort(ascending/descending)
Task - implement a HashTable

## Result output
- Algorithm's name
- Execution time
- Counters: swaps, comparisons
- Sorting result

## Code must be covered with tests
- sort the input array
- sort in ascending order of sorted array in ascending order
- sort in descending order of sorted array in ascending order
- sort in ascending order of sorted array in descending order
- sort in descending order of sorted array in descending order

## How to run
- 'cd' into folder where you want to store this repository
- Clone this repository with command 'git clone https://github.com/yeldmitrenko/Algorithms_Labs.git'
- Choose branch lab_1 with command 'git checkout lab_1'
- Go into folder with files with command 'cd Algorithms_Labs'
- run command 'python main.py'
Code must be covered with tests
14 changes: 14 additions & 0 deletions hash_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from hash_table import HashTable

if __name__ == "__main__":
table = HashTable()

table.insert(0, "value1")
table.insert(1, "value2")
table.insert(2, "value3")
print(table.get_hashmap())

print(table.get(0))
print(table.delete(2))

print(table.get_hashmap())
55 changes: 55 additions & 0 deletions hash_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class HashTable:
def __init__(self):
self.size = 8
self.hashmap = [[] for _ in range(self.size)]

def hashing_func(self, key):
hashed_key = hash(key) % self.size
return hashed_key

def insert(self, key, value):
global i
hash_key = self.hashing_func(key)
key_exist = False
bucket = self.hashmap[hash_key]
for i, kv in enumerate(bucket):
k, v = kv
if key == k:
key_exist = True
break
if key_exist:
bucket[i] = (key, value)
return bucket
else:
bucket.append((key, value))
return bucket

def get(self, key):
hash_key = self.hashing_func(key)
bucket = self.hashmap[hash_key]
if bucket:
for kv in bucket:
k, v = kv
if key == k:
return bucket
elif not bucket:
raise KeyError("does not exist")

def delete(self, key):
global i
hash_key = self.hashing_func(key)
key_exist = False
bucket = self.hashmap[hash_key]
for i, kv in enumerate(bucket):
k, v = kv
if key == k:
key_exist = True
break
if key_exist:
del bucket[i]
return key
else:
raise KeyError("does not exist")

def get_hashmap(self):
return self.hashmap
19 changes: 19 additions & 0 deletions hash_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest
from hash_table import HashTable


class TestHashTable(unittest.TestCase):

def setUp(self) -> None:
self.table = HashTable()

def test_insert(self):
self.assertEqual(self.table.insert("key1", "value1"), [("key1", "value1")])

def test_get(self):
self.table.insert("key1", "value1")
self.assertEqual(self.table.get("key1"), [("key1", "value1")])

def test_delete(self):
self.table.insert("key1", "value1")
self.assertEqual(self.table.delete("key1"), "key1")
64 changes: 0 additions & 64 deletions heap_sort.py

This file was deleted.

28 changes: 0 additions & 28 deletions test.py

This file was deleted.