Skip to content
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

General python support #145

Merged
merged 8 commits into from
Jun 29, 2018
8 changes: 2 additions & 6 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@
"name": "C"
},
{
"lang": "py2",
"name": "Python 2"
},
{
"lang": "py3",
"name": "Python 3"
"lang": "py",
"name": "Python"
},
{
"lang": "js",
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions chapters/FFT/cooley_tukey.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 %}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
12 changes: 6 additions & 6 deletions chapters/euclidean_algorithm/euclidean.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
16 changes: 8 additions & 8 deletions chapters/physics_solvers/verlet/verlet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions chapters/sorting_searching/bubble/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down
26 changes: 10 additions & 16 deletions chapters/sorting_searching/bubble/code/python/bubblesort.py
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ def __init__(self):
self.data = None
self.children = []


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

Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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()
Expand Down
84 changes: 0 additions & 84 deletions chapters/tree_traversal/code/python3/Tree_example.py

This file was deleted.

Loading