Skip to content

Commit 7538ade

Browse files
authored
Merge pull request #48 from lhchavez/unittest
This change standardizes all Python files so that they can run using the unittest module.
2 parents e74872a + 72cc532 commit 7538ade

File tree

86 files changed

+639
-448
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+639
-448
lines changed

Python/chapter01/1.1 - Is Unique/camilo_sol.py renamed to Python/chapter01/1.1 - Is Unique/camilosalazar98.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
characters. What if you cannot use additional data structures?
44
"""
55

6+
import unittest
7+
8+
#This soluation is O(n^2)
69

710
def isUnique(string_):
811
for i in range(len(string_)):
@@ -12,8 +15,6 @@ def isUnique(string_):
1215
return False
1316
return True
1417

15-
print(isUnique("GeeksforGeeks"))
16-
#This soluation is O(n^2)
1718

1819
#let's see if we can go faster
1920

@@ -30,4 +31,14 @@ def isUnique_(string_):
3031
return False
3132
return True
3233

33-
print(isUnique_("camilo"))
34+
35+
class Test(unittest.TestCase):
36+
def test_quadradic(self):
37+
self.assertFalse(isUnique("GeeksforGeeks"))
38+
39+
def test_linear(self):
40+
self.assertTrue(isUnique_("camilo"))
41+
42+
43+
if __name__ == '__main__':
44+
unittest.main()

Python/chapter01/1.1 - Is Unique/Q1.1.py renamed to Python/chapter01/1.1 - Is Unique/ismael_duran.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import unittest
12

23
# Note that this solution assumes a unique string
34
# implies a lowercase letter and the uppercase of
@@ -15,13 +16,18 @@ def uniqueChar(passedStr):
1516

1617
# If the string had unique characters the string
1718
# length remains the same
18-
# If the string has duplicates then size
19+
# If the string has duplicates then size
1920
if len(deleteDuplicateIfAny) == len(passedStr):
2021
return "Unique"
2122
else:
2223
return "Not Unique"
2324

2425

25-
example = "Helloh"
26+
class Test(unittest.TestCase):
27+
def test_uniqueChar(self):
28+
example = "Helloh"
29+
self.assertEqual(uniqueChar(example), "Not Unique")
2630

27-
print(uniqueChar(example))
31+
32+
if __name__ == '__main__':
33+
unittest.main()

Python/chapter01/1.1 - Is Unique/lomeli_1.1.py renamed to Python/chapter01/1.1 - Is Unique/lomeli_noe.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# are 128 unique values.
66

77
import array
8+
import unittest
89

910
def isUnique(str):
1011

@@ -26,5 +27,11 @@ def isUnique(str):
2627
return True
2728

2829

29-
print("%s" % (isUnique("test")))
30-
print("%s" % (isUnique("abc123")))
30+
class Test(unittest.TestCase):
31+
def test_is_unique(self):
32+
self.assertFalse(isUnique("test"))
33+
self.assertTrue(isUnique("abc123"))
34+
35+
36+
if __name__ == '__main__':
37+
unittest.main()

Python/chapter01/1.1 - Is Unique/solution.py

Whitespace-only changes.

Python/chapter01/1.2 - Check Perm/Q1.2.py

-18
This file was deleted.

Python/chapter01/1.2 - Check Perm/camilo_sol_1.2.py renamed to Python/chapter01/1.2 - Check Perm/camilosalazar98.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
one is a permutation of the other.
44
"""
55

6+
import unittest
67

78

8-
9+
#O(n) sol?
910
def perm_check(str_1,str_2):
1011
if len(str_1) != len(str_2):#If both str are diiferent lenghts return false
1112
return False
@@ -29,9 +30,12 @@ def perm_check(str_1,str_2):
2930
return True
3031

3132

32-
#O(n) sol?
33-
print(perm_check("camilo","pop")) #False
33+
class Test(unittest.TestCase):
34+
def test_perm_check(self):
35+
self.assertFalse(perm_check("camilo","pop"))
36+
self.assertFalse(perm_check("camilo","camplo"))
37+
self.assertTrue(perm_check("camilo","olimac"))
3438

35-
print(perm_check("camilo","camplo")) #false
3639

37-
print(perm_check("camilo","olimac")) #True
40+
if __name__ == '__main__':
41+
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
3+
4+
def check_perm(example, example2):
5+
for letter in example:
6+
if letter in example2:
7+
index = example2.index(letter)
8+
example2 = example2[:index] + example2[index+1:]
9+
10+
if example2 == '':
11+
return True
12+
else:
13+
continue
14+
else:
15+
return False
16+
17+
18+
class Test(unittest.TestCase):
19+
def test_perm_check(self):
20+
self.assertFalse(check_perm("hello", "ehloe"))
21+
22+
23+
if __name__ == '__main__':
24+
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Given two strings, write a method to decide if one is a permutation of the other
22

3+
import unittest
4+
35
def isPermutation(str1, str2):
46
if len(str1) != len(str2):
57
return False
@@ -9,5 +11,12 @@ def isPermutation(str1, str2):
911

1012
return sort1 == sort2
1113

12-
print("%s" % (isPermutation("stop", "pots")))
13-
print("%s" % (isPermutation("stops", "ponds")))
14+
15+
class Test(unittest.TestCase):
16+
def test_perm_check(self):
17+
self.assertTrue(isPermutation("stop", "pots"))
18+
self.assertFalse(isPermutation("stops", "ponds"))
19+
20+
21+
if __name__ == '__main__':
22+
unittest.main()

Python/chapter01/1.2 - Check Perm/solution.py

Whitespace-only changes.

Python/chapter01/1.3 - URLify/camilo_solution_1.3.py renamed to Python/chapter01/1.3 - URLify/camilosalazar98.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
Input: "Mr John Smith ", 13 Output: "Mr%20John%20Smith"
99
"""
1010

11+
import unittest
12+
13+
1114
def urlify(str):
1215
len_ = len(str) #gets the lenght of str
1316
new_str = ""#build a new string
@@ -22,4 +25,11 @@ def urlify(str):
2225

2326
return new_str
2427

25-
print(urlify("MY house is on fire "))
28+
class Test(unittest.TestCase):
29+
def test_perm_check(self):
30+
self.assertEqual(urlify("MY house is on fire "),
31+
"MY%20house%20is%20on%20fire")
32+
33+
34+
if __name__ == '__main__':
35+
unittest.main()

Python/chapter01/1.3 - URLify/Q1.3.py renamed to Python/chapter01/1.3 - URLify/ismael_duran.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import unittest
12

23

34
def replaceSpace(stringPas, totalLen):
@@ -13,4 +14,10 @@ def replaceSpace(stringPas, totalLen):
1314
else:
1415
return newString
1516

16-
print(replaceSpace("Hi, how is your ",18))
17+
class Test(unittest.TestCase):
18+
def test_perm_check(self):
19+
self.assertEqual(replaceSpace("Hi, how is your ",18), "Fail")
20+
21+
22+
if __name__ == '__main__':
23+
unittest.main()

Python/chapter01/1.4 - PalinPerm/camilo_solution_1.4.py renamed to Python/chapter01/1.4 - PalinPerm/camilosalazar98.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
Output: True (permutations: "taco cat", "atco eta", etc.)
99
"""
1010

11+
import unittest
12+
13+
1114
def PalinPerm(str):
1215
d = {}
1316
str = str.replace(' ','').lower()#Take out the spaces
@@ -24,5 +27,11 @@ def PalinPerm(str):
2427
return False
2528
return True
2629

27-
print(PalinPerm('taco cat'))#True
28-
print(PalinPerm('taco catt'))#False
30+
class Test(unittest.TestCase):
31+
def test_palin_perm(self):
32+
self.assertTrue(PalinPerm('taco cat'))
33+
self.assertFalse(PalinPerm('taco catt'))
34+
35+
36+
if __name__ == '__main__':
37+
unittest.main()

Python/chapter01/1.4 - PalinPerm/solution.py

Whitespace-only changes.

Python/chapter01/1.5 - OneAway/camilo_solution_1.5.py renamed to Python/chapter01/1.5 - OneAway/camilosalazar98.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
Hints:#23, #97, #130
1111
"""
1212

13+
import unittest
14+
15+
1316
def oneAway(str1,str2):
1417
str1 = str1.lower()
1518
str2 = str2.lower()
@@ -47,7 +50,7 @@ def oneAway(str1,str2):
4750
replacing two letter will make v's count more onces
4851
4952
You'll see this when you print k and v
50-
53+
5154
"""
5255
for k,v in d.items():
5356
#print(k,v)
@@ -61,15 +64,15 @@ def oneAway(str1,str2):
6164
return False
6265

6366

67+
class Test(unittest.TestCase):
68+
def test_positive(self):
69+
self.assertTrue(oneAway('pale', 'ple'))
70+
self.assertTrue(oneAway('pales', 'pale'))
71+
self.assertTrue(oneAway('pale', 'bale'))
6472

73+
def test_negative(self):
74+
self.assertFalse(oneAway('pale', 'bake'))
6575

6676

67-
68-
69-
70-
71-
print(oneAway('pale', 'ple'))#True
72-
print(oneAway('pales', 'pale'))#True
73-
print(oneAway('pale', 'bale'))#True
74-
75-
print(oneAway('pale', 'bake'))#False
77+
if __name__ == '__main__':
78+
unittest.main()

Python/chapter01/1.5 - OneAway/solution.py

Whitespace-only changes.

Python/chapter01/1.6 - String Compression/camilo_solution_1.6.py renamed to Python/chapter01/1.6 - String Compression/camilosalazar98.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
letters (a - z).
88
"""
99

10+
import unittest
1011

1112

1213
def string_compression(str1):
@@ -27,10 +28,9 @@ def string_compression(str1):
2728
return str1
2829

2930

31+
class Test(unittest.TestCase):
32+
def test1(self):
33+
self.assertEqual(string_compression('aabcccccaaa'), 'a2b1c5a3')
3034

31-
32-
33-
34-
35-
36-
print(string_compression('aabcccccaaa'))
35+
if __name__ == '__main__':
36+
unittest.main()

Python/chapter01/1.6 - String Compression/solution.py

Whitespace-only changes.

Python/chapter01/1.7 - Rotate Matrix/camilo_soluation_1.7.py renamed to Python/chapter01/1.7 - Rotate Matrix/camilosalazar98.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
77
"""
88

9+
import unittest
10+
911

1012
def rotate_matrix(matrix):
1113
matrix.reverse()# we frist reverse the matrix then we take its transpose
@@ -19,9 +21,12 @@ def rotate_matrix(matrix):
1921
matrix[j][i] = temp
2022

2123

22-
matrix = [[1,2,3],[4,5,6],[7,8,9]]
23-
rotate_matrix(matrix)
24-
#[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
24+
class Test(unittest.TestCase):
25+
def test_rotate_matrix(self):
26+
matrix = [[1,2,3],[4,5,6],[7,8,9]]
27+
rotate_matrix(matrix)
28+
self.assertEqual(matrix, [[7, 4, 1], [8, 5, 2], [9, 6, 3]])
2529

2630

27-
print(matrix)
31+
if __name__ =='__main__':
32+
unittest.main()

Python/chapter01/1.7 - Rotate Matrix/Nick.py renamed to Python/chapter01/1.7 - Rotate Matrix/nickolasteixeira.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/python3
2+
23
import random
3-
from pprint import pprint
4+
import unittest
5+
46

57
def build_matrix(w, h, max=10):
68
'''function that builds a matrix with random numbers including 0'''
@@ -38,8 +40,23 @@ def rotate_matrix(matrix):
3840
# top right becomes top left
3941
matrix[i][last] = top
4042

43+
44+
class Test(unittest.TestCase):
45+
def test_rotate_matrix(self):
46+
matrix = [[8, 0, 5, 6, 8],
47+
[3, 4, 6, 6, 8],
48+
[3, 2, 10, 3, 0],
49+
[4, 3, 9, 0, 2],
50+
[9, 5, 4, 2, 10]]
51+
rotate_matrix(matrix)
52+
self.assertEqual(
53+
matrix,
54+
[[9, 4, 3, 3, 8],
55+
[5, 3, 2, 4, 0],
56+
[4, 9, 10, 6, 5],
57+
[2, 0, 3, 6, 6],
58+
[10, 2, 0, 8, 8]])
59+
60+
4161
if __name__ =='__main__':
42-
matrix = build_matrix(5, 5)
43-
pprint(matrix)
44-
rotate_matrix(matrix)
45-
pprint(matrix)
62+
unittest.main()

Python/chapter01/1.7 - Rotate Matrix/solution.py

Whitespace-only changes.

0 commit comments

Comments
 (0)