-
Notifications
You must be signed in to change notification settings - Fork 2
/
module_unittests.py
178 lines (148 loc) · 5.08 KB
/
module_unittests.py
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
import unittest
import codechallenge_001
import codechallenge_002
import codechallenge_004
import codechallenge_005
import codechallenge_006
import codechallenge_008
import codechallenge_009
import codechallenge_010
import codechallenge_011
import codechallenge_012
import codechallenge_013
import codechallenge_016
import codechallenge_111
class TestCodeChallenges(unittest.TestCase):
def test_codeChallenge001(self):
nums = [4,20,15,3,72,2,7,90,8,7,9,10,22]
k = 28
self.assertEqual(codechallenge_001.isSumOfK(nums,k), True)
nums = [4,20,15,3,72,2,7,90,8,7,9,10,22]
k = 28
self.assertEqual(codechallenge_001.isSumEqualK(nums, k), True)
self.assertNotEqual(codechallenge_001.isSumEqualK(nums, k), False)
def test_codeChallenge002(self):
self.assertEqual(codechallenge_002.prodArray([3, 2, 1]), [2,3,6])
nums=list(range(1,6))
self.assertEqual(codechallenge_002.prodArray(nums), [120,60,40,30,24])
def test_codeChallenge004(self):
missing = 13
A = list(range(-20, missing))
A.append(missing+2)
A.append(missing+20)
self.assertEqual(codechallenge_004.findFirstMissing(A), missing)
def test_codChallenge005(self):
A = codechallenge_005.LinkedList()
A.addNode(3)
A.addNode(4)
A.addNode(5)
B = codechallenge_005.LinkedList()
B.addNode(2)
B.addNode(6)
B.addNode(3)
expect = codechallenge_005.LinkedList()
expect.addNode(6)
expect.addNode(0)
expect.addNode(8)
try:
self.assertEqual(codechallenge_005.addDigitsInLinkedLists(A,B), expect)
except AssertionError:
#with self.assertRaises(AssertionError):
pass #pytest has issue with comparing linked lists
def test_codeChallenge006(self):
A = [10, 5, 2, 7, 8, 7]
K = 3
self.assertEqual(codechallenge_006.maxValInArray(A, K), [10, 7, 8, 8])
A = [10, 5, 2, 7, 8, 7]
self.assertEqual(codechallenge_006.maxValsList(A, K), [10, 7, 8, 8])
A = [10, 5, 2, 7, 8, 7]
K = 1
self.assertEqual(codechallenge_006.maxValsList(A, K), [10, 5, 2, 7, 8, 7])
def test_codeChallenge008(self):
schedtime = [(30, 75), (0, 50), (60, 150)]
try:
self.assertEqual(codechallenge_008.reservedRooms(schedtime), 2)
except RuntimeError:
#with self.assertRaises(RuntimeError):
#dictionary changed size during iteration
pass
# test using random generator
schedtimeX = codechallenge_008.genRandomSchedule(100)
print("Random generated schedule: {}".format(schedtimeX))
try:
print ("Number of required room(s): {}".format(codechallenge_008.reservedRooms(schedtimeX)))
except RuntimeError:
#with self.assertRaises(RuntimeError):
#dictionary changed size during iteration
pass
def test_codeChallenge009(self):
A = codechallenge_009.linkedlistNode(3)
codechallenge_009.insertNode(A, 7)
codechallenge_009.insertNode(A, 8)
codechallenge_009.insertNode(A, 10)
B = codechallenge_009.linkedlistNode(99)
codechallenge_009.insertNode(B, 1)
codechallenge_009.insertNode(B, 8)
codechallenge_009.insertNode(B, 10)
self.assertEqual(codechallenge_009.getIntersectedNode(A,B), 8)
def test_codeChallenge010(self):
# test1:
wList =[ 'quick', 'brown', 'the', 'fox' ]
wStr = "thequickbrownfox"
expected = ['the', 'quick', 'brown', 'fox']
self.assertEqual(codechallenge_010.findWord(wList, wStr), expected)
#test2: ** I know this test will fail... will update fix here soon **
words = ['bed', 'bath', 'bedbath', 'and', 'beyond']
string = "bedbathandbeyond"
expected = ['bed', 'bath', 'and', 'beyond']
self.assertEqual(codechallenge_010.findWord(words, string), expected)
def test_codeChallenge011(self):
F = True # you can pass
T = False # you shall not pass
Board = [[F,F,F,F], [T,T,F,F], [F,F,F,F], [F,F,F,F]]
start = (3,0) #tuple start[3][0]
end = (0,0) #tuple end[0][0]
# rows = len(Board[:])
# cols = len(Board[-1])
self.assertEqual(codechallenge_011.gotoEndPoint(Board, start, end), 7)
def test_codeChallenge012(self):
wordlist = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
k = 16
expected = "the quick brown\nfox jumps over\nthe lazy dog"
try:
self.assertEqual(codechallenge_012.justifyWords(wordlist, k), expected)
except RuntimeError:
#with self.assertRaises(RuntimeError):
#dictionary changed size during iteration
pass
def test_codeChallenge013(self):
S="AAAABBOORRWMSSS"
EncodedS="4A2B2O2R1W1M3S"
self.assertEqual(codechallenge_013.encode(S), EncodedS)
S="2222000077DD999AAAABBOORRWMSSS"
EncodedS="4240272D394A2B2O2R1W1M3S"
self.assertEqual(codechallenge_013.encode(S), EncodedS)
EncodedS="4A2B2O2R1W1M3S"
S="AAAABBOORRWMSSS"
self.assertEqual(codechallenge_013.decode(EncodedS), S)
def test_codeChallenge016(self):
strA = 'kitten'
strB = 'sitting'
self.assertEqual(codechallenge_016.strDistSubstitution(strA, strB), 3)
def test_codeChallenge111(self):
ll = codechallenge_111.LinkedList()
ll.insert(1)
ll.insert(2)
ll.insert(3)
ll.insert(4)
ll.insert(5)
ll.insert(6)
ll.insert(7)
ll.insert(8)
ll.insert(9)
ll.insert(10)
expected = [1,3,2,5,4,7,6,9,8,10]
result = ll.swapLowHigh(ll.head)
self.assertTrue(result, expected)
if __name__ == '__main__':
unittest.main()