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

Юртаев 5,6,11 #82

Open
wants to merge 5 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
23 changes: 23 additions & 0 deletions lab11/Yurtaev/lab11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def solve(list):
max_h = int(0);
Copy link
Owner

Choose a reason for hiding this comment

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

а зачем вам явные касты к инту? польза разве что для читаемости, но так писать не принято.

max_id = int(-1);
for i in range(0, len(list)):
if (max_h < list[i]):
max_h = list[i];
max_id = i;
curr_max = int(-1);
ans = int(0);
for i in range(0, max_id):
if (list[i] < curr_max):
ans += curr_max - list[i];
else:
curr_max = list[i];
curr_max = int(-1);
i = len(list) - 1;
while (i > max_id):
if (list[i] < curr_max):
ans += curr_max - list[i];
else:
curr_max = list[i];
i -= 1;
return ans;
70 changes: 70 additions & 0 deletions lab11/Yurtaev/lab11_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import unittest
import random
from lab11 import solve

class TestLab11(unittest.TestCase):

def test_trivial_1(self):
arr = [2, 1, 2]
res = solve(arr)
expected = int(1)
self.assertEqual(expected, res)

def test_trivial_2(self):
arr = [1, 2, 2, 1, 5, 1, 2, 2]
res = solve(arr)
expected = int(2)
self.assertEqual(expected, res)

def test_trivial_3(self):
arr = [1, 2, 2, 1, 1, 1, 2, 5]
res = solve(arr)
expected = int(3)
self.assertEqual(expected, res)

def test_trivial_4(self):
arr = [3, 4, 2, 5, 1, 5, 2, 2]
res = solve(arr)
expected = int(6)
self.assertEqual(expected, res)

def test_trivial_5(self):
arr = [3, 4, 2, 5, 1, 5, 2, 2]
res = solve(arr)
expected = int(6)
Copy link
Owner

Choose a reason for hiding this comment

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

здесь ведь должно быть 4, между двумя 5
кажется, что вы неправильно считаете крайние элементы -- у вас "стенки"-границы массива имеют высоту, но это не так. запустите тест из задания, он у вас должен не пройти

self.assertEqual(expected, res)

def test_empty(self):
arr = []
res = solve(arr)
expected = 0
self.assertEqual(expected, res)

def test_equal(self):
arr = [int(99999999) for i in range(0, 10000)]
res = solve(arr)
expexted = 0
self.assertEqual(expexted, res)

def test_nondecreasing(self):
arr = [1]
for i in range(1, 10000):
arr.append(arr[i - 1] + int(random.randint(0, 1000)))
res = solve(arr)
expexted = 0
self.assertEqual(expexted, res)

def test_nonincreasing(self):
arr = [1]
for i in range(1, 10):
arr.append(arr[i - 1] + int(random.randint(0, 10)))
new_arr = [i for i in reversed(arr)]
res = solve(new_arr)
expexted = 0
self.assertEqual(expexted, res)

def test_not_trivial(self):
arr = [1, 2, 3, 1, 4, 3, 2, 1, 2, 3, 4, 1, 2, 3, 2]
res = solve(arr)
expected = int(14)
self.assertEqual(expected, res)
15 changes: 15 additions & 0 deletions lab5/Yurtaev/lab5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from sys import stdin
import random

def quicksort(array):
if len(array) <= 1:
return array;
partition_number = random.choice(array);
mid = [i for i in array if i == partition_number];
left = [i for i in array if i < partition_number];
right = [i for i in array if i > partition_number];
return quicksort(left) + mid + quicksort(right);
Copy link
Owner

Choose a reason for hiding this comment

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

элегантно, но qs нужно делать in-place, это большое преимущество, которое нельзя игнорировать.


array = [int(i) for i in stdin.readline().split()];
array = quicksort(array);
print array;
40 changes: 40 additions & 0 deletions lab6/Yurtaev/radix_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def get_max_dig(array):
max_dig = int(0);
for i in array:
curr_dig = int(0);
i = abs(i);
while i != 0:
curr_dig += 1;
i /= 10;
max_dig = max(max_dig, curr_dig);
return max_dig;

def get_dig(x, i):
sign = int(1);
if (x < 0):
sign *= -1;
x = abs(x);
for j in range(0, i):
x /= 10;
return sign * (x % 10);

def radix_sort(array):
max_dig = get_max_dig(array);
new_list = [[] for k in range(0, 19)];
for i in range(0, max_dig):
list = new_list;
for j in range(0, len(array)):
dig = get_dig(array[j], i);
id = int(0);
if (dig == 0):
id = 9;
if (dig < 0):
id = 9 - abs(dig);
if (dig > 0):
id = dig + 9;
list[id].append(array[j]);
array = [];
for j in range(0, 19):
for k in range(0, len(list[j])):
array.append(list[j][k]);
return array;
103 changes: 103 additions & 0 deletions lab6/Yurtaev/test_sorting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import unittest
import random
from radix_sort import radix_sort

class TestRadixSort(unittest.TestCase):
def check_sorted(self, array):
if len(array) == 0:
return True
prev = array[0]
for i in array:
if i < prev:
return False
prev = i
return True

def test_empty(self):
arr = []
res = radix_sort(arr)
expected = []
self.assertEqual(expected, res)

def test_trivial(self):
arr = [1]
res = radix_sort(arr)
expected = [1]
self.assertFalse(not res)
self.assertEqual(expected, res)

def test_trivial_2(self):
arr = [123456789]
res = radix_sort(arr)
expected = [123456789]
self.assertFalse(not res)
self.assertEqual(expected, res)

def test_trivial_3(self):
arr = [0]
res = radix_sort(arr)
expected = [0]
self.assertFalse(not res)
self.assertEqual(expected, res)

def test_equal_length_ints(self):
arr = [random.randint(1000, 9999) for i in range(1000)]
res = radix_sort(arr)
self.assertTrue(self.check_sorted(res))
self.assertEqual(len(arr), len(res))

def test_different_length_ints(self):
arr = [random.randint(0, 1000000) for i in range(1000)]
res = radix_sort(arr)
self.assertTrue(self.check_sorted(res))
self.assertEqual(len(arr), len(res))

def test_long_array(self):
arr = [random.randint(0, 1000000) for i in range(200000)]
res = radix_sort(arr)
self.assertTrue(self.check_sorted(res))
self.assertEqual(len(arr), len(res))

def test_single_negative(self):
arr = [-1]
res = radix_sort(arr)
expected = [-1]
self.assertFalse(not res)
self.assertEqual(expected, res)
self.assertEqual(len(arr), len(res))

def test_multiple_negative(self):
arr = [random.randint(-1000000, -1) for i in range(1000)]
res = radix_sort(arr)
self.assertTrue(self.check_sorted(res))
self.assertEqual(len(arr), len(res))

def test_random_input(self):
arr = [random.randint(-1000000, 1000000) for i in range(100000)]
res = radix_sort(arr)
self.assertTrue(self.check_sorted(res))
self.assertEqual(len(arr), len(res))

def test_multiple_negative(self):
arr = [random.randint(-1000000, -1) for i in range(100000)]
res = radix_sort(arr)
self.assertTrue(self.check_sorted(res))
self.assertEqual(len(arr), len(res))

def test_random_input_1(self):
arr = [random.randint(-1000000, 1000000) for i in range(100000)]
res = radix_sort(arr)
self.assertTrue(self.check_sorted(res))
self.assertEqual(len(arr), len(res))

def test_random_input_2(self):
arr = [random.randint(-1000000, 1000000) for i in range(100000)]
res = radix_sort(arr)
self.assertTrue(self.check_sorted(res))
self.assertEqual(len(arr), len(res))

def test_random_input_3(self):
arr = [random.randint(-1000000, 1000000) for i in range(100000)]
res = radix_sort(arr)
self.assertTrue(self.check_sorted(res))
self.assertEqual(len(arr), len(res))