Skip to content

Commit 85602f7

Browse files
authored
Merge pull request #9 from nickolasteixeira/chapter1
Chapter1 solutions 1.5 - 1.9
2 parents 17c983f + b59435b commit 85602f7

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python3
2+
import unittest
3+
4+
5+
def one_away(s1, s2):
6+
'''Given two strings, write a function to check if
7+
they are one edit (or zero edits) away.'''
8+
if abs(len(s1) - len(s2)) > 1:
9+
return False
10+
11+
str1 = s1 if len(s1) < len(s2) else s2
12+
str2 = s2 if len(s1) < len(s2) else s1
13+
idx1, idx2 = 0, 0
14+
diff = False
15+
16+
while idx1 < len(str1) and idx2 < len(str2):
17+
if str1[idx1] is not str2[idx2]:
18+
if diff:
19+
return False
20+
diff = True
21+
22+
if len(str1) is len(str2):
23+
idx1 += 1
24+
else:
25+
idx1 += 1
26+
idx2 += 1
27+
28+
return True
29+
30+
31+
class Test(unittest.TestCase):
32+
def test1(self):
33+
s1 = "Tact Cat"
34+
s2 = "TactCat"
35+
s3 = "nick"
36+
s4 = "jeff"
37+
self.assertTrue(one_away(s1, s2))
38+
self.assertFalse(one_away(s3, s4))
39+
40+
41+
if __name__ == '__main__':
42+
unittest.main()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/python3
2+
import unittest
3+
4+
5+
def string_compression(string):
6+
'''Implement a method to perform basic string compression using the counts
7+
of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the
8+
"compressed" string would not become smaller than the original string, your method should return
9+
the original string. You can assume the string has only uppercase and lowercase letters (a - z). '''
10+
if not isinstance(string, str):
11+
return False
12+
13+
new_str = ''
14+
count, new_count = 0, 0
15+
for idx in range(len(string) - 1):
16+
count += 1
17+
if string[idx] is not string[idx + 1]:
18+
new_str += string[idx]
19+
new_str += str(count)
20+
count = 0
21+
if idx is len(string) - 2:
22+
if string[idx] is string[idx + 1]:
23+
new_str += string[idx]
24+
new_str += str(count + 1)
25+
else:
26+
new_str += string[idx + 1]
27+
new_str += '1'
28+
29+
for letter in new_str:
30+
if letter is '1':
31+
new_count += 1
32+
33+
if len(string) // new_count is 1:
34+
return string
35+
return new_str
36+
37+
38+
class Test(unittest.TestCase):
39+
def test1(self):
40+
self.assertEqual(string_compression('aabcccccaaa'), 'a2b1c5a3')
41+
self.assertEqual(
42+
string_compression('bbaaaabcdeeefff'),
43+
'b2a4b1c1d1e3f3')
44+
self.assertEqual(string_compression('abcdef'), 'abcdef')
45+
self.assertNotEqual(string_compression('abcdef'), 'a1b1c1d1e1f1')
46+
47+
48+
if __name__ == '__main__':
49+
unittest.main()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/python3
2+
import random
3+
from pprint import pprint
4+
5+
def build_matrix(w, h, max=10):
6+
'''function that builds a matrix with random numbers including 0'''
7+
return [[random.randint(0, max) for _ in range(w)] for _ in range(h)]
8+
9+
def rotate_matrix(matrix):
10+
'''function that rotates a matrix clockwise'''
11+
if (len(matrix) is 0 or len(matrix) is not len(matrix[0])): return False
12+
matrix_length = len(matrix)
13+
for layer in range(matrix_length//2):
14+
first = layer
15+
last = matrix_length - 1 - layer
16+
for i in range(first, last):
17+
offset = i - first
18+
top = matrix[first][i]
19+
20+
######################
21+
# a ----------> d #
22+
# ^ | #
23+
# | | #
24+
# | | #
25+
# | v #
26+
# b <------------ c #
27+
#######################
28+
29+
# top left inherits from bottom bottom left
30+
matrix[first][i] = matrix[last - offset][first]
31+
32+
# bottom left becomes bottom right
33+
matrix[last - offset][first] = matrix[last][last - offset]
34+
35+
# bottom right becomes top right
36+
matrix[last][last - offset] = matrix[i][last]
37+
38+
# top right becomes top left
39+
matrix[i][last] = top
40+
41+
if __name__ =='__main__':
42+
matrix = build_matrix(5, 5)
43+
pprint(matrix)
44+
rotate_matrix(matrix)
45+
pprint(matrix)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python3
2+
import random
3+
4+
5+
def build_matrix(w, h, max=10):
6+
'''function that builds a matrix with random numbers including 0'''
7+
return [[random.randint(0, max) for _ in range(w)] for _ in range(h)]
8+
9+
10+
def zero_matrix(matrix):
11+
'''Write an algorithm such that if an element in an MxN matrix is 0, its entire row and
12+
column are set to 0.'''
13+
row, column = [], []
14+
zero_present = False
15+
16+
for idx1 in range(len(matrix)):
17+
for idx2 in range(len(matrix[idx1])):
18+
if matrix[idx1][idx2] is 0:
19+
zero_present = True
20+
row.append(idx1)
21+
column.append(idx2)
22+
23+
if zero_present:
24+
for idx1 in range(len(matrix)):
25+
for idx2 in range(len(matrix[idx1])):
26+
if idx1 in row:
27+
matrix[idx1][idx2] = 0
28+
if idx2 in column:
29+
matrix[idx1][idx2] = 0
30+
31+
if __name__ == '__main__':
32+
matrix = build_matrix(4, 4)
33+
print("Old Matrix => ", matrix)
34+
zero_matrix(matrix)
35+
print("New Matrix => ", matrix)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/python3
2+
import unittest
3+
4+
def is_rotation(s1, s2):
5+
'''Assumeyou have a method isSubstringwhich checks if oneword is a substring
6+
of another. Given two strings, sl and s2, write code to check if s2 is a rotation of sl using only one
7+
call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat"). '''
8+
if len(s1) is len(s2) and len(s1) > 0:
9+
return s2 in s1 + s1
10+
return False
11+
12+
13+
class Test(unittest.TestCase):
14+
def test1(self):
15+
self.assertTrue(is_rotation('waterbottle', 'erbottlewat'))
16+
self.assertFalse(is_rotation('watermellon', 'watermellons'))
17+
self.assertFalse(is_rotation('waterbottle', 'bottleaterw'))
18+
19+
20+
if __name__ == '__main__':
21+
unittest.main()

0 commit comments

Comments
 (0)