summaryrefslogtreecommitdiff
path: root/tests/test_hacker_rank_leet_code.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_hacker_rank_leet_code.py')
-rw-r--r--tests/test_hacker_rank_leet_code.py212
1 files changed, 212 insertions, 0 deletions
diff --git a/tests/test_hacker_rank_leet_code.py b/tests/test_hacker_rank_leet_code.py
new file mode 100644
index 0000000..0e6b4a6
--- /dev/null
+++ b/tests/test_hacker_rank_leet_code.py
@@ -0,0 +1,212 @@
+import unittest
+from sources.hacker_rank_leet_code import is_leap, \
+ runner_up, second_lowest, average_mark, diagnoal_diff, \
+ minion_game, substring_count, top_k_words_692, reorder_data_in_log_937, \
+ run_length, binary_search, find_boundary, first_not_smaller, \
+ find_first_occurrence, square_root, find_min_rotated, \
+ peak_of_mountain_array, min_max_weight, countAnalogusArrays, \
+ minimalHeaviestSetA, getNumberOfOptions, howManySwaps, storage
+
+
+class MyTestCase(unittest.TestCase):
+ def test_is_leap(self):
+ self.assertEqual(False, is_leap(1990))
+ self.assertEqual(True, is_leap(2000))
+ self.assertEqual(True, is_leap(2400))
+ self.assertEqual(False, is_leap(1800))
+ self.assertEqual(False, is_leap(1900))
+ self.assertEqual(False, is_leap(2100))
+ self.assertEqual(False, is_leap(2200))
+ self.assertEqual(False, is_leap(2300))
+ self.assertEqual(False, is_leap(2500))
+
+ def test_runner_up(self):
+ arr = [2, 3, 6, 6, 5]
+ self.assertEqual(5, runner_up(arr))
+ self.assertEqual(3, runner_up(arr, True))
+
+ def test_second_lowest(self):
+ # students = [["Harry", 37.21], ["Berry", 37.21], ["Tina", 37.2], ["Akriti", 41], ["Harsh", 39]]
+ students = [[]] * 5
+ students[0] = ["Harry", 37.21]
+ students[1] = ["Berry", 37.21]
+ students[2] = ["Tina", 37.2]
+ students[3] = ["Akriti", 41]
+ students[4] = ["Harsh", 39]
+ self.assertEqual('Berry', second_lowest(students)[0])
+
+ def test_average_mark(self):
+ student_marks = {}
+ student_marks['Krishna'] = [67, 68, 69]
+ student_marks['Arjun'] = [70, 98, 63]
+ student_marks['Malika'] = [52, 56, 60]
+ self.assertEqual(56, average_mark(student_marks, 'Malika'))
+ student_marks.clear()
+ student_marks['Harsh'] = [25, 26.5, 28]
+ student_marks['Malika'] = [26, 28, 30]
+ self.assertEqual(26.5, average_mark(student_marks, 'Harsh'))
+
+ def test_diagnoal_diff(self):
+ square_matrix = [[]] * 3
+ square_matrix[0] = [1, 2, 3]
+ square_matrix[1] = [4, 5, 6]
+ square_matrix[2] = [9, 8, 9]
+ self.assertEqual(2, diagnoal_diff(square_matrix))
+ square_matrix = [[]] * 3
+ square_matrix[0] = [11, 2, 4]
+ square_matrix[1] = [4, 5, 6]
+ square_matrix[2] = [10, 8, -12]
+ self.assertEqual(15, diagnoal_diff(square_matrix))
+
+ def test_substring_count(self):
+ s = "banana"
+ subs = "ana"
+ self.assertEqual(2, substring_count(s, subs))
+ subs = "a"
+ self.assertEqual(3, substring_count(s, subs))
+
+ def test_minion_game(self):
+ s = "BANANA"
+ self.assertEqual("Stuart 12", minion_game(s))
+
+ def test_top_k_words_692(self):
+ words = ["i", "love", "leetcode", "i", "love", "coding"]
+ self.assertEqual("i", top_k_words_692(words, 2)[0])
+ self.assertEqual("love", top_k_words_692(words, 2)[1])
+
+ words = ["i", "love", "leetcode", "i", "love", "coding"]
+ self.assertEqual("i", top_k_words_692(words, 3)[0])
+ self.assertEqual("love", top_k_words_692(words, 3)[1])
+ self.assertEqual("coding", top_k_words_692(words, 3)[2])
+
+ words = ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"]
+ self.assertEqual("the", top_k_words_692(words, 4)[0])
+ self.assertEqual("is", top_k_words_692(words, 4)[1])
+ self.assertEqual("sunny", top_k_words_692(words, 4)[2])
+ self.assertEqual("day", top_k_words_692(words, 4)[3])
+
+ words = ["aaa", "aa", "a"]
+ self.assertEqual("a", top_k_words_692(words, 1)[0])
+
+ def test_reorder_data_in_log_937(self):
+ logs = ["dig1 8 1 5 1", "let1 art can", "dig2 3 6", "let2 own kit dig", "let3 art zero"]
+ self.assertEqual(["let1 art can", "let3 art zero", "let2 own kit dig", "dig1 8 1 5 1", "dig2 3 6"],
+ reorder_data_in_log_937(logs))
+
+ logs = ["a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo"]
+ self.assertEqual(["g1 act car", "a8 act zoo", "ab1 off key dog", "a1 9 2 3 1", "zo4 4 7"],
+ reorder_data_in_log_937(logs))
+
+ logs = ["a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo", "a2 act car"]
+ self.assertEqual(["a2 act car", "g1 act car", "a8 act zoo", "ab1 off key dog", "a1 9 2 3 1", "zo4 4 7"],
+ reorder_data_in_log_937(logs))
+
+ def test_run_length(self):
+ input = "aaaabbccc"
+ self.assertEqual("4a2b3c", run_length(input))
+ input = "aaaabbccca"
+ self.assertEqual("4a2b3c1a", run_length(input))
+ input = "abcd"
+ self.assertEqual("1a1b1c1d", run_length(input))
+ input = ""
+ self.assertEqual("", run_length(input))
+ input = "a"
+ self.assertEqual("1a", run_length(input))
+
+ def test_binary_search(self):
+ input = [1, 2, 3, 4, 5, 6]
+ self.assertEqual(2, binary_search(input, 3))
+ input = [1, 2, 3, 4, 5, 6]
+ self.assertEqual(-1, binary_search(input, 7))
+ input = [1]
+ self.assertEqual(-1, binary_search(input, 7))
+ input = [1]
+ self.assertEqual(0, binary_search(input, 1))
+ input = [1, 3, 5, 7, 8]
+ self.assertEqual(2, binary_search(input, 5))
+
+ def test_find_boundary(self):
+ input = [False, False, False, True, True, True]
+ self.assertEqual(3, find_boundary(input))
+ input = [False]
+ self.assertEqual(-1, find_boundary(input))
+ input = [False, False, True, True, True]
+ self.assertEqual(2, find_boundary(input))
+ input = [True]
+ self.assertEqual(0, find_boundary(input))
+ input = [True, True, True, True, True]
+ self.assertEqual(0, find_boundary(input))
+
+ def test_first_not_smaller(self):
+ input = [1, 3, 3, 5, 8, 8, 10]
+ target = 2
+ self.assertEqual(1, first_not_smaller(input, target))
+ input = [2, 3, 5, 7, 11, 13, 17, 19]
+ target = 6
+ self.assertEqual(3, first_not_smaller(input, target))
+
+ def test_find_first_occurence(self):
+ input = [1, 3, 3, 3, 3, 6, 10, 10, 10, 100]
+ target = 3
+ self.assertEqual(1, find_first_occurrence(input, target))
+ input = [2, 3, 5, 7, 11, 13, 17, 19]
+ target = 6
+ self.assertEqual(-1, find_first_occurrence(input, target))
+
+ def test_square_root(self):
+ input = 16
+ self.assertEqual(4, square_root(input))
+ input = 8
+ self.assertEqual(2, square_root(input))
+ input = 25
+ self.assertEqual(5, square_root(input))
+ input = 10
+ self.assertEqual(3, square_root(input))
+
+ def test_find_min_rotated(self):
+ input = [30, 40, 50, 10, 20]
+ self.assertEqual(3, find_min_rotated(input))
+ input = [3, 5, 7, 11, 13, 17, 19, 2]
+ self.assertEqual(7, find_min_rotated(input))
+
+ def test_peak_of_mountain_array(self):
+ input = [0, 1, 2, 3, 2, 1, 0]
+ self.assertEqual(3, peak_of_mountain_array(input))
+
+ def test_peak_min_max_weight(self):
+ weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ d = 5
+ self.assertEqual(15, min_max_weight(weights, d))
+
+ def test_countAnalogusArrays(self):
+ consecutiveDifference = [-2, -1, -2, 5]
+ lowerBound = 3
+ upperBound = 10
+ self.assertEqual(3, countAnalogusArrays(consecutiveDifference, lowerBound, upperBound))
+
+ def test_countAnalogusArrays(self):
+ weights = [3,7,5,6,2]
+ self.assertEqual([6, 7], minimalHeaviestSetA(weights))
+
+ def test_getNumberOfOptions(self):
+ priceOfJeans = [2,3]
+ priceOfShoes = [4]
+ priceOfSkirts = [2,3]
+ priceOfTops = [1,2]
+ dollars = 10
+ self.assertEqual(4, getNumberOfOptions(priceOfJeans, priceOfShoes,priceOfSkirts,priceOfTops,dollars))
+
+ def test_howManySwaps(self):
+ arr = [5,1,4,2]
+ self.assertEqual(4, howManySwaps(arr))
+
+ def test_storage(self):
+ n = 6
+ m = 6
+ h = [4]
+ v = [2]
+ self.assertEqual(4, storage(n, m, h, v))
+
+
+if __name__ == '__main__':
+ unittest.main()