From 2abe1b005a66b6884b44872054206c06e5584067 Mon Sep 17 00:00:00 2001 From: yurtaevnikita Date: Sat, 14 Nov 2015 08:49:08 +0300 Subject: [PATCH 1/5] Create lab5.py --- lab5/Yurtaev/lab5.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lab5/Yurtaev/lab5.py diff --git a/lab5/Yurtaev/lab5.py b/lab5/Yurtaev/lab5.py new file mode 100644 index 0000000..342244f --- /dev/null +++ b/lab5/Yurtaev/lab5.py @@ -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); + +array = [int(i) for i in stdin.readline().split()]; +array = quicksort(array); +print array; From 199b8e12ad0130449ae91c6f37308e3c876a690c Mon Sep 17 00:00:00 2001 From: yurtaevnikita Date: Sat, 14 Nov 2015 08:50:46 +0300 Subject: [PATCH 2/5] Create radix_sort.py --- lab6/Yurtaev/radix_sort.py | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lab6/Yurtaev/radix_sort.py diff --git a/lab6/Yurtaev/radix_sort.py b/lab6/Yurtaev/radix_sort.py new file mode 100644 index 0000000..cf3120d --- /dev/null +++ b/lab6/Yurtaev/radix_sort.py @@ -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; From 80665c54ab0561e135c2347990df4a30d86ab66a Mon Sep 17 00:00:00 2001 From: yurtaevnikita Date: Sat, 14 Nov 2015 08:51:27 +0300 Subject: [PATCH 3/5] Create test_sorting.py --- lab6/Yurtaev/test_sorting.py | 103 +++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 lab6/Yurtaev/test_sorting.py diff --git a/lab6/Yurtaev/test_sorting.py b/lab6/Yurtaev/test_sorting.py new file mode 100644 index 0000000..f310d58 --- /dev/null +++ b/lab6/Yurtaev/test_sorting.py @@ -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)) From c0c727c16c9dde7109405348c10a599c9ba5c96d Mon Sep 17 00:00:00 2001 From: yurtaevnikita Date: Sat, 14 Nov 2015 08:52:39 +0300 Subject: [PATCH 4/5] Create lab11.py --- lab11/Yurtaev/lab11.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lab11/Yurtaev/lab11.py diff --git a/lab11/Yurtaev/lab11.py b/lab11/Yurtaev/lab11.py new file mode 100644 index 0000000..c3ec4e2 --- /dev/null +++ b/lab11/Yurtaev/lab11.py @@ -0,0 +1,23 @@ +def solve(list): + max_h = int(0); + 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; From 1aeef0528bdf04fa2412020da5ff22ad8433427e Mon Sep 17 00:00:00 2001 From: yurtaevnikita Date: Sat, 14 Nov 2015 08:53:14 +0300 Subject: [PATCH 5/5] Create lab11_tests.py --- lab11/Yurtaev/lab11_tests.py | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 lab11/Yurtaev/lab11_tests.py diff --git a/lab11/Yurtaev/lab11_tests.py b/lab11/Yurtaev/lab11_tests.py new file mode 100644 index 0000000..00a3fc2 --- /dev/null +++ b/lab11/Yurtaev/lab11_tests.py @@ -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) + 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)