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

Кабанова №7,14,16 #19

Open
wants to merge 31 commits into
base: master
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
Empty file removed lab1/package
Empty file.
27 changes: 27 additions & 0 deletions lab10/Kabanova/pythagorean_triples.py
Original file line number Diff line number Diff line change
@@ -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)))
43 changes: 43 additions & 0 deletions lab10/Kabanova/triples_test.py
Original file line number Diff line number Diff line change
@@ -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)

26 changes: 26 additions & 0 deletions lab11/Kabanova/histogram.py
Original file line number Diff line number Diff line change
@@ -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)))



30 changes: 30 additions & 0 deletions lab11/Kabanova/histogram_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest

from histogram import max_square


class TestMaxSquare(unittest.TestCase):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тесты похоже не запускались, первые 3 падают
но алгоритм правильный

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)

48 changes: 48 additions & 0 deletions lab12/Kabanova/bst_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import unittest

from bst_tree import UnbalancedBinarySearchTree


class TestSorting(unittest.TestCase):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тесты падают

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)
100 changes: 100 additions & 0 deletions lab12/Kabanova/bst_tree.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

у этого генератора нет завершения

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
42 changes: 42 additions & 0 deletions lab13/Kabanova/splay_tests.py
Original file line number Diff line number Diff line change
@@ -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])
Loading