summaryrefslogtreecommitdiff
path: root/tests/test_hacker_rank_leet_code.py
blob: 0e6b4a69fcc51326f49dbda5819a950eabc1d376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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()