Skip to content

Commit a9c48e3

Browse files
committed
Add complexity measures for chapter 1, and a readme
1 parent 6e38782 commit a9c48e3

File tree

10 files changed

+111
-47
lines changed

10 files changed

+111
-47
lines changed

chapter_1/p1_1.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
from collections import Counter
22

3+
# Method: Character map
4+
# Time: O(n)
5+
# Space: O(n)
6+
7+
# Follow-up
8+
# Method: Sort and compare
9+
# Time: O(n*log(n))
10+
# Space: O(n)
11+
312

413
def all_uniqs(input_str: str):
514
ctr = Counter(input_str)
@@ -24,4 +33,5 @@ def all_uniqs_no_datastruct(x: str):
2433
inputs = ["", "aaasodkc", "abbccdcss", "aaa", "b", "bdes"]
2534

2635
for x in inputs:
27-
print(f"Input {x} result: {all_uniqs(x)} same as {all_uniqs_no_datastruct(x)}")
36+
print(
37+
f"Input {x} result: {all_uniqs(x)} same as {all_uniqs_no_datastruct(x)}")

chapter_1/p1_2.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from collections import Counter
22

3+
# Method: Sort and compare
4+
# Time: O(n*log(n))
5+
# Space: O(n)
6+
37

48
def check_perm(a: str, b: str):
59
sa = sorted(a)

chapter_1/p1_3.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
from typing import List
22

3+
# Method: Count spaces, move into place from the end
4+
# Time: O(n)
5+
# Space: O(1)
6+
37

48
def urlize(a: List[chr], true_len):
59
ei = true_len - 1
610

7-
nr_s = 0
11+
nr_spaces = 0
812
for i in range(true_len):
913
if a[i] == " ":
10-
nr_s += 1
14+
nr_spaces += 1
1115

12-
ei = true_len - 1 - (nr_s//3) * 2
16+
ei = true_len - 1 - (nr_spaces//3) * 2
1317
# print(f"Turns out the string starts at {ei}")
1418
end_i = true_len-1
1519
while ei >= 0:
@@ -26,7 +30,9 @@ def urlize(a: List[chr], true_len):
2630
return "".join(a)
2731

2832

29-
def get_prob_input(str_w_spaces: str):
33+
def get_prob_input(str_w_spaces: str) -> str:
34+
"""Add the necessary buffer at the end, for a normal str
35+
"""
3036
spaces = str_w_spaces.count(" ")
3137
return str_w_spaces + " " * (spaces * 2), len(str_w_spaces) + spaces*2
3238

@@ -41,4 +47,3 @@ def get_prob_input(str_w_spaces: str):
4147

4248
a, fn = get_prob_input(" a c a c")
4349
print(f"Input is |{a}|, result |{urlize(list(a), fn)}| ")
44-

chapter_1/p1_4.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
import string
22
from collections import Counter
33

4+
# Method: Map and check for odds
5+
# Time: O(n)
6+
# Space: O(n)
7+
8+
# Method: Character bit-map, check set bits
9+
# Time: O(n)
10+
# Space: O(1) (limited by alphabet)
11+
412

513
def is_palindrom_perm(word: str) -> bool:
6-
clean_w = proc(word)
14+
clean_w = pre_process(word)
715
ctr = Counter(clean_w)
816
odd_c = sum(x % 2 for x in ctr.values())
917
return not odd_c > 1
1018

1119

1220
def is_palindrome_perm_better(word: str) -> bool:
1321
bs = {c: 0b0 for c in string.ascii_lowercase}
14-
for c in proc(word):
22+
for c in pre_process(word):
1523
bs[c] ^= 0b1 # Flip by xor with one
1624
return not sum(x for x in bs.values()) > 1
1725

1826

19-
def proc(x: str) -> str:
27+
def pre_process(x: str) -> str:
2028
# x.translate(string.maketrans("","",string.punctuation))
2129
letter_iter = filter(lambda c: c.isalpha(), x.strip())
2230
return "".join(map(lambda x: x.lower(), letter_iter))
2331

2432

2533
if __name__ == "__main__":
26-
print(proc("a...!>!>!a<<> <!!!aaa"))
34+
print(pre_process("a...!>!>!a<<> <!!!aaa"))
2735

2836
def printex(ex): return print(
2937
f"For exampple: {ex} ==> {is_palindrom_perm(ex)} \

chapter_1/p1_5.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Method: Char by char check, count differences
2+
# Time: O(min(n,m)), where n,m the lenghts of the input strings
3+
# Space: O(1)
4+
15

26
def is_edit(a: str, b: str) -> bool:
37
la = len(a)
@@ -17,28 +21,28 @@ def check_edit(a: str, b: str) -> bool:
1721
lb = len(b)
1822
i = 0
1923
j = 0
20-
mm = 0
24+
mismatches = 0
2125

2226
while i < la and j < lb:
2327
if a[i] == b[j]:
2428
i += 1
2529
else:
26-
mm += 1
30+
mismatches += 1
2731
j += 1
2832

29-
if mm > 1:
33+
if mismatches > 1:
3034
return False
3135

3236
return abs(i-j) <= 1
3337

3438

3539
def check_subst(a, b) -> bool:
36-
mm = 0
40+
mistmatches = 0
3741

3842
for i in range(len(a)):
3943
if a[i] != b[i]:
40-
mm += 1
41-
if mm > 1:
44+
mistmatches += 1
45+
if mistmatches > 1:
4246
return False
4347

4448
return True

chapter_1/p1_6.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
def basic_compr(a: str) -> str:
2-
cl = 0
3-
n = len(a)
1+
# Method: Build new, counting occurences
2+
# Time: O(n)
3+
# Space: O(n)
4+
5+
6+
def basic_compr(input_str: str) -> str:
7+
len_compres = 0
8+
len_orig = len(input_str)
49
res = []
510

611
prev = None
712
prev_count = 0
8-
for c in a:
13+
for c in input_str:
914
if c != prev:
1015
if prev:
1116
res.append(prev)
1217
res.append(str(prev_count))
13-
cl += 1 + len(str(prev_count))
14-
if cl > n:
15-
return a
18+
len_compres += 1 + len(str(prev_count))
19+
if len_compres > len_orig:
20+
return input_str
1621
prev = c
1722
prev_count = 1
1823
else:
@@ -21,9 +26,9 @@ def basic_compr(a: str) -> str:
2126
res.append(prev)
2227
res.append(str(prev_count))
2328

24-
cl += 1 + len(str(prev_count))
29+
len_compres += 1 + len(str(prev_count))
2530

26-
return a if cl > n else "".join(res)
31+
return input_str if len_compres > len_orig else "".join(res)
2732

2833

2934
if __name__ == "__main__":

chapter_1/p1_7.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from typing import List
22

3+
# Method: Recursive edge-walking
4+
# Time: O(n*m), where n,m the dimensions of the matrix
5+
# Space: O(1)
6+
37

48
def rotate(arr: List[List[int]]) -> List[List[int]]:
59
rot_help(arr, 0, len(arr[0])-1, 0, len(arr)-1)

chapter_1/p1_8.py

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
from typing import List
22

3+
# Method: Individual cell checks, with handling of first line and col
4+
# Time: O(n*m)
5+
# Space: O(1)
6+
37

48
def zero_m(matric: List[List[int]]):
5-
n, m = len(matric), len(matric[0])
9+
rows, cols = len(matric), len(matric[0])
610
fcol0 = False
711
frow0 = False
812

9-
for i in range(n):
10-
if matric[i][0] == 0:
13+
for r in range(rows):
14+
if matric[r][0] == 0:
1115
fcol0 = True
1216
break
1317

14-
for j in range(m):
15-
if matric[0][j] == 0:
18+
for c in range(cols):
19+
if matric[0][c] == 0:
1620
frow0 = True
1721
break
1822

19-
for i in range(n):
20-
for j in range(m):
21-
if matric[i][j] == 0:
22-
matric[i][0] = 0
23-
matric[0][j] = 0
23+
for r in range(rows):
24+
for c in range(cols):
25+
if matric[r][c] == 0:
26+
matric[r][0] = 0
27+
matric[0][c] = 0
2428

2529
print(f"Gonna zero now, first row {frow0} and first col {fcol0}")
2630
mrint(matric)
2731

28-
for i in range(1, n):
29-
if matric[i][0] == 0:
30-
for j in range(m):
31-
matric[i][j] = 0
32+
for r in range(1, rows):
33+
if matric[r][0] == 0:
34+
for c in range(cols):
35+
matric[r][c] = 0
3236

33-
for j in range(1, m):
34-
if matric[0][j] == 0:
35-
for i in range(n):
36-
matric[i][j] = 0
37+
for c in range(1, cols):
38+
if matric[0][c] == 0:
39+
for r in range(rows):
40+
matric[r][c] = 0
3741

3842
if fcol0:
39-
for i in range(n):
40-
matric[i][0] = 0
43+
for r in range(rows):
44+
matric[r][0] = 0
4145

4246
if frow0:
43-
for j in range(m):
44-
matric[0][j] = 0
47+
for c in range(cols):
48+
matric[0][c] = 0
4549

4650
return matric
4751

chapter_1/p1_9.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Method: Attach to itself
2+
# Time: O(n) (create new string, potentially substring check)
3+
# Space: O(n) (store the new big string)
4+
5+
16
def checkrot(s1: str, s2: str):
27
s22 = s2 + s2
38
return isSubstr(s1, s22) and len(s1) == len(s2)

chapter_1/readme.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Chapter 1: Arrays and Strings
2+
3+
The complexity table is for a quick lookup, more details in the actual file (e.g. explaining the symbols used)
4+
5+
| Nr. | Title | Solution | Time | Space | Notes |
6+
|:---: |:-----: |:--------: |:----: |:-----: |:-----: |
7+
| 1.1 | Is unique | [Python](./p1_1.py) | O(n) | O(n) | |
8+
| 1.2 | Check Permutation | [Python](./p1_2.py) | O(n*log(n)) | O(n) | |
9+
| 1.3 | URLify | [Python](./p1_3.py) | O(n) | O(1) | |
10+
| 1.4 | Palindrome Permutation | [Python](./p1_4py) | O(n) | O(1) | |
11+
| 1.5 | One Away | [Python](./p1_5.py) | O(min(n,m)) | O(1) | |
12+
| 1.6 | String Compression | [Python](./p1_6.py) | O(n) | O(n) | |
13+
| 1.7 | Rotate Matrix | [Python](./p1_7.py) | O(n*m) | O(1) | |
14+
| 1.8 | Zero Matrix | [Python](./p1_8.py) | O(n*m) | O(1) | |
15+
| 1.9 | String Rotation | [Python](./p1_9.py) | O(n) | O(n) | |

0 commit comments

Comments
 (0)