diff --git a/lab1/package b/lab1/package deleted file mode 100644 index e69de29..0000000 diff --git a/lab10/Kabanova/pythagorean_triples.py b/lab10/Kabanova/pythagorean_triples.py new file mode 100644 index 0000000..81a7b0a --- /dev/null +++ b/lab10/Kabanova/pythagorean_triples.py @@ -0,0 +1,27 @@ +import sys + + +def find_triples(array): + number_of_triples = 0 + array.sort() + length = len(array) + for i in range(0, length): + array[i] = array[i]*array[i] + for i in range(0, length): + j = 0 + k = i + while j != k: + if array[j] + array[k] == array[i]: + number_of_triples += 1 + k -= 1 + else: + if array[j] + array[k] < array[i]: + j += 1 + else: + k -= 1 + return number_of_triples + + +if __name__ == "__main__": + array_of_elements = [int(k) for k in sys.stdin.readline().split(' ')] + sys.stdout.write(str(find_triples(array_of_elements))) diff --git a/lab10/Kabanova/triples_test.py b/lab10/Kabanova/triples_test.py new file mode 100644 index 0000000..fb2dbac --- /dev/null +++ b/lab10/Kabanova/triples_test.py @@ -0,0 +1,43 @@ +import unittest + +from pythagorean_triples import find_triples + + +class TestTriples(unittest.TestCase): + + def test_empty_array(self): + array = [] + result = find_triples(array) + expected = 0 + self.assertEqual(expected, result) + + def test_example(self): + array = [23, 247, 19, 96, 264, 265, 132, 181] + result = find_triples(array) + expected = 2 + self.assertEqual(expected, result) + + def test_another(self): + array = [3, 4, 6, 10, 56] + result = find_triples(array) + expected = 0 + self.assertEqual(expected, result) + + def test_another_1(self): + array = [4, 3, 5] + result = find_triples(array) + expected = 1 + self.assertEqual(expected, result) + + def test_another_2(self): + array = [4, 3, 5, 144, 17, 145] + result = find_triples(array) + expected = 2 + self.assertEqual(expected, result) + + def test_another_3(self): + array = [4, 3, 5, 144, 17] + result = find_triples(array) + expected = 1 + self.assertEqual(expected, result) + diff --git a/lab11/Kabanova/histogram.py b/lab11/Kabanova/histogram.py new file mode 100644 index 0000000..0a0392a --- /dev/null +++ b/lab11/Kabanova/histogram.py @@ -0,0 +1,26 @@ +import sys + + +def max_square(array): + max_elem = array[0] + square = 0 + maxsquare = 0 + if len(array) == 1: + return 0 + else: + for i in range(1, len(array)): + if max_elem <= array[i]: + if square > maxsquare: + maxsquare = square + square = 0 + max_elem = array[i] + else: + square += max_elem - array[i] + return maxsquare + +if __name__ == "__main__": + array_of_elements = [int(k) for k in sys.stdin.readline().split(' ')] + sys.stdout.write(str(max_square(array_of_elements))) + + + diff --git a/lab11/Kabanova/histogram_tests.py b/lab11/Kabanova/histogram_tests.py new file mode 100644 index 0000000..4cabeac --- /dev/null +++ b/lab11/Kabanova/histogram_tests.py @@ -0,0 +1,30 @@ +import unittest + +from histogram import max_square + + +class TestMaxSquare(unittest.TestCase): + def test_empty_array(self): + array = [] + result = max_square(array) + expected = 0 + self.assertEqual(expected, result) + + def test_one_elem(self): + array = [2] + result = max_square(array) + expected = 0 + self.assertEqual(expected, result) + + def test_three_elem(self): + array = [2, 5, 1] + result = max_square(array) + expected = 4 + self.assertEqual(expected, result) + + def test_example(self): + array = [2, 5, 1, 2, 3, 4, 7, 7, 6] + result = max_square(array) + expected = 10 + self.assertEqual(expected, result) + diff --git a/lab12/Kabanova/bst_tests.py b/lab12/Kabanova/bst_tests.py new file mode 100644 index 0000000..1de1e31 --- /dev/null +++ b/lab12/Kabanova/bst_tests.py @@ -0,0 +1,48 @@ +import unittest + +from bst_tree import UnbalancedBinarySearchTree + + +class TestSorting(unittest.TestCase): + def test_empty(self): + bst_tree_set = UnbalancedBinarySearchTree() + result = [] + for v in bst_tree_set: + result.append(v) + expected = [] + self.assertEqual(expected, result) + + def test_1(self): + bst_tree_set = UnbalancedBinarySearchTree() + bst_tree_set.add(1) + result = [] + for v in bst_tree_set: + result.append(v) + expected = [1] + self.assertEqual(expected, result) + + def test_2(self): + bst_tree_set = UnbalancedBinarySearchTree() + bst_tree_set.add(1) + bst_tree_set.add(2) + bst_tree_set.add(9) + bst_tree_set.add(4) + bst_tree_set.add(3) + result = [] + for v in bst_tree_set: + result.append(v) + expected = [1, 2, 3, 4, 9] + self.assertEqual(expected, result) + + def test_3(self): + bst_tree_set = UnbalancedBinarySearchTree() + bst_tree_set.add(4) + bst_tree_set.add(2) + bst_tree_set.add(0) + bst_tree_set.add(1) + bst_tree_set.add(3) + result = [] + for v in bst_tree_set: + result.append(v) + expected = [0, 1, 2, 3, 4] + self.assertEqual(expected, result) diff --git a/lab12/Kabanova/bst_tree.py b/lab12/Kabanova/bst_tree.py new file mode 100644 index 0000000..a019b7e --- /dev/null +++ b/lab12/Kabanova/bst_tree.py @@ -0,0 +1,100 @@ +__author__ = 'catherinekabanova' + + +class Set: + def add(self, value): + pass + + def iterate(self): + pass + + +class Node: + def __init__(self, val): + self.val = val + self.left = None + self.right = None + self.parent = None + + +class UnbalancedBinarySearchTree(Set): + def __init__(self): + self.root = None + + def add(self, value): + if self.root is None: + self.root = Node(value) + return + z = Node(value) + y = None + x = self.root + while x is not None: + y = x + if value < x.val: + x = x.left + elif value > x.val: + x = x.right + else: + return + z.parent = y + if value < y.val: + y.left = z + else: + y.right = z + + def search(self, x, value): + if x is None: + return False + elif x.value == value: + return True + if value < x.value: + return self.search(x.left, value) + else: + return self.search(x.right, value) + + def iterate(self): + tree_iterator = TreeIterator(self.root) + while tree_iterator.has_next(): + yield tree_iterator.next() + + def __iter__(self): + return self.iterate() + + +class TreeIterator: + def __init__(self, root): + root = self.find_smallest(root) + self.min = Node(None) + self.min.parent = root + + def has_next(self): + slider = self.min + if slider.right is not None: + return True + while slider.parent is not None and slider is slider.parent.right: + slider = slider.parent + if slider.parent is None: + return False + return True + + def next(self): + self.min = self.find_next(self.min) + return self.min.val + + def find_smallest(self, root): + if root is None: + return None + while root.left is not None: + root = root.left + return root + + def find_next(self, node): + if node.right is not None: + node = node.right + while node.left is not None: + node = node.left + return node + else: + while node.parent is not None and node is node.parent.right: + node = node.parent + return node.parent diff --git a/lab13/Kabanova/splay_tests.py b/lab13/Kabanova/splay_tests.py new file mode 100644 index 0000000..bee9fd0 --- /dev/null +++ b/lab13/Kabanova/splay_tests.py @@ -0,0 +1,42 @@ +__author__ = 'catherinekabanova' +import unittest +import splay_tree + + +class TestSplayTree(unittest.TestCase): + + def test_0_empty(self): + tree = splay_tree.SplayTree() + self.assertFalse(tree.contains(6)) + + def test_1_iter(self): + tree = splay_tree.SplayTree() + tree.insert(0) + tree.insert(6) + tree.insert(2) + tree.insert(9) + array = [] + for v in tree: + array.append(v.key) + expected = [0, 2, 6, 9] + self.assertEquals(expected, array) + + def test_2_contain(self): + tree = splay_tree.SplayTree() + array = [5, 75, 63, 89, 10, 11] + for elem in array: + tree.insert(elem) + self.assertFalse(tree.contains(100)) + self.assertTrue(tree.contains(11)) + + def test_3_remove(self): + tree = splay_tree.SplayTree() + array = [5, 75, 63, 89, 10, 11] + for elem in array: + tree.insert(elem) + tree.remove(89) + tree.remove(5) + array_out = [] + for v in tree: + array_out.append(v.key) + self.assertEqual(array_out, [10, 11, 63, 75]) \ No newline at end of file diff --git a/lab13/Kabanova/splay_tree.py b/lab13/Kabanova/splay_tree.py new file mode 100644 index 0000000..e22c38a --- /dev/null +++ b/lab13/Kabanova/splay_tree.py @@ -0,0 +1,117 @@ +__author__ = 'catherinekabanova' + + +class Node: + def __init__(self, key): + self.key = key + self.right = None + self.left = None + + +class SplayTree: + """ + A splay tree is a self-adjusting binary search tree with the additional property + that recently accessed elements are quick to access again. + This class includes methods: insert, remove, contains, splay. + """ + + def __init__(self): + """Initialize splay tree""" + self.root = None + self.header = Node(None) + + def insert(self, value): + """Insert a new node with the key = value into the splay tree.""" + if self.root is None: + self.root = Node(value) + self.splay(value) + if self.root.key != value: + node_to_insert = Node(value) + if value < self.root.key: + node_to_insert.left = self.root.left + node_to_insert.right = self.root + self.root.left = None + else: + node_to_insert.right = self.root.right + node_to_insert.left = self.root + self.root.right = None + self.root = node_to_insert + + def remove(self, value): + """Remove node with the key = value from the splay tree.""" + self.splay(value) + if value != self.root.key: + raise Exception('There is no such key') + if self.root.left is None: + self.root = self.root.right + else: + x = self.root.right + self.root = self.root.left + self.splay(value) + self.root.right = x + + def contains(self, value): + """Check whether the splay tree contains a node with the key = value.""" + if self.root is None: + return None + self.splay(value) + if self.root.key != value: + return None + return self.root.key + + def splay(self, value): + """Splay node with key = value.""" + l_node = self.header + r_node = self.header + t_node = self.root + self.header.left = self.header.right = None + while True: + if value < t_node.key: + if t_node.left is None: + break + if value < t_node.left.key: + y = t_node.left + t_node.left = y.right + y.right = t_node + t_node = y + if t_node.left is None: + break + r_node.left = t_node + r_node = t_node + t_node = t_node.left + elif value > t_node.key: + if t_node.right is None: + break + if value > t_node.right.key: + y = t_node.right + t_node.right = y.left + y.left = t_node + t_node = y + if t_node.right is None: + break + l_node.right = t_node + l_node = t_node + t_node = t_node.right + else: + break + l_node.right = t_node.left + r_node.left = t_node.right + t_node.left = self.header.right + t_node.right = self.header.left + self.root = t_node + + def __iter__(self): + """Iterate the splay tree.""" + node = self.root + if node is None: + return + while True: + while node.left is not None: + node = node.left + self.splay(node.key) + if self.root.right is not None: + node = self.root.right + yield self.root + else: + yield self.root + break diff --git a/lab14/Kabanova/top_sort.py b/lab14/Kabanova/top_sort.py new file mode 100644 index 0000000..8905f81 --- /dev/null +++ b/lab14/Kabanova/top_sort.py @@ -0,0 +1,44 @@ +# colors: 0 - white, 1 - grey, 2 - black +class Vertex: + def __init__(self, key): + self.key = key + self.edges = [] + self.color = 0 + + +class Graph: + def __init__(self): + self.graph = [] + self.sorted_vertexes = [] + + def add_vertex(self, v_key): + i = len(self.graph) + while i <= v_key: + self.graph.append(Vertex(i)) + i += 1 + + def add_directed_link(self, v1, v2): + self.graph[v1].edges.append(self.graph[v2]) + + def dfs(self, v_key): + v = self.graph[v_key] + if v.color != 2: + v.color = 1 + for vertex in v.edges: + if vertex.color == 0: + self.dfs(vertex.key) + if vertex.color == 1: + return "None" + v.color = 2 + self.sorted_vertexes.append(v.key) + + def topological_sort(self, v_key): + if len(self.graph) == 0: + return 0 + for vertex in self.graph: + if vertex.color == 0: + p = self.dfs(vertex.key) + if p == "None": + return "None" + return self.sorted_vertexes + diff --git a/lab14/Kabanova/top_tests.py b/lab14/Kabanova/top_tests.py new file mode 100644 index 0000000..14defab --- /dev/null +++ b/lab14/Kabanova/top_tests.py @@ -0,0 +1,58 @@ +__author__ = 'catherinekabanova' +import unittest +import top_sort + + +class TestGraph(unittest.TestCase): + def test_empty(self): + graph = top_sort.Graph() + self.assertEqual(graph.topological_sort(0), 0) + + def test_cycle(self): + graph = top_sort.Graph() + graph.add_vertex(0) + graph.add_vertex(1) + graph.add_vertex(2) + graph.add_vertex(3) + graph.add_directed_link(0, 1) + graph.add_directed_link(0, 2) + graph.add_directed_link(0, 3) + graph.add_directed_link(2, 3) + graph.add_directed_link(3, 0) + self.assertEqual(graph.topological_sort(0), "None") + + def test_1(self): + graph = top_sort.Graph() + graph.add_vertex(0) + graph.add_vertex(1) + graph.add_vertex(2) + graph.add_vertex(3) + graph.add_vertex(4) + graph.add_vertex(5) + graph.add_vertex(6) + graph.add_vertex(7) + graph.add_directed_link(0, 1) + graph.add_directed_link(0, 2) + graph.add_directed_link(0, 3) + graph.add_directed_link(2, 3) + graph.add_directed_link(4, 5) + graph.add_directed_link(4, 6) + graph.add_directed_link(4, 7) + self.assertEqual(graph.topological_sort(0), [1, 3, 2, 0, 5, 6, 7, 4]) + + def test_2(self): + graph = top_sort.Graph() + graph.add_vertex(0) + graph.add_vertex(1) + graph.add_vertex(2) + graph.add_vertex(3) + graph.add_vertex(4) + graph.add_vertex(5) + graph.add_vertex(6) + graph.add_vertex(7) + graph.add_directed_link(2, 5) + graph.add_directed_link(2, 6) + graph.add_directed_link(4, 7) + graph.add_directed_link(5, 7) + graph.add_directed_link(6, 7) + self.assertEqual(graph.topological_sort(0), [0, 1, 7, 5, 6, 2, 3, 4]) diff --git a/lab16/Kabanova/paths_d.py b/lab16/Kabanova/paths_d.py new file mode 100644 index 0000000..203aaef --- /dev/null +++ b/lab16/Kabanova/paths_d.py @@ -0,0 +1,50 @@ +import heapq + + +class Vertex: + def __init__(self, v_key): + self.v_key = v_key + self.weight = {} + self.path = float("inf") + self.parent = None + + def __lt__(self, other): + return self.path < other.path + + +class WeightedGraph: + def __init__(self): + self.graph = [] + + def __str__(self): + return str(self.graph) + + def add_vertex(self, vertex): + self.graph.append(Vertex(vertex)) + + def add_direct_link(self, vertex_1, vertex_2, weight): + self.graph[vertex_1].weight[vertex_2] = weight + + def relax(self, start_v, end_v, weight): + if self.graph[end_v].path > self.graph[start_v].path + weight: + self.graph[end_v].path = self.graph[start_v].path + weight + self.graph[end_v].parent = start_v + + def paths(self, vertex_to): + queue = [] + for vertex in self.graph: + if vertex.v_key == vertex_to: + vertex.path = 0 + vertex.parent = vertex_to + else: + vertex.path = float("inf") + vertex.parent = None + heapq.heappush(queue, vertex) + while len(queue) != 0: + vertex_u = heapq.heappop(queue) + for w in vertex_u.weight: + self.relax(vertex_u.v_key, w, vertex_u.weight[w]) + for i in self.graph: + if i.path == float("inf"): + i.path = None + return [i.path for i in self.graph] \ No newline at end of file diff --git a/lab16/Kabanova/test_dejkstra.py b/lab16/Kabanova/test_dejkstra.py new file mode 100644 index 0000000..c14226a --- /dev/null +++ b/lab16/Kabanova/test_dejkstra.py @@ -0,0 +1,37 @@ +import unittest +import paths_d + + +class TestD(unittest.TestCase): + + def test_0(self): + graph = paths_d.WeightedGraph() + graph.add_vertex(0) + graph.add_vertex(1) + result = graph.paths(0) + expected = [0, None] + self.assertEquals(expected, result) + + def test_1(self): + graph = paths_d.WeightedGraph() + graph.add_vertex(0) + graph.add_vertex(1) + graph.add_vertex(2) + graph.add_direct_link(0, 1, 1) + graph.add_direct_link(1, 2, 0) + graph.add_direct_link(0, 2, 1) + result = graph.paths(0) + expected = [0, 1, 1] + self.assertEquals(expected, result) + + def test_2(self): + graph = paths_d.WeightedGraph() + graph.add_vertex(0) + graph.add_vertex(1) + graph.add_vertex(2) + graph.add_direct_link(0, 1, 2) + graph.add_direct_link(2, 1, 0) + graph.add_direct_link(0, 2, 1) + result = graph.paths(0) + expected = [0, 1, 1] + self.assertEquals(expected, result) diff --git a/lab18/Kabanova/d_l_tests.py b/lab18/Kabanova/d_l_tests.py new file mode 100644 index 0000000..4a1b1df --- /dev/null +++ b/lab18/Kabanova/d_l_tests.py @@ -0,0 +1,41 @@ +__author__ = 'catherinekabanova' +import unittest +from dam_lev_distance import d_l_distance + + +class TestSorting(unittest.TestCase): + def test_0_empty(self): + first_word = "" + second_word = "" + result = d_l_distance(first_word, second_word) + expected = 0 + self.assertEqual(expected, result) + + def test_1_empty(self): + first_word = "cat" + second_word = "" + result = d_l_distance(first_word, second_word) + expected = 3 + self.assertEqual(expected, result) + + def test_2(self): + first_word = "ab" + second_word = "aba" + result = d_l_distance(first_word, second_word) + expected = 1 + self.assertEqual(expected, result) + + def test_3(self): + first_word = "ab" + second_word = "aabs" + result = d_l_distance(first_word, second_word) + expected = 2 + self.assertEqual(expected, result) + + def test_4(self): + first_word = "Levenshtien" + second_word = "Frankenstein" + result = d_l_distance(first_word, second_word) + expected = 7 + self.assertEqual(expected, result) + diff --git a/lab18/Kabanova/dam_lev_distance.py b/lab18/Kabanova/dam_lev_distance.py new file mode 100644 index 0000000..623406d --- /dev/null +++ b/lab18/Kabanova/dam_lev_distance.py @@ -0,0 +1,24 @@ +from urllib3.connectionpool import xrange + +__author__ = 'catherinekabanova' + + +def d_l_distance(first_word, second_word): + distance = {} + len_1 = len(first_word) + len_2 = len(second_word) + for i in xrange(-1, len_1 + 1): + distance[(i, -1)] = i + 1 + for j in xrange(-1, len_2 + 1): + distance[(-1, j)] = j + 1 + for i in xrange(len_1): + for j in xrange(len_2): + if first_word[i] == second_word[j]: + difference = 0 + else: + difference = 1 + distance[(i, j)] = min(distance[(i - 1, j)] + 1, distance[(i, j - 1)] + 1, distance[(i - 1, j - 1)] + difference) + if i and j and first_word[i] == second_word[j - 1] and first_word[i - 1] == second_word[j]: + distance[(i, j)] = min(distance[(i, j)], distance[i - 2, j - 2] + difference) + return distance[len_1 - 1, len_2 - 1] + diff --git a/lab19/Kabanova/palindrom.py b/lab19/Kabanova/palindrom.py new file mode 100644 index 0000000..38024cb --- /dev/null +++ b/lab19/Kabanova/palindrom.py @@ -0,0 +1,32 @@ +import sys + + +def find_char(array, char): + for i in range(len(array) - 1, -1, -1): + if array[i] == char: + index = i + return True, index + return False, 0 + + +def max_palindrom(string): + if len(string) < 2: + return string + else: + char = string[0] + (char_exist, index) = find_char(string[1:], char) + if char_exist: + palindrom = [char] + max_palindrom(string[1:index + 1]) + [char] + else: + palindrom = [] + rest_palindrom = max_palindrom(string[1:]) + if len(palindrom) > len(rest_palindrom): + return palindrom + else: + return rest_palindrom + +if __name__ == "__main__": + string = sys.stdin.readline() + array_string = [str(k) for k in list(string)] + sys.stdout.write("".join(max_palindrom(array_string))) + diff --git a/lab19/Kabanova/palindrom_test.py b/lab19/Kabanova/palindrom_test.py new file mode 100644 index 0000000..317d1d8 --- /dev/null +++ b/lab19/Kabanova/palindrom_test.py @@ -0,0 +1,36 @@ +from unittest import TestCase +from palindrom import max_palindrom + +__author__ = 'catherinekabanova' + + +class Tests(TestCase): + def test_1(self): + string = "a" + array_string = [str(k) for k in list(string)] + result = "".join(max_palindrom(array_string)) + self.assertEqual(result, "a") + + def test_2(self): + string = "" + array_string = [str(k) for k in list(string)] + result = "".join(max_palindrom(array_string)) + self.assertEqual(result, "") + + def test_3(self): + string = "abca" + array_string = [str(k) for k in list(string)] + result = "".join(max_palindrom(array_string)) + self.assertEqual(result, "aca") + + def test_4(self): + string = "babcad" + array_string = [str(k) for k in list(string)] + result = "".join(max_palindrom(array_string)) + self.assertEqual(result, "aca") + + def test_5(self): + string = "аfроaзаупаланалаsdпуаilзора" + array_string = [str(k) for k in list(string)] + result = "".join(max_palindrom(array_string)) + self.assertEqual(result, "арозаупаланалапуазора") diff --git a/lab2/Kabanova/eratosthen_sieve.py b/lab2/Kabanova/eratosthen_sieve.py new file mode 100644 index 0000000..2aeb27c --- /dev/null +++ b/lab2/Kabanova/eratosthen_sieve.py @@ -0,0 +1,12 @@ +def sieve(n): + pros = [True for i in range(n + 1)] + pros[0] = False + pros[1] = False + for i in range(2, n + 1): + if pros[i]: + for k in range(2, n + 1): + if i * k < (n + 1): + pros[i * k] = False + return pros + +print sieve(10) diff --git a/lab20/Kabanova/sticks.py b/lab20/Kabanova/sticks.py new file mode 100644 index 0000000..4f15ac2 --- /dev/null +++ b/lab20/Kabanova/sticks.py @@ -0,0 +1,31 @@ +import sys + + +def max_square(number_of_sticks, sticks): + if number_of_sticks < 4: + return 0 + else: + sticks.sort() + sticks.reverse() + max_sq = 0 + max_stick = 0 + i = 1 + while i < len(sticks): + if sticks[i - 1] - sticks[i] <= 1: + if max_stick == 0: + max_stick = sticks[i] + i += 1 + else: + max_sq += max_stick * sticks[i] + max_stick = 0 + i += 1 + i += 1 + return max_sq + +if __name__ == "__main__": + first_line = int(sys.stdin.readline()) + second_line = [int(i) for i in sys.stdin.readline().split()] + sys.stdout.write(str(max_square(first_line, second_line))) + + + diff --git a/lab20/Kabanova/sticks_tests.py b/lab20/Kabanova/sticks_tests.py new file mode 100644 index 0000000..1310b66 --- /dev/null +++ b/lab20/Kabanova/sticks_tests.py @@ -0,0 +1,38 @@ +from unittest import TestCase +from sticks import max_square + +class Tests(TestCase): + def test_0(self): + number_of_sticks = 0 + sticks = [] + result = max_square(number_of_sticks, sticks) + expected = 0 + self.assertEqual(result, expected) + + def test_1(self): + number_of_sticks = 3 + sticks = [1, 3, 2] + result = max_square(number_of_sticks, sticks) + expected = 0 + self.assertEqual(result, expected) + + def test_2(self): + number_of_sticks = 4 + sticks = [1, 2, 6, 7] + result = max_square(number_of_sticks, sticks) + expected = 6 + self.assertEqual(result, expected) + + def test_3(self): + number_of_sticks = 7 + sticks = [1, 2, 3, 3, 6, 7, 8] + result = max_square(number_of_sticks, sticks) + expected = 21 + self.assertEqual(result, expected) + + def test_4(self): + number_of_sticks = 10 + sticks = [1, 0, 1, 8, 4, 12, 1, 16, 15, 10] + result = max_square(number_of_sticks, sticks) + expected = 15 + self.assertEqual(result, expected) \ No newline at end of file diff --git a/lab3/Kabanova/coins.py b/lab3/Kabanova/coins.py new file mode 100644 index 0000000..90a9a4b --- /dev/null +++ b/lab3/Kabanova/coins.py @@ -0,0 +1,18 @@ +import sys +first_line = sys.stdin.readline() +second_line = sys.stdin.readline() +number = int(first_line) +array_coins = [int(k) for k in second_line.split(' ')] + + +def methods(quantum, n, coins): + if quantum == 0: + return 1 + elif quantum < 0 or n == 0: + return 0 + else: + return methods(quantum, n - 1, coins) + methods(quantum - coins[n - 1], n, coins) + +sys.stdout.write(str(methods(number, len(array_coins), array_coins))) + + diff --git a/lab4/Kabanova/sorting.py b/lab4/Kabanova/sorting.py new file mode 100644 index 0000000..5daef50 --- /dev/null +++ b/lab4/Kabanova/sorting.py @@ -0,0 +1,64 @@ +import sys + + +line = sys.stdin.readline() +list_of_elements = [int(k) for k in line.split(' ')] + +k = 10 + + +def merge(elements, start, middle, end): + end_l = middle - start + end_r = end - middle + list_l = [] + list_r = [] + for i in range(0, end_l + 1): + list_l.append(elements[start + i]) + for j in range(0, end_r): + list_r.append(elements[middle + j + 1]) + list_l.append(2015 ** 10) + list_r.append(2015 ** 10) + i = 0 + j = 0 + for k in range(start, end): + if list_l[i] <= list_r[j]: + elements[k] = list_l[i] + i += 1 + else: + elements[k] = list_r[j] + j += 1 + + +def merge_sort(elements, start, end): + if end - start + 1 > k: + middle = (start + end) // 2 + merge_sort(elements, start, middle) + merge_sort(elements, middle, end) + merge(elements, start, middle + 1, end) + else: + insertion_sort(elements) + return elements + + +def insertion_sort(elements): + for j in range(2, len(elements)): + key = elements[j] + i = j - 1 + while i > 0 and elements[i] > key: + elements[i + 1] = elements[i] + i -= 1 + elements[i + 1] = key + return elements + + +def combine_sorting(elements): + if len(elements) < k: + insertion_sort(elements) + else: + merge_sort(elements, 0, len(elements) - 1) + + +combine_sorting(list_of_elements) +for i in range(0, len(list_of_elements)): + sys.stdout.write(str(list_of_elements[i]) + ' ') + diff --git a/lab5/Kabanova/quicksort.py b/lab5/Kabanova/quicksort.py new file mode 100644 index 0000000..5b477f6 --- /dev/null +++ b/lab5/Kabanova/quicksort.py @@ -0,0 +1,42 @@ +import random +import sys + +line = sys.stdin.readline() +array_of_elements = [int(k) for k in line.split(' ')] + + +def quick_sort(elements, start, end): + if start < end: + middle = random_partition_quick_sort(elements, start, end) + quick_sort(elements, start, middle - 1) + quick_sort(elements, middle + 1, end) + return elements + + +def partition_quick_sort(elements, start, end): + x = elements[end] + i = start - 1 + for j in range(start, end): + if elements[j] <= x: + i += 1 + swap(elements, i, j) + swap(elements, i + 1, end) + return i + 1 + + +def random_partition_quick_sort(elements, start, end): + random_int = random.randint(start, end) + swap(elements, end, random_int) + return partition_quick_sort(elements, start, end) + + +def swap(array, i, j): + middle = array[i] + array[i] = array[j] + array[j] = middle + return array + +quick_sort(array_of_elements, 0, len(array_of_elements) - 1) +for i in range(0, len(array_of_elements)): + sys.stdout.write(str(array_of_elements[i]) + ' ') + diff --git a/lab6/Kabanova/radix_sort.py b/lab6/Kabanova/radix_sort.py new file mode 100644 index 0000000..4fa4295 --- /dev/null +++ b/lab6/Kabanova/radix_sort.py @@ -0,0 +1,46 @@ +import sys +import math + +__author__ = 'catherinekabanova' + + +def simple_radix_sort(array): + max_length = -1 + for number in array: + number_len = int(math.log10(number)) + 1 + if number_len > max_length: + max_length = number_len + buckets = [[] for i in range(0, 10)] + for digit in range(0, max_length): + for number in array: + buckets[(number // 10**digit) % 10].append(number) + del array[:] + for bucket in buckets: + array.extend(bucket) + del bucket[:] + return array + + +def radix_sort(array): + positive_array = [] + negative_array = [] + for number in array: + if number >= 0: + positive_array.append(number) + else: + negative_array.append(abs(number)) + if negative_array: + negative_array = simple_radix_sort(negative_array) + for i in range(len(negative_array)): + negative_array[i] *= -1 + negative_array.reverse() + if positive_array: + positive_array = simple_radix_sort(positive_array) + return negative_array + positive_array + +if __name__ == '__main__': + array_of_elements = [int(k) for k in sys.stdin.readline().split(' ')] + radix_sort(array_of_elements) + for i in range(0, len(array_of_elements)): + sys.stdout.write(str(radix_sort(array_of_elements)[i]) + ' ') + diff --git a/lab6/Kabanova/radix_sort_test.py b/lab6/Kabanova/radix_sort_test.py new file mode 100644 index 0000000..63f8871 --- /dev/null +++ b/lab6/Kabanova/radix_sort_test.py @@ -0,0 +1,34 @@ +import unittest +from radix_sort import radix_sort + + +class TestSorting(unittest.TestCase): + def test_trivial(self): + arr = [1, 3, 2] + res = radix_sort(arr) + expected = [1, 2, 3] + self.assertEqual(expected, res) + + def test_empty(self): + arr = [21, 10, 36, 99, 57] + res = radix_sort(arr) + expected = [10, 21, 36, 57, 99] + self.assertEqual(expected, res) + + def test_one_negative(self): + arr = [-21, 10, 36, 99, 57] + res = radix_sort(arr) + expected = [-21, 10, 36, 57, 99] + self.assertEqual(expected, res) + + def test_two_negative(self): + arr = [-21, 10, 36, -99, 57] + res = radix_sort(arr) + expected = [-99, -21, 10, 36, 57] + self.assertEqual(expected, res) + + def test_all_negative(self): + arr = [-21, -10, -36, -99, -57] + res = radix_sort(arr) + expected = [-99, -57, -36, -21, -10] + self.assertEqual(expected, res) diff --git a/lab7/Kabanova/mergesort.py b/lab7/Kabanova/mergesort.py new file mode 100644 index 0000000..5daef50 --- /dev/null +++ b/lab7/Kabanova/mergesort.py @@ -0,0 +1,64 @@ +import sys + + +line = sys.stdin.readline() +list_of_elements = [int(k) for k in line.split(' ')] + +k = 10 + + +def merge(elements, start, middle, end): + end_l = middle - start + end_r = end - middle + list_l = [] + list_r = [] + for i in range(0, end_l + 1): + list_l.append(elements[start + i]) + for j in range(0, end_r): + list_r.append(elements[middle + j + 1]) + list_l.append(2015 ** 10) + list_r.append(2015 ** 10) + i = 0 + j = 0 + for k in range(start, end): + if list_l[i] <= list_r[j]: + elements[k] = list_l[i] + i += 1 + else: + elements[k] = list_r[j] + j += 1 + + +def merge_sort(elements, start, end): + if end - start + 1 > k: + middle = (start + end) // 2 + merge_sort(elements, start, middle) + merge_sort(elements, middle, end) + merge(elements, start, middle + 1, end) + else: + insertion_sort(elements) + return elements + + +def insertion_sort(elements): + for j in range(2, len(elements)): + key = elements[j] + i = j - 1 + while i > 0 and elements[i] > key: + elements[i + 1] = elements[i] + i -= 1 + elements[i + 1] = key + return elements + + +def combine_sorting(elements): + if len(elements) < k: + insertion_sort(elements) + else: + merge_sort(elements, 0, len(elements) - 1) + + +combine_sorting(list_of_elements) +for i in range(0, len(list_of_elements)): + sys.stdout.write(str(list_of_elements[i]) + ' ') + diff --git a/lab7/Kabanova/quicksort.py b/lab7/Kabanova/quicksort.py new file mode 100644 index 0000000..5b477f6 --- /dev/null +++ b/lab7/Kabanova/quicksort.py @@ -0,0 +1,42 @@ +import random +import sys + +line = sys.stdin.readline() +array_of_elements = [int(k) for k in line.split(' ')] + + +def quick_sort(elements, start, end): + if start < end: + middle = random_partition_quick_sort(elements, start, end) + quick_sort(elements, start, middle - 1) + quick_sort(elements, middle + 1, end) + return elements + + +def partition_quick_sort(elements, start, end): + x = elements[end] + i = start - 1 + for j in range(start, end): + if elements[j] <= x: + i += 1 + swap(elements, i, j) + swap(elements, i + 1, end) + return i + 1 + + +def random_partition_quick_sort(elements, start, end): + random_int = random.randint(start, end) + swap(elements, end, random_int) + return partition_quick_sort(elements, start, end) + + +def swap(array, i, j): + middle = array[i] + array[i] = array[j] + array[j] = middle + return array + +quick_sort(array_of_elements, 0, len(array_of_elements) - 1) +for i in range(0, len(array_of_elements)): + sys.stdout.write(str(array_of_elements[i]) + ' ') + diff --git a/lab7/Kabanova/radixsort.py b/lab7/Kabanova/radixsort.py new file mode 100644 index 0000000..4fa4295 --- /dev/null +++ b/lab7/Kabanova/radixsort.py @@ -0,0 +1,46 @@ +import sys +import math + +__author__ = 'catherinekabanova' + + +def simple_radix_sort(array): + max_length = -1 + for number in array: + number_len = int(math.log10(number)) + 1 + if number_len > max_length: + max_length = number_len + buckets = [[] for i in range(0, 10)] + for digit in range(0, max_length): + for number in array: + buckets[(number // 10**digit) % 10].append(number) + del array[:] + for bucket in buckets: + array.extend(bucket) + del bucket[:] + return array + + +def radix_sort(array): + positive_array = [] + negative_array = [] + for number in array: + if number >= 0: + positive_array.append(number) + else: + negative_array.append(abs(number)) + if negative_array: + negative_array = simple_radix_sort(negative_array) + for i in range(len(negative_array)): + negative_array[i] *= -1 + negative_array.reverse() + if positive_array: + positive_array = simple_radix_sort(positive_array) + return negative_array + positive_array + +if __name__ == '__main__': + array_of_elements = [int(k) for k in sys.stdin.readline().split(' ')] + radix_sort(array_of_elements) + for i in range(0, len(array_of_elements)): + sys.stdout.write(str(radix_sort(array_of_elements)[i]) + ' ') + diff --git a/lab7/Kabanova/sorting_compare.py b/lab7/Kabanova/sorting_compare.py new file mode 100644 index 0000000..bc75098 --- /dev/null +++ b/lab7/Kabanova/sorting_compare.py @@ -0,0 +1,112 @@ +from random import randint +from time import time + +import pylab + +from mergesort import combine_sorting +from quicksort import quick_sort +from radixsort import radix_sort + + +def generate_random_array(lo, hi, size): + answer = [randint(lo, hi) for i in range(size)] + for i in range(1, size): + if randint(0, 1000000) < 30000: + answer[randint(0, i)] = answer[i - 1] + return answer + + +def positive_and_negative(s): + return generate_random_array(-1000000, 1000000, s) + + +def positive(s): + return generate_random_array(0, 10000, s) + + +def partially_sorted(s): + hi = 10000 + answer = [0 for i in range(s)] + answer[0] = randint(0, hi) + for i in range(1, s): + if randint(0, 1000000) < 30000: + answer[i] = answer[i - 1] + else: + answer[i] = (answer[i - 1] + randint(0, hi)) % hi + return answer + + +def ascending_sorted(s): + answer = [0 for i in range(s)] + answer[0] = randint(0, 10000) + for i in range(1, s): + if randint(0, 1000000) < 30000: + answer[i] = answer[i - 1] + else: + answer[i] = answer[i - 1] + randint(0, (10000 - answer[i - 1]) // 10) + return answer + + +def descending_sorted(s): + answer = ascending_sorted(s) + answer.reverse() + return answer + + +def unique_number(s): + number = randint(-1000000, 1000000) + return [number for i in range(s)] + + +root = "/" +parts = __file__.split('/') +for i in range(1, len(parts) - 3): + root += parts[i] + '/' + + +def grade(sort_func, array_generator, size): + mil = 0.0 + for i in range(5): + array = array_generator(size) + t1 = time() + sort_func(array) + t2 = time() + mil += t2 - t1 + mil /= 5.0 + return mil + + +sort_functions = {"Quick": quick_sort, + "Merge": combine_sorting, + "Radix": radix_sort, + "Sort": sorted} + +generators = {"Array of random integers from [-1e6, 1e6]": positive_and_negative, + "Array of random inegerts from [0, 1e4]": positive, + "Array with some sorted subarrays": partially_sorted, + "Already sorted array": ascending_sorted, + "Descending sorted array": descending_sorted, + "Array with the only distinct element": unique_number} + +if __name__ == "__main__": + + sizes = [100 + 100000 * i for i in range(6)] + cur = 1 + + for gen_name, gen in generators.items(): + pylab.subplot(2, 3, cur) + pylab.xlabel("size, elements") + pylab.ylabel("time, sec") + print("Now passing: %s" % gen_name) + for func_name, func in sort_functions.items(): + millis = [] + for size in sizes: + millis.append(grade(func, gen, size)) + print("\tUsing %s on array of size %s" % (func_name, size)) + print("\t***\n\n") + pylab.plot(sizes, millis, label=func_name) + pylab.title(gen_name) + pylab.legend(loc='upper left', title="Sorts") + cur += 1 + + pylab.show() diff --git a/lab8/Kabanova/example_queue.py b/lab8/Kabanova/example_queue.py new file mode 100644 index 0000000..c5a61dc --- /dev/null +++ b/lab8/Kabanova/example_queue.py @@ -0,0 +1,119 @@ +class Queue: + def pop(self): + pass + + def push(self, n): + pass + + def size(self): + pass + + +class StackQueue(Queue): + def __init__(self): + self.queue1 = ArrayQueue() + self.queue2 = ArrayQueue() + + def pop(self): + if self.queue1.is_empty() and self.queue2.is_empty(): + print("empty") + else: + if self.queue1.is_empty(): + while not self.queue2.is_empty(): + self.queue1.push(self.queue2.last_elem()) + self.queue2.pop() + print(self.queue1.pop()) + else: + print(self.queue1.pop()) + + def push(self, n): + self.queue2.push(n) + print("ok") + + def size(self): + return self.queue1.size() + self.queue2.size() + + def __str__(self): + return str(self.queue1) + str(self.queue2) + + +class ArrayQueue: + def __init__(self): + self.queue = [] + + def push(self, x): + self.queue.append(x) + return x + + def pop(self): + pop_elem = self.last_elem() + if len(self.queue) != 1: + self.queue = [self.queue[i] for i in range(0, len(self.queue) - 1)] + else: + self.queue = [] + return pop_elem + + def size(self): + return len(self.queue) + + def is_empty(self): + if not self.queue: + return True + else: + return False + + def last_elem(self): + return self.queue[len(self.queue) - 1] + + def __str__(self): + return str(self.queue) + + +class MaxElementQueue(Queue): + def __init__(self): + self.queue = StackQueue() + self.max2 = float('-Inf') + self.max_queue = ArrayQueue() + + def pop(self): + if self.size() > 0: + local_max = float('-Inf') + if self.queue.queue1.is_empty(): + while not self.queue.queue2.is_empty(): + item = self.queue.queue2.last_elem() + self.queue.queue2.pop() + self.queue.queue1.push(item) + if item > local_max: + local_max = item + self.max_queue.push(local_max) + if self.queue.queue1.last_elem() == self.max_queue.last_elem(): + self.max_queue.pop() + print(self.queue.queue1.pop()) + else: + if not self.max_queue.is_empty(): + if self.queue.queue1.last_elem() == self.max_queue.last_elem(): + self.max_queue.pop() + print(self.queue.queue1.pop()) + else: + print("empty") + + def push(self, n): + if self.size() == 0: + self.max2 = n + else: + if n >= self.max2: + self.max2 = n + self.queue.push(n) + + def size(self): + return self.queue.size() + + def max_elem(self): + if self.queue.size() == 0: + print("empty") + else: + if self.max2 > self.max_queue.last_elem(): + print(self.max2) + else: + print(self.max_queue.last_elem()) + diff --git a/lab8/Kabanova/queue_tests.py b/lab8/Kabanova/queue_tests.py new file mode 100644 index 0000000..01a215f --- /dev/null +++ b/lab8/Kabanova/queue_tests.py @@ -0,0 +1,55 @@ +import unittest + +import example_queue + + +class TestMaxElement(unittest.TestCase): + def test_empty_queue(self): + ex_queue = example_queue.MaxElementQueue() + result = ex_queue.max_elem() + expected = "empty" + self.assertEqual(expected, result) + + def test_max_no_pop(self): + ex_queue = example_queue.MaxElementQueue() + ex_queue.push(1) + ex_queue.push(3) + ex_queue.push(100) + ex_queue.push(42) + ex_queue.push(3) + ex_queue.push(64) + result = ex_queue.max_elem() + expected = 100 + self.assertEqual(expected, result) + + def test_one_pop(self): + ex_queue = example_queue.MaxElementQueue() + ex_queue.push(1) + ex_queue.push(3) + ex_queue.push(100) + ex_queue.pop() + ex_queue.push(42) + ex_queue.push(3) + ex_queue.push(64) + result = ex_queue.max_elem() + expected = 64 + self.assertEqual(expected, result) + + def test_all_pop(self): + ex_queue = example_queue.MaxElementQueue() + ex_queue.push(1) + ex_queue.push(3) + ex_queue.pop() + ex_queue.pop() + ex_queue.push(100) + ex_queue.push(42) + ex_queue.push(3) + ex_queue.push(64) + ex_queue.pop() + ex_queue.pop() + ex_queue.pop() + ex_queue.pop() + result = ex_queue.max_elem() + expected = "empty" + self.assertEqual(expected, result) +