From 45afe4280a3d8b1d56d0207ca9ecb475a5bbd83e Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Sun, 27 Dec 2020 11:07:14 +0200 Subject: [PATCH 1/3] added files for 5 lab --- main.py | 9 +++++++++ tests.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 main.py create mode 100644 tests.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..3f2c32f --- /dev/null +++ b/main.py @@ -0,0 +1,9 @@ +def naive_search(string:str, substring:str): + result = [] + for element in range(len(string)-len(substring)+1): + for sub_element in range(len(substring)): + if not string[element+sub_element]==substring[sub_element]: + break + else: + result.append((element, element+len(substring))) + return result \ No newline at end of file diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..97b2ad2 --- /dev/null +++ b/tests.py @@ -0,0 +1,15 @@ +import unittest + +from main import naive_search + + +class MyTestCase(unittest.TestCase): + def test_naive_search(self): + self.assertEqual(naive_search("aabaacaadaabaaba", "aaba"), [(0, 4), (9, 13), (12, 16)]) + self.assertEqual(naive_search("AABCCAADDEE", "FAA"), []) + self.assertEqual(naive_search("AAAAAAA", "AAAA"), [(0, 4), (1, 5), (2, 6), (3, 7)]) + self.assertEqual(naive_search("AAAAAAAAAAAAAAAAAB", "AAAB"), [(14, 18)]) + + +if __name__ == '__main__': + unittest.main() From a79e8b677b38fb829aa99946a6c8f2aeaa066ef5 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Sun, 27 Dec 2020 13:48:53 +0200 Subject: [PATCH 2/3] Update README.md --- README.md | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/README.md b/README.md index 6a4f722..336782f 100644 --- a/README.md +++ b/README.md @@ -25,15 +25,4 @@ python main.py ``` *** - -this program shows you the results of insertion and merge sorts for each algorithm, it also shows some additional information, such as: -* time, -* swap counter, -* comparison counter. - -counters represent the numbers of corresponding operations that occurred during the sorting process. - -the list of objects is taken from *basin.csv* file. Each row there represents a new object as a sequence of its properties' values separated by commas. The order is: -``` -address, volume_of_water, max_number_of_visitors -``` +Цей код шукає підстрічки у стрічці за допомогою наївного алгоритму. From 65a2d613b3aa06d2844b1872dccb735242c0bd4c Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 29 Dec 2020 19:12:33 +0200 Subject: [PATCH 3/3] added code with some changes --- main.py | 15 ++++++++------- tests.py | 8 ++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 3f2c32f..e1d2cbf 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,10 @@ -def naive_search(string:str, substring:str): +def naive_search(string: str, substring: str) -> list: result = [] - for element in range(len(string)-len(substring)+1): - for sub_element in range(len(substring)): - if not string[element+sub_element]==substring[sub_element]: - break - else: - result.append((element, element+len(substring))) + if string is not None and substring is not None and len(string) >= len(substring) and len(substring) != 0: + for element in range(len(string) - len(substring) + 1): + for sub_element in range(len(substring)): + if not string[element + sub_element] == substring[sub_element]: + break + else: + result.append((element, element + len(substring))) return result \ No newline at end of file diff --git a/tests.py b/tests.py index 97b2ad2..56ce760 100644 --- a/tests.py +++ b/tests.py @@ -10,6 +10,14 @@ def test_naive_search(self): self.assertEqual(naive_search("AAAAAAA", "AAAA"), [(0, 4), (1, 5), (2, 6), (3, 7)]) self.assertEqual(naive_search("AAAAAAAAAAAAAAAAAB", "AAAB"), [(14, 18)]) + def test_empty_string(self): + self.assertEqual(naive_search("","abc"), []) + + def test_empty_substr(self): + self.assertEqual(naive_search("abc",""),[]) + + + if __name__ == '__main__': unittest.main()