Skip to content

Tidy up code imports #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chapters/FFT/cooley_tukey.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ For some reason, though, putting code to this transformation really helped me fi
{% sample lang="jl" %}
[import:2-11, lang:"julia"](code/julia/fft.jl)
{% sample lang="c" %}
[import:7-19, lang:"c_cpp"](code/c/fft.c)
[import:8-19, lang:"c_cpp"](code/c/fft.c)
{% sample lang="cpp" %}
[import:2-11, lang:"julia"](code/julia/fft.jl)
{% sample lang="hs" %}
Expand Down Expand Up @@ -117,7 +117,7 @@ In the end, the code looks like:
{% sample lang="jl" %}
[import:14-31, lang:"julia"](code/julia/fft.jl)
{% sample lang="c" %}
[import:21-40, lang:"c_cpp"](code/c/fft.c)
[import:20-39, lang:"c_cpp"](code/c/fft.c)
{% sample lang="cpp" %}
[import:27-57, lang:"c_cpp"](code/c++/fft.cpp)
{% sample lang="hs" %}
Expand Down
4 changes: 2 additions & 2 deletions chapters/sorting_searching/bogo/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ In code, it looks something like this:
{% sample lang="cs" %}
[import:9-15, lang:"csharp"](code/cs/BogoSort.cs)
{% sample lang="clj" %}
[import:2-10, lang:"clojure"](code/clojure/bogo.clj)
[import:2-11, lang:"clojure"](code/clojure/bogo.clj)
{% sample lang="c" %}
[import:4-27, lang:"c_cpp"](code/c/bogo_sort.c)
[import:4-29, lang:"c_cpp"](code/c/bogo_sort.c)
{% sample lang="java" %}
[import:2-17, lang:"java"](code/java/bogo.java)
{% sample lang="js" %}
Expand Down
2 changes: 1 addition & 1 deletion chapters/sorting_searching/bubble/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
{% sample lang="cs" %}
[import:9-27, lang:"csharp"](code/cs/BubbleSort.cs)
{% sample lang="c" %}
[import:3-21, lang:"c_cpp"](code/c/bubble_sort.c)
[import:4-22, lang:"c_cpp"](code/c/bubble_sort.c)
{% sample lang="java" %}
[import:2-12, lang:"java"](code/java/bubble.java)
{% sample lang="js" %}
Expand Down
39 changes: 34 additions & 5 deletions chapters/tree_traversal/code/python/Tree_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Node:

def __init__(self):
self.data = None
self.children = []


def create_tree(node, num_row, num_child):
node.data = num_row

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

return node


def DFS_recursive(node):
if len(node.children) > 0:
if node.data != None:
print(node.data)

for child in node.children:
DFS_recursive(child)


def DFS_recursive_postorder(node):
for child in node.children:
DFS_recursive(child)

if node.data != None:
print(node.data)

for child in node.children:
DFS_recursive(child)

# This assumes only 2 children, but accounts for other possibilities
def DFS_recursive_inorder_btree(node):
if (len(node.children) == 2):
DFS_recursive_inorder_btree(node.children[1])
print(node.data)
DFS_recursive_inorder_btree(node.children[2])
elif (len(node.children) == 1):
DFS_recursive_inorder_btree(node.children[1])
print(node.data)
elif (len(node.children) == 0):
print(node.data)
else:
print("Not a binary tree!")


def DFS_stack(node):
stack = []
Expand All @@ -34,6 +59,7 @@ def DFS_stack(node):
for child in temp.children:
stack.append(child)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's established code style, but all other python files use single newline between functions

def BFS_queue(node):
queue = []
queue.append(node)
Expand All @@ -47,6 +73,7 @@ def BFS_queue(node):
for child in temp.children:
queue.append(child)


def main():
tree = create_tree(Node(), 3, 3)

Expand All @@ -59,5 +86,7 @@ def main():
print("Queue:")
BFS_queue(tree)

main()

if __name__ == '__main__':
main()

14 changes: 6 additions & 8 deletions chapters/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Trees are naturally recursive data structures, and because of this, we cannot ac
This has not been implemented in your chosen language, so here is the Julia code
[import:3-7, lang:"julia"](code/julia/Tree.jl)
{% sample lang="py" %}
[import:1-5, lang:"python"](code/python/Tree_example.py)
[import:1-4, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:3-7, lang:"julia"](code/julia/Tree.jl)
Expand All @@ -43,7 +43,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
{% sample lang="js" %}
[import:12-15, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:7-15, lang:"python"](code/python/Tree_example.py)
[import:18-23, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:9-16, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -79,8 +79,7 @@ This has not been implemented in your chosen language, so here is the Julia code
{% sample lang="js" %}
[import:17-20, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:18-26, lang:"julia"](code/julia/Tree.jl)
[import:26-31, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:18-26, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -112,8 +111,7 @@ This has not been implemented in your chosen language, so here is the Julia code
{% sample lang="js" %}
[import:22-34, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:28-43, lang:"julia"](code/julia/Tree.jl)
[import:34-46, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:28-43, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -154,7 +152,7 @@ In code, it looks like this:
{% sample lang="js" %}
[import:36-43, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:24-35, lang:"python"](code/python/Tree_example.py)
[import:49-60, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:45-56, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -187,7 +185,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
{% sample lang="js" %}
[import:45-52, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:37-48, lang:"python"](code/python/Tree_example.py)
[import:63-74, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:58-69, lang:"julia"](code/julia/Tree.jl)
Expand Down