Skip to content

Commit d630624

Browse files
committed
Standardize python's unittests
This change makes all the Python solutions use unittest and run silently.
1 parent e74872a commit d630624

File tree

60 files changed

+625
-441
lines changed

Some content is hidden

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

60 files changed

+625
-441
lines changed

Python/chapter01/1.1 - Is Unique/Q1.1.py

Lines changed: 9 additions & 3 deletions
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/camilo_sol.py

Lines changed: 14 additions & 3 deletions
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/lomeli_1.1.py

Lines changed: 9 additions & 2 deletions
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.
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
example = "hello"
2-
example2 = "ehloe"
1+
import unittest
32

4-
for letter in example:
5-
if letter in example2:
3+
4+
def check_perm(example, example2):
5+
for letter in example:
6+
if letter in example2:
67
index = example2.index(letter)
78
example2 = example2[:index] + example2[index+1:]
8-
print(example2)
99

1010
if example2 == '':
11-
print("done with permutation")
12-
break
11+
return True
1312
else:
1413
continue
15-
else:
16-
print("Not permutatable")
17-
break
18-
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()

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

Lines changed: 9 additions & 5 deletions
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()
Lines changed: 11 additions & 2 deletions
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/Q1.3.py

Lines changed: 8 additions & 1 deletion
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.3 - URLify/camilo_solution_1.3.py

Lines changed: 11 additions & 1 deletion
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()

0 commit comments

Comments
 (0)