Skip to content

Commit 0bf1e93

Browse files
author
Kimfucious
committed
copy all exercies to solutions, clearn exercises
1 parent 413c4fe commit 0bf1e93

Some content is hidden

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

62 files changed

+83
-1147
lines changed

exercises/anagrams.py

+1-31
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,4 @@
1010

1111

1212
def anagrams(stringA, stringB):
13-
"""
14-
Using regular expression sub() to remove non characters from string with
15-
helper function. sorted() returns a list, but no need to join
16-
"""
17-
import re
18-
19-
def clean_string(string):
20-
return sorted(re.sub(r"[^\w]", "", string).lower())
21-
return clean_string(stringA) == clean_string(stringB)
22-
23-
24-
# def anagrams(stringA, stringB):
25-
# """
26-
# Using sorted() directly on the strings w/o helper function
27-
# """
28-
# import re
29-
# return sorted(re.sub(r"[^\w]", "", stringA).lower()) == sorted(re.sub(r"[^\w]", "", stringB).lower())
30-
31-
32-
# def anagrams(stringA, stringB):
33-
# """
34-
# Using dict comprehension to built character maps with sorted() on the
35-
# dictionaries
36-
# """
37-
# import re
38-
# char_map_one = {char: stringA.count(
39-
# char) for char in re.sub(r"[^\w]", "", stringA).lower()}
40-
# char_map_two = {char: stringB.count(
41-
# char) for char in re.sub(r"[^\w]", "", stringB).lower()}
42-
43-
# return sorted(char_map_one) == sorted(char_map_two)
13+
pass

exercises/bst.py

+1-25
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,4 @@
1111

1212

1313
class Node:
14-
def __init__(self, data):
15-
self.data = data
16-
self.left = None
17-
self.right = None
18-
19-
def contains(self, data):
20-
if self.data == data:
21-
return self
22-
if self.left and data < self.data:
23-
return self.left.contains(data)
24-
elif self.right and data > self.data:
25-
return self.right.contains(data)
26-
else:
27-
return None
28-
29-
def insert(self, data):
30-
if self.left and data < self.data:
31-
self.left.insert(data)
32-
elif data < self.data:
33-
self.left = Node(data)
34-
35-
if self.right and data > self.data:
36-
self.right.insert(data)
37-
elif data > self.data:
38-
self.right = Node(data)
14+
pass

exercises/capitalize.py

+1-25
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,4 @@
88

99

1010
def capitalize(string):
11-
"""
12-
Using the Python str.title() method
13-
"""
14-
return string.title()
15-
16-
# def capitalize(string):
17-
# """
18-
# Using a loop on the string
19-
# """
20-
# result = string[0].upper()
21-
# for index in range(1, len(string)):
22-
# if string[index-1] == " ":
23-
# result += string[index].upper()
24-
# else:
25-
# result += string[index]
26-
# return result
27-
28-
# def capitalize(string):
29-
# """
30-
# Using a loop on a list split from the string
31-
# """
32-
# words = []
33-
# for word in string.split(" "):
34-
# words.append(word[0].upper() + word[1:])
35-
# return " ".join(words)
11+
pass

exercises/chunk.py

+1-32
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,4 @@
1010

1111

1212
def chunk(l, size):
13-
"""
14-
Using while until the list is empty Emptying out the list using del
15-
"""
16-
chunked = []
17-
while len(l) > 0:
18-
chunked.append(l[0:size])
19-
del l[0:size]
20-
return chunked
21-
22-
# def chunk(l, size):
23-
# """
24-
# Using while with an index counter
25-
# """
26-
# chunked = []
27-
# index = 0
28-
# while index < len(l):
29-
# chunked.append(l[index:index+size])
30-
# index += size
31-
# return chunked
32-
33-
# def chunk(l, size):
34-
# """
35-
# Using interation DOES NOT WORK!
36-
# """
37-
# chunked = []
38-
# for element in l:
39-
# last = chunked[len(chunked) - 1]
40-
# if not last or len(last) == size:
41-
# chunked.append([element])
42-
# else:
43-
# last.append(element)
44-
# return chunked
13+
pass

exercises/circular.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,4 @@
1313

1414

1515
def circular(l):
16-
slow = l.get_at(0)
17-
fast = l.get_at(0)
18-
19-
while fast.next and fast.next.next:
20-
slow = slow.next
21-
fast = fast.next.next
22-
if slow == fast:
23-
return True
24-
return False
16+
pass

exercises/fib.py

+1-46
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,4 @@
1111

1212

1313
def fib(n):
14-
"""
15-
Using recursion
16-
"""
17-
if n <= 1:
18-
return n
19-
return fib(n-1) + fib(n-2)
20-
21-
# There are issues with these solutions as the instructions have the Fib series
22-
# starting at zero and Stephen's tests assert that it starts at 1
23-
24-
# def fib(n):
25-
# result = [0, 1]
26-
27-
# if n == 1:
28-
# return 0
29-
30-
# for index in range(2, n+1):
31-
# a = result[index - 1]
32-
# b = result[index - 2]
33-
# result.append(a + b)
34-
35-
# return result[-1]
36-
37-
38-
# def fib(n):
39-
# """
40-
# Using a while loop and a counter
41-
# """
42-
# if n == 1:
43-
# return 0
44-
# if n > 0 and n < 3:
45-
# return 1
46-
# a = 1
47-
# b = 1
48-
# c = 2
49-
# counter = 3
50-
# while counter <= n:
51-
# if counter == n:
52-
# return c
53-
# a = b
54-
# b = c
55-
# c = a + b
56-
# counter += 1
57-
58-
59-
print(fib(3))
14+
pass

exercises/fib_memoize.py

+1-16
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,5 @@
1010
# function, so I named it george.
1111

1212

13-
def memoize(fn):
14-
cache = {}
15-
16-
def george(*args):
17-
if args in cache:
18-
return cache[args]
19-
else:
20-
cache[args] = fn(*args)
21-
return cache[args]
22-
return george
23-
24-
25-
@memoize
2613
def fib(n):
27-
if n < 2:
28-
return n
29-
return fib(n-1) + fib(n-2)
14+
pass

exercises/fizzbuzz.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,4 @@
1212

1313

1414
def fizzbuzz(n):
15-
for num in range(1, n+1):
16-
if num % 15 == 0:
17-
print("fizzbuzz")
18-
elif num % 5 == 0:
19-
print("buzz")
20-
elif num % 3 == 0:
21-
print("fizz")
22-
else:
23-
print(num)
15+
pass

exercises/fromlast.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,4 @@
1212

1313

1414
def from_last(l, n):
15-
slow = l.get_at(0)
16-
fast = l.get_at(n)
17-
while fast.next:
18-
slow = slow.next
19-
fast = fast.next
20-
return slow
15+
pass

exercises/levelwidth.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,4 @@
1212

1313

1414
def level_width(root):
15-
widths = [0]
16-
l = [root, "end"]
17-
while len(l) > 1:
18-
node = l.pop(0)
19-
if node == "end":
20-
l.append("end")
21-
widths.append(0)
22-
else:
23-
l = l + node.children
24-
widths[-1] += 1
25-
return widths
15+
pass

exercises/linkedlist.py

+2-101
Original file line numberDiff line numberDiff line change
@@ -4,107 +4,8 @@
44

55

66
class Node:
7-
def __init__(self, data, next=None):
8-
self.data = data
9-
self.next = next
7+
pass
108

119

1210
class LinkedList:
13-
def __init__(self, head=None):
14-
self.head = head
15-
16-
def __iter__(self):
17-
node = self.head
18-
while node:
19-
yield node
20-
node = node.next
21-
22-
def clear(self):
23-
self.head = None
24-
25-
def for_each(self, fn):
26-
if not self.head:
27-
return
28-
node = self.head
29-
while node:
30-
fn(node)
31-
node = node.next
32-
33-
def get_at(self, index):
34-
counter = 0
35-
node = self.head
36-
while node:
37-
if counter == index:
38-
return node
39-
counter += 1
40-
node = node.next
41-
return None
42-
43-
def get_first(self):
44-
return self.head
45-
46-
def get_last(self):
47-
if not self.head:
48-
return None
49-
node = self.head
50-
while node:
51-
if not node.next:
52-
return node
53-
node = node.next
54-
55-
def insert_first(self, data):
56-
self.head = Node(data, self.head)
57-
58-
def insert_at(self, data, index):
59-
if not self.head:
60-
self.head = Node(data)
61-
return
62-
if index == 0:
63-
self.head = Node(data, self.head)
64-
return
65-
prev = self.get_at(index - 1) or self.get_last()
66-
prev.next = Node(data, prev.next)
67-
68-
def insert_last(self, data):
69-
last = self.get_last()
70-
if last:
71-
last.next = Node(data)
72-
else:
73-
self.head = Node(data)
74-
75-
def remove_at(self, index):
76-
if not self.head:
77-
return None
78-
if index == 0:
79-
self.head = self.head.next
80-
return
81-
prev = self.get_at(index - 1)
82-
if not prev or not prev.next:
83-
return
84-
prev.next = prev.next.next
85-
86-
def remove_first(self):
87-
if not self.head:
88-
return
89-
self.head = self.head.next
90-
91-
def remove_last(self):
92-
if not self.head:
93-
return None
94-
if not self.head.next:
95-
self.head = None
96-
return
97-
prev = self.head
98-
node = self.head.next
99-
while node.next:
100-
prev = node
101-
node = node.next
102-
prev.next = None
103-
104-
def size(self):
105-
counter = 0
106-
node = self.head
107-
while node:
108-
counter += 1
109-
node = node.next
110-
return counter
11+
pass

0 commit comments

Comments
 (0)