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

Васич - всё / {20} #83

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
1 change: 1 addition & 0 deletions lab1/Vasich/Hello world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
here we are
37 changes: 37 additions & 0 deletions lab10/Vasich/pythagorean_triples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from sys import stdin, stdout


def triples(seq):
# n log(n)
seq.sort()

arr = [seq[0] ** 2]

j = 0

# n
for i in range(1, len(seq)):
value = seq[i] ** 2
if arr[j] != value:
arr.append(value)
j += 1

if len(arr) < 3:
return 0

count = 0
# n^2 (3SUM)
for i in range(2, len(arr)):
start = 0
end = i - 1
while start < end:
if arr[start] + arr[end] == arr[i]:
count += 1
start += 1
end -= 1
elif arr[start] + arr[end] > arr[i]:
end -= 1
else:
start += 1

return count
49 changes: 49 additions & 0 deletions lab10/Vasich/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from lab10.Vasich.pythagorean_triples import triples
from random import randint

import unittest


class TestTriples(unittest.TestCase):
def _check(self, input_arr, out_value):
work_copy = [input_arr[i] ** 2 for i in range(len(input_arr))]
work_copy.sort()
arr = [work_copy[0]]

j = 0
for i in range(1, len(work_copy)):
value = work_copy[i]
if arr[j] != value:
arr.append(value)
j += 1

count = 0
for i in range(len(arr) - 1, -1, -1):
for j in range(i - 1, -1, -1):
for k in range(j - 1, -1, -1):
if arr[i] == arr[j] + arr[k]:
count += 1
return count == out_value

def test_egypt(self):
input_arr = [3, 4, 5]
self.assertEqual(1, triples(input_arr))

def test_manual1(self):
input_arr = [20, 21, 29, 12, 16, 3]
self.assertEqual(2, triples(input_arr))

def test_manual2(self):
input_arr = [23, 247, 19, 96, 264, 265, 132, 181]
self.assertEqual(2, triples(input_arr))

def test_manual3(self):
input_arr = [12, 15, 24, 27, 40, 1, 42]
self.assertEqual(0, triples(input_arr))

def test_random(self):
size = 500
input_arr = [0] * size
for i in range(size):
input_arr[i] = randint(0, 1000)
self.assertTrue(self._check(input_arr, triples(input_arr)))
24 changes: 24 additions & 0 deletions lab11/Vasich/bar_chart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def one_pass_sum(seq, indices):
max_sum = float("-inf")
curr_sum = 0
height = seq[indices[0]]

for i in indices[1:]:
if seq[i] < height:
curr_sum += height - seq[i]
else:
if curr_sum >= max_sum:
max_sum = curr_sum
height = seq[i]
curr_sum = 0
return max_sum


def chart_area(seq):
if len(seq) < 3:
return 0

forward = one_pass_sum(seq, range(len(seq)))
backward = one_pass_sum(seq, range(len(seq) - 1, -1, -1))

return max(forward, backward)
23 changes: 23 additions & 0 deletions lab11/Vasich/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from lab11.Vasich.bar_chart import chart_area

import unittest


class TestBarChart(unittest.TestCase):
def test_empty_chart(self):
self.assertEqual(0, chart_area([]))

def test_plane(self):
self.assertEqual(0, chart_area([2, 2, 2, 2, 2]))

def test_ascending(self):
self.assertEqual(0, chart_area([2, 3, 5, 8, 11]))

def test_descending(self):
self.assertEqual(0, chart_area([52, 43, 35, 28, 11]))

def test_castle_wall(self):
self.assertEqual(1, chart_area([1, 2, 1, 2, 1, 2, 1, 2]))

def test_mount_elbrus(self):
self.assertEqual(25, chart_area([1, 2, 4, 8, 16, 6, 6, 8, 15, 7, 6, 7, 0]))
76 changes: 76 additions & 0 deletions lab12/Vasich/tets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from lab12.Vasich.tree import UnbalancedBinarySearchTree
from random import randint
import unittest


class TestQueue(unittest.TestCase):
def _check(self, input_arr):
tree = UnbalancedBinarySearchTree()

for v in input_arr:
tree.add(v)

input_arr.sort()
i = 0
for p in tree:
if p != input_arr[i]:
return False
i += 1
return True

def test_empty(self):
self.assertTrue(self._check([]))

def test_ascending(self):
size = 100
input_arr = [0] * size
for i in range(1, size):
input_arr[i] = input_arr[i - 1] + randint(1, 10)
self.assertTrue(self._check(input_arr))

def test_repeating_ascending(self):
size = 100
input_arr = [0] * size
for i in range(size):
input_arr[i] = input_arr[i - 1] + randint(0, 4)
self.assertTrue(self._check(input_arr))

def test_descending(self):
size = 100
input_arr = [randint(0, size)] * size
for i in range(size):
input_arr[i] = input_arr[i - 1] - randint(1, 10)
self.assertTrue(self._check(input_arr))

def test_random(self):
size = 100
input_arr = [0] * size
for i in range(size):
input_arr[i] = randint(-1000, 1000)
self.assertTrue(self._check(input_arr))

def test_contains(self):
size = 100
input_arr = [0] * size
for i in range(size):
input_arr[i] = randint(-1000, 1000)

tree = UnbalancedBinarySearchTree()
for v in input_arr:
tree.add(v)

value = input_arr[randint(0, size)]
self.assertTrue(tree.contains(value))

def test_contains1(self):
size = 100
input_arr = [0] * size
for i in range(size):
input_arr[i] = randint(-1000, 1000)

tree = UnbalancedBinarySearchTree()
for v in input_arr:
tree.add(v)

value = 1001
self.assertFalse(tree.contains(value))
62 changes: 62 additions & 0 deletions lab12/Vasich/tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

class Set:
def add(self, value):
pass

def __iter__(self):
pass


class UnbalancedBinarySearchTree(Set):

def __init__(self, value=None, parent=None):
self.value = value
self.parent = parent
self.left = None
self.right = None

def add(self, value):
if self.parent is None:
self.parent = self
self.value = value
return self

curr = self
while True:
if value <= curr.value:
if curr.left:
curr = curr.left
else:
curr.left = UnbalancedBinarySearchTree(value, curr)
break
else:
if curr.right:
curr = curr.right
else:
curr.right = UnbalancedBinarySearchTree(value, curr)
break
return self

def contains(self, value):
curr = self
while curr:
if value == curr.value:
return True
curr = curr.left if value <= curr.value else curr.right
return False

def iterate(self):
if self.left:
yield from self.left
yield self.value
if self.right:
yield from self.right

def __iter__(self):
if self.parent is None:
return iter([])
return self.iterate()


if __name__ == "__main__":
pass
Loading