Skip to content

Commit fd84797

Browse files
committed
exercises
1 parent 1691650 commit fd84797

17 files changed

+475
-26
lines changed

binary_tree_traversal.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -169,37 +169,31 @@ def depth_first_search_recursive(search_for):
169169

170170
def breadth_first_search_iterative(search_for):
171171
iterator = []
172-
a_node = build_3_layer_even_tree()
173-
print(a_node)
172+
a_node = build_4_layer_even_tree()
174173

175174
if _bfs_iterative(search_for, a_node, iterator):
176175
return True
177176
return False
178177

179178

180-
def add_to_queue(node, iterator):
181-
if node.left is not None:
182-
iterator.append(node.left)
183-
if node.right is not None:
184-
iterator.append(node.right)
185-
186-
187-
def _bfs_iterative(search_for, node, iterator):
179+
def _bfs_iterative(search_for, node, queue):
188180
if node is None:
189181
return
190182

191-
# add root
192-
iterator.append(node)
183+
print(node)
193184

194-
add_to_queue(node, iterator)
185+
# add root
186+
queue.append(node)
195187

196-
while len(search_for) > 0:
197-
obj = iterator.pop(0)
188+
while queue:
189+
obj = queue.pop(0)
198190
print(obj.value)
199191
if obj.value == search_for:
200192
return True
201-
else:
202-
add_to_queue(obj, iterator)
193+
if obj.left is not None:
194+
queue.append(obj.left)
195+
if obj.right is not None:
196+
queue.append(obj.right)
203197

204198

205199
def depth_first_iterative(search_for):
@@ -208,15 +202,25 @@ def depth_first_iterative(search_for):
208202

209203
stack = [root]
210204
while stack:
211-
node = stack.pop(0)
205+
node = stack.pop()
212206
print(node.value)
213207
if node.value == search_for:
214208
return True
215209
if node.right is not None:
216-
stack.insert(0, node.right)
210+
stack.append(node.right)
217211
if node.left is not None:
218-
stack.insert(0, node.left)
212+
stack.append(node.left)
219213
return False
220214

215+
"""
216+
______a______
217+
/ \
218+
__b__ __c__
219+
/ \ / \
220+
d e f g
221+
/ \ / \ / \ / \
222+
h i j k l m n o
223+
"""
221224

222225
depth_first_iterative('p')
226+
breadth_first_search_iterative('z')

insert_ads/__init__.py

Whitespace-only changes.

insert_ads/exercise.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
raw = ['Artisan', 'live-edge', 'ugh', 'DIY', 'poutine', 'flexitarian', 'leggings', 'lo-fi', '3', 'wolf', 'moon',
2+
'biodiesel', 'ennui', 'kombucha', 'gentrify', 'XOXO', 'health', 'goth.', 'Brunch', 'fixie', 'put', 'a',
3+
'bird', 'on', 'it', 'you', 'probably', "haven't", 'heard', 'of', 'them', 'photo', 'booth', 'hell', 'of',
4+
'bespoke', 'bicycle', 'rights.', 'Mustache', 'neutra', 'truffaut,', 'DIY', 'hoodie', 'slow-carb', 'pop-up',
5+
'man', 'braid', 'pitchfork.', 'Artisan', 'activated', 'charcoal', 'tofu', 'shoreditch,', 'pug', 'readymade',
6+
'church-key', '+1', 'iPhone', 'normcore', 'fingerstache', 'keytar', 'truffaut', 'lumbersexual', 'paleo.',
7+
'Crucifix', 'austin', 'cred', 'taxidermy', 'truffaut', 'bicycle', 'rights', 'hell', 'of', 'pabst',
8+
'activated', 'charcoal.', 'Narwhal', 'forage', 'letterpress', 'paleo', 'gentrify', 'la', 'croix', 'synth',
9+
'freegan', 'bespoke', 'keytar.', 'Dreamcatcher', 'bespoke', 'bushwick', 'listicle', 'lomo.']
10+
11+
added_content = ['Kombucha', 'pop-up', 'blog', 'bitters', 'quinoa', 'blue', 'bottle', 'intelligentsia', 'flexitarian',
12+
'copper', 'mug', 'pour-over', 'messenger', 'bag', "90's", 'neutra', 'lomo.', 'Hella', "90's",
13+
'everyday', 'carry', 'mlkshk', 'scenester', 'four', 'dollar', 'toast', 'live-edge', 'cliche', 'wolf',
14+
'truffaut', 'cronut', 'ramps', 'succulents.', 'Slow-carb', 'fam', 'blue', 'bottle', 'adaptogen',
15+
'hammock', 'shoreditch.', 'Pour-over', 'fingerstache', 'mlkshk', 'tofu', 'normcore.', 'Tote', 'bag',
16+
'four', 'dollar', 'toast', 'lumbersexual', 'raw', 'denim', 'venmo', 'kickstarter', 'fixie',
17+
'stumptown', 'letterpress', 'locavore', 'echo', 'park', 'unicorn.', 'Forage', 'cardigan', 'tote',
18+
'bag', 'mlkshk.', 'Unicorn', 'la', 'croix', 'kickstarter', 'coloring', 'book', 'ugh', 'tilde',
19+
'sourdough', 'starter.']
20+
21+
22+
"""
23+
Part 1: We have a list of content and we want to inject ads after the 3rd piece of content and then after every 4
24+
pieces.
25+
26+
Part 2: We have 20 more pieces of content to add to the list and there should be ads between every 4 of them as a
27+
continuation of the previous list.
28+
"""
29+
def evenly_ad_ads(content_with_ads, content_without_ads, start_ad_index, ad_interval):
30+
# add one to make it so ads go after the interval instead of on the interval
31+
ad_interval = ad_interval + 1
32+
33+
content_length = len(content_without_ads)
34+
content_counter = 0
35+
36+
# iterate through the list and add either an AD or content until there is no more content to add
37+
while content_counter < content_length:
38+
content_with_ads_size = len(content_with_ads)
39+
if content_with_ads_size == start_ad_index or (content_with_ads_size - start_ad_index) % ad_interval == 0:
40+
content_with_ads.append("AD!")
41+
else:
42+
content_with_ads.append(content_without_ads[content_counter])
43+
content_counter += 1
44+
45+
return content_with_ads
46+
47+
"""
48+
what if we need to insert content into the middle of the list
49+
"""
50+
def insert_content(content_with_ads, inserted_content, insert_index, start_ad_index, ad_interval):
51+
# divide the list at the spot of insertion. Everything before the insertion spot should stay the same.
52+
content_with_ads_left = content_with_ads[:insert_index]
53+
content_with_ads_right = content_with_ads[insert_index:]
54+
# remove all of the ads after the insertion and re-add the ads after insertion
55+
content_without_ads_right = list(filter(lambda x: x != 'AD!', content_with_ads_right))
56+
inserted_content.extend(content_without_ads_right)
57+
58+
return evenly_ad_ads(content_with_ads_left, inserted_content, start_ad_index, ad_interval)
59+
60+
61+
62+
parts_1_2 = evenly_ad_ads(evenly_ad_ads([], raw, 3, 4), added_content, 3, 4)
63+
inserted_list = insert_content(parts_1_2, ["INSERT1", "INSERT2", "INSERT3", "INSERT4"], 1, 3, 4)

linked_list_any_combo_palindrome.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from linked_lists import LinkedList
2+
3+
""" determine if any combination of the linked list is a palindrome """
4+
5+
def is_any_combo_ll_palindrome(linked_list):
6+
num_letters = 0
7+
unaccounted_letters = set()
8+
while linked_list is not None:
9+
num_letters += 1
10+
current_value = linked_list.val
11+
if current_value in unaccounted_letters:
12+
unaccounted_letters.remove(current_value)
13+
else:
14+
unaccounted_letters.add(current_value)
15+
linked_list = linked_list.next
16+
if num_letters % 2 == 0:
17+
return len(unaccounted_letters) == 0
18+
else:
19+
return len(unaccounted_letters) == 1
20+
21+
a = LinkedList("c")
22+
b = LinkedList("i")
23+
c = LinkedList("v")
24+
d = LinkedList("i")
25+
b2 = LinkedList("c")
26+
# e = LinkedList("f")
27+
a.insert(b)
28+
b.insert(c)
29+
c.insert(d)
30+
d.insert(b2)
31+
# b2.insert(e)
32+
33+
print(is_any_combo_ll_palindrome(a) is True)

linked_list_digit_notification.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
""" the 2 linked lists given each represent a number where each digit is a node in the linked list in reverse order.
2+
Add the two numbers.
3+
ex: 4->2->8 = 824
4+
+
5+
2->3->1 = 132
6+
= 956
7+
"""
8+
from linked_lists import LinkedList
9+
10+
11+
def add_digits(n1, n2):
12+
n1_current_node = n1
13+
n2_current_node = n2
14+
sum_linked_list = None
15+
carryover = 0
16+
while n1_current_node is not None or n2_current_node is not None:
17+
digit_sum = carryover
18+
carryover = 0
19+
if n1_current_node is not None:
20+
digit_sum += n1_current_node.val
21+
n1_current_node = n1_current_node.next
22+
if n2_current_node is not None:
23+
digit_sum += n2_current_node.val
24+
n2_current_node = n2_current_node.next
25+
if digit_sum >= 10:
26+
carryover = 1
27+
digit_sum -= 10
28+
29+
next_link = LinkedList(digit_sum)
30+
next_link.next = sum_linked_list
31+
sum_linked_list = next_link
32+
33+
concat_num = []
34+
if carryover == 1:
35+
concat_num.append(1)
36+
37+
while sum_linked_list is not None:
38+
concat_num.append(sum_linked_list.val)
39+
sum_linked_list = sum_linked_list.next
40+
return int("".join([str(i) for i in concat_num]))
41+
42+
a = LinkedList(4)
43+
b = LinkedList(2)
44+
c = LinkedList(8)
45+
a.insert(b)
46+
b.insert(c)
47+
48+
49+
a2 = LinkedList(2)
50+
b2 = LinkedList(3)
51+
c2 = LinkedList(3)
52+
a2.insert(b2)
53+
b2.insert(c2)
54+
55+
print(add_digits(a, a2) == 1156)

linked_list_kth_to_last.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
""" find the kth to last element in a singly linked list """
2+
import queue
3+
4+
from linked_lists import LinkedList
5+
6+
7+
"""
8+
time: n
9+
space: k
10+
"""
11+
def find_kth_last(head, k):
12+
counter = 0
13+
q = queue.Queue()
14+
current = head
15+
while True:
16+
counter += 1
17+
if current is None:
18+
break
19+
q.put(current)
20+
if counter > k:
21+
q.get()
22+
current = current.next
23+
return q.get().val
24+
25+
26+
"""
27+
time: n
28+
space: 1
29+
"""
30+
def find_kth_last_two_pointers(head, k):
31+
slow_pointer = head
32+
fast_pointer = head
33+
34+
for i in range(k):
35+
fast_pointer = fast_pointer.next
36+
37+
while True:
38+
if fast_pointer is None:
39+
return slow_pointer.val
40+
slow_pointer = slow_pointer.next
41+
fast_pointer = fast_pointer.next
42+
43+
44+
a = LinkedList("a")
45+
b = LinkedList("b")
46+
c = LinkedList("c")
47+
d = LinkedList("d")
48+
b2 = LinkedList("e")
49+
e = LinkedList("f")
50+
a.insert(b)
51+
b.insert(c)
52+
c.insert(d)
53+
d.insert(b2)
54+
b2.insert(e)
55+
56+
find_kth_last_two_pointers(a, 3)

linked_list_palindrome.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
""" determine if the linked list is a palindrome """
2+
from linked_lists import LinkedList
3+
4+
5+
def is_palindrome(linked_list):
6+
pass
7+
8+
9+
a = LinkedList("c")
10+
b = LinkedList("i")
11+
c = LinkedList("v")
12+
d = LinkedList("i")
13+
b2 = LinkedList("c")
14+
# e = LinkedList("f")
15+
a.insert(b)
16+
b.insert(c)
17+
c.insert(d)
18+
d.insert(b2)
19+
# b2.insert(e)
20+
21+
print(is_palindrome(a) is True)
22+

linked_list_remove_duplicates.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
""" remove duplicates from linked list """
2+
from linked_lists import LinkedList
3+
4+
""" time: n
5+
space: n
6+
"""
7+
def rm_duplicates(head):
8+
seen_values = set(head.val)
9+
current = head
10+
while True:
11+
current = current.next
12+
if current is None:
13+
break
14+
if current.val in seen_values:
15+
current.prev.next = current.next
16+
else:
17+
seen_values.add(current.val)
18+
19+
"""
20+
time: n^2
21+
space: 1
22+
"""
23+
def rm_duplicates_space(head):
24+
current = head
25+
breaker_one = True
26+
while breaker_one:
27+
if current is None:
28+
break
29+
breaker_two = True
30+
tracer_two = current.next
31+
while breaker_two:
32+
if tracer_two is None:
33+
break
34+
if current.val == tracer_two.val:
35+
tracer_two.prev.next = tracer_two.next
36+
tracer_two = tracer_two.next
37+
current = current.next
38+
39+
40+
a = LinkedList("a")
41+
b = LinkedList("b")
42+
c = LinkedList("c")
43+
d = LinkedList("d")
44+
b2 = LinkedList("b")
45+
e = LinkedList("e")
46+
a.insert(b)
47+
b.insert(c)
48+
c.insert(d)
49+
d.insert(b2)
50+
b2.insert(e)
51+
52+
rm_duplicates_space(a)
53+
a = 1
54+

linked_lists.py

-5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,3 @@ def find_continue(link, value):
3232

3333
def search(head, value):
3434
return find_continue(head, value)
35-
36-
37-
head = add_links()
38-
print_to_end(head)
39-
c = search(head, 'c')

0 commit comments

Comments
 (0)