Skip to content
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

Lab 7 #10

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
24 changes: 2 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
# Lab-1
# Lab 7 - Dynamic Programming

## Task
Implement a sorting algorithm - HeapSort(ascending/descending)
### Rabin Karp String Matching Algorithm

## 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'
64 changes: 0 additions & 64 deletions heap_sort.py

This file was deleted.

46 changes: 46 additions & 0 deletions rabin_karp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
alphabet_size = 256


def calculate_hash(pattern, primary_number):
reducing_hash_number = 1

for i in range(len(pattern) - 1):
reducing_hash_number = (reducing_hash_number * alphabet_size) % primary_number

return reducing_hash_number


def rabin_karp_search(text, pattern, primary_number=101):
positions_array = []
text_hash_value = 0
pattern_hash_value = 0

reducing_hash_number = calculate_hash(pattern, primary_number)

for i in range(len(pattern)):
text_hash_value = (alphabet_size * text_hash_value + ord(text[i])) % primary_number
pattern_hash_value = (alphabet_size * pattern_hash_value + ord(pattern[i])) % primary_number

for i in range(len(text) - len(pattern) + 1):
if pattern_hash_value == text_hash_value:
matches_number = 0
for j in range(len(pattern)):
if pattern[j] == text[j + i]:
matches_number += 1
else:
break
if matches_number == len(pattern):
positions_array.append(i)

if i < len(text) - len(pattern):
text_hash_value = (alphabet_size * (text_hash_value - ord(text[i]) * reducing_hash_number) + ord(
text[i + len(pattern)])) % primary_number
text_hash_value = text_hash_value + primary_number if text_hash_value < 0 else text_hash_value
return positions_array


if __name__ == '__main__':
text = "BBCLLNMNLLN"
pattern = "LLN"

print(rabin_karp_search(text, pattern))
36 changes: 17 additions & 19 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import unittest
from heap_sort import heap_sort
from copy import deepcopy

from lab7.rabin_karp import rabin_karp_search

class TestHeapSort(unittest.TestCase):
def setUp(self) -> None:
self.array_example = [1, 2, 56, 45, -9, 78, 11]
self.array_sorted_asc = [-9, 1, 2, 11, 45, 56, 78]
self.array_sorted_desc = [78, 56, 45, 11, 2, 1, -9]

def test_sort_asc(self):
self.assertListEqual(heap_sort(deepcopy(self.array_example), "asc"), self.array_sorted_asc)
class RabinKarpTest(unittest.TestCase):
def setUp(self) -> None:
self.test_text1 = "AAA NN MM LL K AA"
self.test_pattern1 = "AA"
self.benchmark_positions_array1 = [0, 1, 15]

def test_sort_desc(self):
self.assertListEqual(heap_sort(deepcopy(self.array_example), "desc"), self.array_sorted_desc)
self.test_pattern2 = "LLN"
self.test_text2 = "BBCLLNMNLLN"
self.benchmark_positions_array2 = [3, 8]

def test_sort_asc_in_asc(self):
self.assertListEqual(heap_sort(deepcopy(self.array_sorted_asc), "asc"), self.array_sorted_asc)
def test_rabin_karp_search_1(self):
pattern_positions = rabin_karp_search(self.test_text1, self.test_pattern1)
self.assertEqual(pattern_positions, self.benchmark_positions_array1)

def test_sort_asc_in_desc(self):
self.assertListEqual(heap_sort(deepcopy(self.array_sorted_asc), "desc"), self.array_sorted_desc)
def test_rabin_karp_search_2(self):
pattern_positions = rabin_karp_search(self.test_text2, self.test_pattern2)
self.assertEqual(pattern_positions, self.benchmark_positions_array2)

def test_sort_desc_in_asc(self):
self.assertListEqual(heap_sort(deepcopy(self.array_sorted_desc), "asc"), self.array_sorted_asc)

def test_sort_desc_in_desc(self):
self.assertListEqual(heap_sort(deepcopy(self.array_sorted_desc), "desc"), self.array_sorted_desc)
if __name__ == '__main__':
unittest.main()