Skip to content

Commit 5b1b2fb

Browse files
author
Bogdan Stoicescu
committed
More problems from chapter 2, moved around 2 and 1 to correct order
1 parent 1c6afbb commit 5b1b2fb

File tree

8 files changed

+176
-22
lines changed

8 files changed

+176
-22
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ graph_out/
33
.vscode/
44
settings.json
55

6-
*/__pycache__/*
6+
*/__pycache__/*
7+
.DS_Store
8+
Leet.egg-*/

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
## Cracking the Coding Interview (6th edition)
2+
3+
### Current progress
4+
- Chapter 1: 9/9
5+
- Chapter 2: some/8
6+
- Chapter 10: 2/11
7+
8+
### Schedule
29
- Week 1: Chapter 4: Graphs and Trees
3-
- Week 2: Chapter 10: Sorting and Searching
10+
- Week 2: Chapter 10: Sorting and Searching
11+
- Week 3: Chapter 1: Arrays and Strings
12+
13+
### Running the solutions
14+
15+
For convenience, the project has a `setup.py` that specifies a package
16+
This can be installed locally in the virtualenvironment by using
17+
```bash
18+
pip install -e .
19+
```

chapter_2/linked_list_utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def __init__(self, val, next_node=None):
77
self. next = next_node
88

99
def __str__(self):
10-
return f"({self.val}) -> {self.next!r}"
10+
return f"({self.val}) -> {self.next}"
1111

1212

1313
def link_list_of_list(arr) -> Node:
@@ -18,6 +18,9 @@ def link_list_of_list(arr) -> Node:
1818
now_elem = now_elem.next
1919
return head
2020

21+
def llol(arr):
22+
return link_list_of_list(arr)
23+
2124

2225
if __name__ == "__main__":
2326
ex1 = [1, 2, 3]

chapter_2/p2_1.py

+39-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
1-
from linked_list_utils import *
1+
from chapter_2.linked_list_utils import Node, llol
22

33

4-
def k_to_last(k: int, h: Node):
5-
ldr = h
6-
folw = h
7-
d = -1
4+
def remove_dups_ll(head: Node):
5+
seen = set()
6+
prev = None
7+
while head:
8+
if head.val in seen:
9+
prev.next = head.next
10+
else:
11+
seen.add(head.val)
12+
prev = head
13+
head = head.next
814

9-
while ldr:
10-
print(f"Ldr is {ldr} and folw {folw}")
11-
d += 1
12-
if d > k:
13-
folw = folw.next
14-
ldr = ldr.next
1515

16-
if d<k:
17-
raise Exception(f"Input too short for distance {d}")
18-
19-
return folw.val
16+
def remove_dups_nobuf(h: Node):
17+
rc = h
18+
while rc:
19+
target = rc.val
20+
prev = rc
21+
restp = rc.next
22+
while restp:
23+
if restp.val == target:
24+
prev.next = restp.next
25+
else:
26+
prev = restp
27+
restp = restp.next
28+
rc = rc.next
2029

2130

2231
if __name__ == "__main__":
23-
ex1 = link_list_of_list([1, 2, 3, 4, 5, 6, 7])
24-
d = 5
25-
print(ex1)
26-
print(k_to_last(d, ex1))
32+
exs = [[1, 2, 3, 4, 5],
33+
[11, 2, 3, 4, 5, 6, 11],
34+
[11, 2, 3, 4, 5, 6, 11, 11, 11, 23],
35+
[0],
36+
[0, 0],
37+
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 3, 1]]
38+
39+
for ex in exs:
40+
r1 = llol(ex)
41+
r2 = llol(ex)
42+
remove_dups_nobuf(r1)
43+
remove_dups_nobuf(r2)
44+
print(f"For ex {ex} for ans 1: {r1}")
45+
print(f"For ex {ex} for ans 2: {r2}")
46+
print("-"*50)

chapter_2/p2_2.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from chapter_2.linked_list_utils import *
2+
3+
4+
def k_to_last(k: int, h: Node):
5+
ldr = h
6+
folw = h
7+
d = -1
8+
9+
while ldr:
10+
print(f"Ldr is {ldr} and folw {folw}")
11+
d += 1
12+
if d > k:
13+
folw = folw.next
14+
ldr = ldr.next
15+
16+
if d < k:
17+
raise Exception(f"Input too short for distance {d}")
18+
19+
return folw.val
20+
21+
22+
if __name__ == "__main__":
23+
ex1 = link_list_of_list([1, 2, 3, 4, 5, 6, 7])
24+
d = 5
25+
print(ex1)
26+
print(k_to_last(d, ex1))

chapter_2/p2_3.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from chapter_2.linked_list_utils import llol, Node
2+
3+
4+
def remove_mid(mid_node: Node):
5+
victim = mid_node.next
6+
if not victim:
7+
raise Exception("No following nodes")
8+
9+
# Take the value, and point to the next (can be none)
10+
mid_node.val = victim.val
11+
mid_node.next = victim.next
12+
victim.next = None
13+
14+
15+
if __name__ == "__main__":
16+
exs = [([1, 2, 3, 4, 5], 3),
17+
([1, 1, 1, 1], 1),
18+
([1, 2, 4, 5, 6, 7], 2),
19+
([1, 2, 3], 3)]
20+
21+
for arr, elem in exs:
22+
head = llol(arr)
23+
in_node = head
24+
while in_node.val != elem:
25+
in_node = in_node.next
26+
27+
try:
28+
remove_mid(in_node)
29+
print(f"For input {arr} to remove {elem} got result {head}")
30+
except Exception as e:
31+
print(f"For input {arr} to remove {elem} got error: {e}")
32+
print("-"*50)

chapter_2/p2_4.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from chapter_2.linked_list_utils import llol, Node
2+
3+
4+
def part_around(x: int, head: Node):
5+
low_head = None
6+
high_head = None
7+
low_root = None
8+
high_root = None
9+
10+
elem_now = head
11+
while elem_now:
12+
elem_next = elem_now.next
13+
if elem_now.val >= x:
14+
if not high_root:
15+
high_head = elem_now
16+
high_root = elem_now
17+
else:
18+
high_head.next = elem_now
19+
high_head = elem_now
20+
else:
21+
if not low_head:
22+
low_head = elem_now
23+
low_root = elem_now
24+
else:
25+
low_head.next = elem_now
26+
low_head = elem_now
27+
elem_now = elem_next
28+
29+
if low_root:
30+
low_head.next = high_root # Connect the parts
31+
high_head.next = None
32+
return low_root
33+
else:
34+
return high_root # No low elems
35+
36+
37+
if __name__ == "__main__":
38+
exs = [([1, 2, 3, 4, 5, 6, 7], 4),
39+
([10, 4, 7, 3, 1, 200, 40, 50, 3], 4),
40+
([30, 40, 50], 30),
41+
([1, 2, 5, 3, 2, 200], 3)
42+
]
43+
44+
for arr, x in exs:
45+
head = llol(arr)
46+
print(
47+
f"For\t{head} around partition {x},\ngot\t{part_around(x, head)}")
48+
print("-"*50)

setup.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='Leet',
5+
version='0.0.1',
6+
packages=find_packages(),
7+
)

0 commit comments

Comments
 (0)