diff --git a/book.json b/book.json index d32aedc1f..e2453bdf6 100644 --- a/book.json +++ b/book.json @@ -36,12 +36,8 @@ "name": "C" }, { - "lang": "py2", - "name": "Python 2" - }, - { - "lang": "py3", - "name": "Python 3" + "lang": "py", + "name": "Python" }, { "lang": "js", diff --git a/chapters/FFT/code/python2/fft.py b/chapters/FFT/code/python/fft.py similarity index 100% rename from chapters/FFT/code/python2/fft.py rename to chapters/FFT/code/python/fft.py diff --git a/chapters/FFT/cooley_tukey.md b/chapters/FFT/cooley_tukey.md index 6c764aada..497a1ab58 100644 --- a/chapters/FFT/cooley_tukey.md +++ b/chapters/FFT/cooley_tukey.md @@ -77,7 +77,7 @@ For some reason, though, putting code to this transformation really helped me fi [import:2-11, lang:"julia"](code/julia/fft.jl) {% sample lang="hs" %} [import:2-11, lang:"julia"](code/julia/fft.jl) -{% sample lang="py2" %} +{% sample lang="py" %} [import:2-11, lang:"julia"](code/julia/fft.jl) {% sample lang="scratch" %} [import:2-11, lang:"julia"](code/julia/fft.jl) @@ -122,8 +122,8 @@ In the end, the code looks like: [import:27-57, lang:"c_cpp"](code/c++/fft.cpp) {% sample lang="hs" %} [import:6-19, lang:"haskell"](code/hs/fft.hs) -{% sample lang="py2" %} -[import:5-16, lang:"python"](code/python2/fft.py) +{% sample lang="py" %} +[import:5-16, lang:"python"](code/python/fft.py) {% sample lang="scratch" %} [import:14-31, lang:"julia"](code/julia/fft.jl) {% endmethod %} @@ -230,9 +230,9 @@ Note: I implemented this in Julia because the code seems more straightforward in {% sample lang="hs" %} ### Haskell [import, lang:"haskell"](code/hs/fft.hs) -{% sample lang="py2" %} +{% sample lang="py" %} ### Python -[import, lang:"python"](code/python2/fft.py) +[import, lang:"python"](code/python/fft.py) {% sample lang="scratch" %} ### Scratch Some rather impressive scratch code was submitted by Jie and can be found here: https://scratch.mit.edu/projects/37759604/#editor diff --git a/chapters/euclidean_algorithm/code/python2/euclidean_example.py b/chapters/euclidean_algorithm/code/python/euclidean_example.py similarity index 75% rename from chapters/euclidean_algorithm/code/python2/euclidean_example.py rename to chapters/euclidean_algorithm/code/python/euclidean_example.py index 709e59a77..3e81f47dc 100644 --- a/chapters/euclidean_algorithm/code/python2/euclidean_example.py +++ b/chapters/euclidean_algorithm/code/python/euclidean_example.py @@ -24,6 +24,8 @@ def euclid_sub(a, b): return a -print euclid_mod(64 * 67, 64 * 81) -print euclid_sub(128 * 12, 128 * 77) +def main(): + print(euclid_mod(64 * 67, 64 * 81)) + print(euclid_sub(128 * 12, 128 * 77)) +main() diff --git a/chapters/euclidean_algorithm/euclidean.md b/chapters/euclidean_algorithm/euclidean.md index bfa0dc432..da2340fda 100644 --- a/chapters/euclidean_algorithm/euclidean.md +++ b/chapters/euclidean_algorithm/euclidean.md @@ -15,8 +15,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:20-33, lang="c_cpp"](code/c++/euclidean.cpp) {% sample lang="js" %} [import:15-29, lang="javascript"](code/javascript/euclidean_example.js) -{% sample lang="py2" %} -[import:14-25, lang="python"](code/python2/euclidean_example.py) +{% sample lang="py" %} +[import:14-25, lang="python"](code/python/euclidean_example.py) {% sample lang="haskell" %} [import:3-11, lang="haskell"](code/haskell/euclidean_example.hs) {% sample lang="rs" %} @@ -46,8 +46,8 @@ Modern implementations, though, often use the modulus operator (%) like so [import:7-17, lang="c_cpp"](code/c++/euclidean.cpp) {% sample lang="js" %} [import:1-13, lang="javascript"](code/javascript/euclidean_example.js) -{% sample lang="py2" %} -[import:1-12, lang="python"](code/python2/euclidean_example.py) +{% sample lang="py" %} +[import:1-12, lang="python"](code/python/euclidean_example.py) {% sample lang="haskell" %} [import:13-24, lang="haskell"](code/haskell/euclidean_example.hs) {% sample lang="rs" %} @@ -87,9 +87,9 @@ Program.cs {% sample lang="js" %} ### JavaScript [import, lang="javascript"](code/javascript/euclidean_example.js) -{% sample lang="py2" %} +{% sample lang="py" %} ### Python -[import, lang="python"](code/python2/euclidean_example.py) +[import, lang="python"](code/python/euclidean_example.py) {% sample lang="haskell" %} ### Haskell [import, lang="haskell"](code/haskell/euclidean_example.hs) diff --git a/chapters/physics_solvers/verlet/code/python2/verlet.py b/chapters/physics_solvers/verlet/code/python/verlet.py similarity index 92% rename from chapters/physics_solvers/verlet/code/python2/verlet.py rename to chapters/physics_solvers/verlet/code/python/verlet.py index 882fa3084..9853432bb 100644 --- a/chapters/physics_solvers/verlet/code/python2/verlet.py +++ b/chapters/physics_solvers/verlet/code/python/verlet.py @@ -52,17 +52,17 @@ def main(): sim = Verlet(Ball(pos = 5.0, acc = -10)) sim.run() - print "Verlet:", sim.time + print("Verlet:", sim.time) sim = Stormer_Verlet(Ball(pos = 5.0, acc = -10)) sim.run() - print "Stormer Verlet:", sim.time + print("Stormer Verlet:", sim.time) sim = Velocity_Verlet(Ball(pos = 5.0, acc = -10)) sim.run() - print "Velocity Verlet:", sim.time + print("Velocity Verlet:", sim.time) if __name__ == "__main__": diff --git a/chapters/physics_solvers/verlet/verlet.md b/chapters/physics_solvers/verlet/verlet.md index b936601a5..48f57c9ed 100644 --- a/chapters/physics_solvers/verlet/verlet.md +++ b/chapters/physics_solvers/verlet/verlet.md @@ -38,8 +38,8 @@ Here is what it looks like in code: [import:3-16, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} [import:2-18, lang:"java"](code/java/verlet.java) -{% sample lang="py2" %} -[import:28-33, lang:"python"](code/python2/verlet.py) +{% sample lang="py" %} +[import:28-33, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} Unfortunately, this has not yet been implemented in haskell, so here's Julia code: [import:1-13, lang:"julia"](code/julia/verlet.jl) @@ -84,8 +84,8 @@ Here's what it looks like in code: [import:18-33, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} [import:21-40, lang:"java"](code/java/verlet.java) -{% sample lang="py2" %} -[import:35-42, lang:"python"](code/python2/verlet.py) +{% sample lang="py" %} +[import:35-42, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} Unfortunately, this has not yet been implemented in scratch, so here's Julia code: [import:15-31, lang:"julia"](code/julia/verlet.jl) @@ -141,8 +141,8 @@ Here is the velocity Verlet method in code: [import:35-46, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} [import:43-57, lang:"java"](code/java/verlet.java) -{% sample lang="py2" %} -[import:44-48, lang:"python"](code/python2/verlet.py) +{% sample lang="py" %} +[import:44-48, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} Unfortunately, this has not yet been implemented in haskell, so here's Julia code: [import:33-45, lang:"julia"](code/julia/verlet.jl) @@ -181,9 +181,9 @@ Both of these methods work simply by iterating timestep-by-timestep and can be w {% sample lang="java" %} ### Java [import, lang:"java"](code/java/verlet.java) -{% sample lang="py2" %} +{% sample lang="py" %} ### Python -[import, lang:"python"](code/python2/verlet.py) +[import, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} ### Haskell [import, lang:"haskell"](code/haskell/verlet.hs) diff --git a/chapters/sorting_searching/bubble/bubble_sort.md b/chapters/sorting_searching/bubble/bubble_sort.md index 4e9220b6f..2ee5a945e 100644 --- a/chapters/sorting_searching/bubble/bubble_sort.md +++ b/chapters/sorting_searching/bubble/bubble_sort.md @@ -18,6 +18,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with [import:2-12, lang:"java"](code/java/bubble.java) {% sample lang="js" %} [import:1-11, lang:"javascript"](code/js/bubble.js) +{% sample lang="py" %} +[import:4-9, lang:"python"](code/python/bubblesort.py) {% sample lang="hs" %} [import, lang:"haskell"](code/haskell/bubbleSort.hs) {% sample lang="cpp" %} diff --git a/chapters/sorting_searching/bubble/code/python/bubblesort.py b/chapters/sorting_searching/bubble/code/python/bubblesort.py index 6125ecffb..19dd19858 100644 --- a/chapters/sorting_searching/bubble/code/python/bubblesort.py +++ b/chapters/sorting_searching/bubble/code/python/bubblesort.py @@ -1,23 +1,17 @@ -#!usr/bin/python3 - -#import section import random def bubble_sort(array): - len_array = len(array) - for i in range(len_array): - for j in range(len_array - i - 1): - if(array[j] > array[j+1]): - array[j], array[j+1] = array[j+1], array[j] #swap elements in the list - + len_array = len(array) + for i in range(len_array): + for j in range(len_array - i - 1): + if(array[j] > array[j+1]): + array[j], array[j+1] = array[j+1], array[j] #swap elements in the list def main(): - number = [random.randint(0, 10000) for _ in range(10)] - print("Before Sorting {}".format(number)) - bubble_sort(number) - print("After Sorting {}".format(number)) - + number = [random.randint(0, 1000) for _ in range(10)] + print("Before Sorting {}".format(number)) + bubble_sort(number) + print("After Sorting {}".format(number)) -if __name__ == "__main__": - main() +main() diff --git a/chapters/tree_traversal/code/python2/Tree_example.py b/chapters/tree_traversal/code/python/Tree_example.py similarity index 86% rename from chapters/tree_traversal/code/python2/Tree_example.py rename to chapters/tree_traversal/code/python/Tree_example.py index fbe2061c3..7027dbd8b 100644 --- a/chapters/tree_traversal/code/python2/Tree_example.py +++ b/chapters/tree_traversal/code/python/Tree_example.py @@ -4,7 +4,6 @@ def __init__(self): self.data = None self.children = [] - def create_tree(node, num_row, num_child): node.data = num_row @@ -17,7 +16,7 @@ def create_tree(node, num_row, num_child): def DFS_recursive(node): if len(node.children) > 0: - print node.data + print(node.data) for child in node.children: DFS_recursive(child) @@ -29,7 +28,7 @@ def DFS_stack(node): temp = None while len(stack) > 0: - print stack[-1].data + print(stack[-1].data) temp = stack.pop() for child in temp.children: @@ -42,7 +41,7 @@ def BFS_queue(node): temp = None while len(queue) > 0: - print queue[0].data + print(queue[0].data) temp = queue.pop(0) for child in temp.children: @@ -51,13 +50,13 @@ def BFS_queue(node): def main(): tree = create_tree(Node(), 3, 3) - print "Recursive:" + print("Recursive:") DFS_recursive(tree) - print "Stack:" + print("Stack:") DFS_stack(tree) - print "Queue:" + print("Queue:") BFS_queue(tree) main() diff --git a/chapters/tree_traversal/code/python3/Tree_example.py b/chapters/tree_traversal/code/python3/Tree_example.py deleted file mode 100644 index 1aec718d4..000000000 --- a/chapters/tree_traversal/code/python3/Tree_example.py +++ /dev/null @@ -1,84 +0,0 @@ -# Depth-First and Breadth-First Traversal -# Submitted by Matthew Giallourakis -from collections import deque - -class node(): - "Create the node structure" - def __init__(self, value, left, right): - self.value = value - self.left = left - self.right = right - -def make_tree(root,nums): - "Makes a binary search tree from a list of numbers" - for num in nums: - temp_node = root - new_node = node(value=num, left=None, right=None) - while True: - if num < temp_node.value: - if temp_node.left is None: - temp_node.left = new_node - break - else: - temp_node = temp_node.left - elif num > temp_node.value: - if temp_node.right is None: - temp_node.right = new_node - break - else: - temp_node = temp_node.right - -def depth_first_search(root): - "Traverses through a tree depth-first by putting nodes on a stack" - stack = deque() - result_list = [] - stack.append(root) - while len(stack) != 0: - # Take off the node at the top of the stack, add it to the list - temp_node = stack.pop() - result_list.append(temp_node.value) - # Add the children of that node to the top of the stack - if temp_node.right is not None: - stack.append(temp_node.right) - if temp_node.left is not None: - stack.append(temp_node.left) - return result_list - - -def breadth_first_search(root): - "Traverses through a tree breadth-first by putting nodes into a queue" - queue = deque() - result_list = [] - queue.append(root) - while len(queue) != 0: - # Take off the node at the top of the queue, add it to the list - temp_node = queue.pop() - result_list.append(temp_node.value) - # Add the children of that node to the bottom of the queue - if temp_node.left is not None: - queue.appendleft(temp_node.left) - if temp_node.right is not None: - queue.appendleft(temp_node.right) - return result_list - - -def main(): - nums = [5,8,6,9,2,1,3] - root = node(value=nums.pop(0),left=None,right=None) - make_tree(root,nums) - # Tree Structure - # 5 - # 2 8 - # 1 3 6 9 - # - - print("Depth First:",depth_first_search(root)) - # prints [5, 2, 1, 3, 8, 6, 9] - - print("Breadth First:",breadth_first_search(root)) - # prints [5, 2, 8, 1, 3, 6, 9] - - -if __name__ == '__main__': - main() - diff --git a/chapters/tree_traversal/tree_traversal.md b/chapters/tree_traversal/tree_traversal.md index 2fec2ca22..0f48a8f18 100644 --- a/chapters/tree_traversal/tree_traversal.md +++ b/chapters/tree_traversal/tree_traversal.md @@ -14,10 +14,8 @@ Trees are naturally recursive data structures, and because of this, we cannot ac {% sample lang="js" %} 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="py2" %} -[import:1-5, lang:"python"](code/python2/Tree_example.py) -{% sample lang="py3" %} -[import:5-10, lang:"python"](code/python3/Tree_example.py) +{% sample lang="py" %} +[import:1-5, 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) @@ -40,11 +38,8 @@ Because of this, the most straightforward way to traverse the tree might be recu [import:37-45, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} [import:12-15, lang:"javascript"](code/javascript/tree.js) -{% sample lang="py2" %} -[import:8-16, lang:"python"](code/python2/Tree_example.py) -{% sample lang="py3" %} -This has not been implemented in your chosen language, so here is the Julia code -[import:9-16, lang:"julia"](code/julia/Tree.jl) +{% sample lang="py" %} +[import:7-15, 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) @@ -77,10 +72,7 @@ This has not been implemented in your chosen language, so here is the Julia code [import:47-53, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} [import:17-20, lang:"javascript"](code/javascript/tree.js) -{% sample lang="py2" %} -This has not been implemented in your chosen language, so here is the Julia code -[import:18-26, lang:"julia"](code/julia/Tree.jl) -{% sample lang="py3" %} +{% 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) {% sample lang="scratch" %} @@ -111,10 +103,7 @@ This has not been implemented in your chosen language, so here is the Julia code [import:55-73, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} [import:22-34, lang:"javascript"](code/javascript/tree.js) -{% sample lang="py2" %} -This has not been implemented in your chosen language, so here is the Julia code -[import:28-43, lang:"julia"](code/julia/Tree.jl) -{% sample lang="py3" %} +{% 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) {% sample lang="scratch" %} @@ -154,10 +143,8 @@ In code, it looks like this: [import:75-93, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} [import:36-43, lang:"javascript"](code/javascript/tree.js) -{% sample lang="py2" %} -[import:25-36, lang:"python"](code/python2/Tree_example.py) -{% sample lang="py3" %} -[import:31-45, lang:"python"](code/python3/Tree_example.py) +{% sample lang="py" %} +[import:24-35, 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) @@ -187,10 +174,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can [import:95-113, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} [import:45-52, lang:"javascript"](code/javascript/tree.js) -{% sample lang="py2" %} -[import:38-49, lang:"python"](code/python2/Tree_example.py) -{% sample lang="py3" %} -[import:48-62, lang:"python"](code/python3/Tree_example.py) +{% sample lang="py" %} +[import:37-48, 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) @@ -223,12 +208,9 @@ tree_traversal.c {% sample lang="js" %} ### JavaScript [import, lang:"javascript"](code/javascript/tree.js) -{% sample lang="py2" %} -### Python 2 -[import, lang:"python"](code/python2/Tree_example.py) -{% sample lang="py3" %} -### Python 3 -[import, lang:"python"](code/python3/Tree_example.py) +{% sample lang="py" %} +### Python +[import, lang:"python"](code/python/Tree_example.py) {% sample lang="scratch" %} ### Scratch ![scratch tree](code/scratch/scratch_tree.png)