Skip to content

Commit c481bf9

Browse files
Thuriijune128
authored andcommitted
Tidy up code imports (#194)
* Fix sort code imports * Complete python tree traversal example * Fix FFT code imports * Actually port the Julia code
1 parent 0b5a6fd commit c481bf9

File tree

5 files changed

+45
-18
lines changed

5 files changed

+45
-18
lines changed

chapters/FFT/cooley_tukey.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ For some reason, though, putting code to this transformation really helped me fi
7272
{% sample lang="jl" %}
7373
[import:2-11, lang:"julia"](code/julia/fft.jl)
7474
{% sample lang="c" %}
75-
[import:7-19, lang:"c_cpp"](code/c/fft.c)
75+
[import:8-19, lang:"c_cpp"](code/c/fft.c)
7676
{% sample lang="cpp" %}
7777
[import:2-11, lang:"julia"](code/julia/fft.jl)
7878
{% sample lang="hs" %}
@@ -117,7 +117,7 @@ In the end, the code looks like:
117117
{% sample lang="jl" %}
118118
[import:14-31, lang:"julia"](code/julia/fft.jl)
119119
{% sample lang="c" %}
120-
[import:21-40, lang:"c_cpp"](code/c/fft.c)
120+
[import:20-39, lang:"c_cpp"](code/c/fft.c)
121121
{% sample lang="cpp" %}
122122
[import:27-57, lang:"c_cpp"](code/c++/fft.cpp)
123123
{% sample lang="hs" %}

chapters/sorting_searching/bogo/bogo_sort.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ In code, it looks something like this:
1818
{% sample lang="cs" %}
1919
[import:9-15, lang:"csharp"](code/cs/BogoSort.cs)
2020
{% sample lang="clj" %}
21-
[import:2-10, lang:"clojure"](code/clojure/bogo.clj)
21+
[import:2-11, lang:"clojure"](code/clojure/bogo.clj)
2222
{% sample lang="c" %}
23-
[import:4-27, lang:"c_cpp"](code/c/bogo_sort.c)
23+
[import:4-29, lang:"c_cpp"](code/c/bogo_sort.c)
2424
{% sample lang="java" %}
2525
[import:2-17, lang:"java"](code/java/bogo.java)
2626
{% sample lang="js" %}

chapters/sorting_searching/bubble/bubble_sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
1313
{% sample lang="cs" %}
1414
[import:9-27, lang:"csharp"](code/cs/BubbleSort.cs)
1515
{% sample lang="c" %}
16-
[import:3-21, lang:"c_cpp"](code/c/bubble_sort.c)
16+
[import:4-22, lang:"c_cpp"](code/c/bubble_sort.c)
1717
{% sample lang="java" %}
1818
[import:2-12, lang:"java"](code/java/bubble.java)
1919
{% sample lang="js" %}

chapters/tree_traversal/code/python/Tree_example.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Node:
2-
32
def __init__(self):
43
self.data = None
54
self.children = []
65

6+
77
def create_tree(node, num_row, num_child):
88
node.data = num_row
99

@@ -14,12 +14,37 @@ def create_tree(node, num_row, num_child):
1414

1515
return node
1616

17+
1718
def DFS_recursive(node):
18-
if len(node.children) > 0:
19+
if node.data != None:
20+
print(node.data)
21+
22+
for child in node.children:
23+
DFS_recursive(child)
24+
25+
26+
def DFS_recursive_postorder(node):
27+
for child in node.children:
28+
DFS_recursive(child)
29+
30+
if node.data != None:
1931
print(node.data)
2032

21-
for child in node.children:
22-
DFS_recursive(child)
33+
34+
# This assumes only 2 children, but accounts for other possibilities
35+
def DFS_recursive_inorder_btree(node):
36+
if (len(node.children) == 2):
37+
DFS_recursive_inorder_btree(node.children[1])
38+
print(node.data)
39+
DFS_recursive_inorder_btree(node.children[2])
40+
elif (len(node.children) == 1):
41+
DFS_recursive_inorder_btree(node.children[1])
42+
print(node.data)
43+
elif (len(node.children) == 0):
44+
print(node.data)
45+
else:
46+
print("Not a binary tree!")
47+
2348

2449
def DFS_stack(node):
2550
stack = []
@@ -34,6 +59,7 @@ def DFS_stack(node):
3459
for child in temp.children:
3560
stack.append(child)
3661

62+
3763
def BFS_queue(node):
3864
queue = []
3965
queue.append(node)
@@ -47,6 +73,7 @@ def BFS_queue(node):
4773
for child in temp.children:
4874
queue.append(child)
4975

76+
5077
def main():
5178
tree = create_tree(Node(), 3, 3)
5279

@@ -59,5 +86,7 @@ def main():
5986
print("Queue:")
6087
BFS_queue(tree)
6188

62-
main()
89+
90+
if __name__ == '__main__':
91+
main()
6392

chapters/tree_traversal/tree_traversal.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Trees are naturally recursive data structures, and because of this, we cannot ac
1717
This has not been implemented in your chosen language, so here is the Julia code
1818
[import:3-7, lang:"julia"](code/julia/Tree.jl)
1919
{% sample lang="py" %}
20-
[import:1-5, lang:"python"](code/python/Tree_example.py)
20+
[import:1-4, lang:"python"](code/python/Tree_example.py)
2121
{% sample lang="scratch" %}
2222
This has not been implemented in your chosen language, so here is the Julia code
2323
[import:3-7, lang:"julia"](code/julia/Tree.jl)
@@ -43,7 +43,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
4343
{% sample lang="js" %}
4444
[import:12-15, lang:"javascript"](code/javascript/tree.js)
4545
{% sample lang="py" %}
46-
[import:7-15, lang:"python"](code/python/Tree_example.py)
46+
[import:18-23, lang:"python"](code/python/Tree_example.py)
4747
{% sample lang="scratch" %}
4848
This has not been implemented in your chosen language, so here is the Julia code
4949
[import:9-16, lang:"julia"](code/julia/Tree.jl)
@@ -79,8 +79,7 @@ This has not been implemented in your chosen language, so here is the Julia code
7979
{% sample lang="js" %}
8080
[import:17-20, lang:"javascript"](code/javascript/tree.js)
8181
{% sample lang="py" %}
82-
This has not been implemented in your chosen language, so here is the Julia code
83-
[import:18-26, lang:"julia"](code/julia/Tree.jl)
82+
[import:26-31, lang:"python"](code/python/Tree_example.py)
8483
{% sample lang="scratch" %}
8584
This has not been implemented in your chosen language, so here is the Julia code
8685
[import:18-26, lang:"julia"](code/julia/Tree.jl)
@@ -112,8 +111,7 @@ This has not been implemented in your chosen language, so here is the Julia code
112111
{% sample lang="js" %}
113112
[import:22-34, lang:"javascript"](code/javascript/tree.js)
114113
{% sample lang="py" %}
115-
This has not been implemented in your chosen language, so here is the Julia code
116-
[import:28-43, lang:"julia"](code/julia/Tree.jl)
114+
[import:34-46, lang:"python"](code/python/Tree_example.py)
117115
{% sample lang="scratch" %}
118116
This has not been implemented in your chosen language, so here is the Julia code
119117
[import:28-43, lang:"julia"](code/julia/Tree.jl)
@@ -154,7 +152,7 @@ In code, it looks like this:
154152
{% sample lang="js" %}
155153
[import:36-43, lang:"javascript"](code/javascript/tree.js)
156154
{% sample lang="py" %}
157-
[import:24-35, lang:"python"](code/python/Tree_example.py)
155+
[import:49-60, lang:"python"](code/python/Tree_example.py)
158156
{% sample lang="scratch" %}
159157
This has not been implemented in your chosen language, so here is the Julia code
160158
[import:45-56, lang:"julia"](code/julia/Tree.jl)
@@ -187,7 +185,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
187185
{% sample lang="js" %}
188186
[import:45-52, lang:"javascript"](code/javascript/tree.js)
189187
{% sample lang="py" %}
190-
[import:37-48, lang:"python"](code/python/Tree_example.py)
188+
[import:63-74, lang:"python"](code/python/Tree_example.py)
191189
{% sample lang="scratch" %}
192190
This has not been implemented in your chosen language, so here is the Julia code
193191
[import:58-69, lang:"julia"](code/julia/Tree.jl)

0 commit comments

Comments
 (0)