Skip to content

Commit c0e5c79

Browse files
authored
Merge branch 'master' into master
2 parents f46d0c8 + 524de7f commit c0e5c79

File tree

19 files changed

+193
-169
lines changed

19 files changed

+193
-169
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Maxime Dherbécourt
99
Jess 3Jane
1010
Pen Pal
1111
Mukundan
12-
Chinmaya Mahesh
12+
Chinmaya Mahesh
13+
Unlambder

book.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@
3636
"name": "C"
3737
},
3838
{
39-
"lang": "py2",
40-
"name": "Python 2"
41-
},
42-
{
43-
"lang": "py3",
44-
"name": "Python 3"
39+
"lang": "py",
40+
"name": "Python"
4541
},
4642
{
4743
"lang": "js",
@@ -86,6 +82,14 @@
8682
{
8783
"lang": "go",
8884
"name": "Go"
85+
},
86+
{
87+
"lang": "racket",
88+
"name": "Racket"
89+
}
90+
{
91+
"lang": "m",
92+
"name": "Matlab"
8993
}
9094

9195
],
File renamed without changes.

chapters/FFT/cooley_tukey.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ For some reason, though, putting code to this transformation really helped me fi
7777
[import:2-11, lang:"julia"](code/julia/fft.jl)
7878
{% sample lang="hs" %}
7979
[import:2-11, lang:"julia"](code/julia/fft.jl)
80-
{% sample lang="py2" %}
80+
{% sample lang="py" %}
8181
[import:2-11, lang:"julia"](code/julia/fft.jl)
8282
{% sample lang="scratch" %}
8383
[import:2-11, lang:"julia"](code/julia/fft.jl)
@@ -122,8 +122,8 @@ In the end, the code looks like:
122122
[import:27-57, lang:"c_cpp"](code/c++/fft.cpp)
123123
{% sample lang="hs" %}
124124
[import:6-19, lang:"haskell"](code/hs/fft.hs)
125-
{% sample lang="py2" %}
126-
[import:5-16, lang:"python"](code/python2/fft.py)
125+
{% sample lang="py" %}
126+
[import:5-16, lang:"python"](code/python/fft.py)
127127
{% sample lang="scratch" %}
128128
[import:14-31, lang:"julia"](code/julia/fft.jl)
129129
{% endmethod %}
@@ -230,9 +230,9 @@ Note: I implemented this in Julia because the code seems more straightforward in
230230
{% sample lang="hs" %}
231231
### Haskell
232232
[import, lang:"haskell"](code/hs/fft.hs)
233-
{% sample lang="py2" %}
233+
{% sample lang="py" %}
234234
### Python
235-
[import, lang:"python"](code/python2/fft.py)
235+
[import, lang:"python"](code/python/fft.py)
236236
{% sample lang="scratch" %}
237237
### Scratch
238238
Some rather impressive scratch code was submitted by Jie and can be found here: https://scratch.mit.edu/projects/37759604/#editor

chapters/euclidean_algorithm/code/python2/euclidean_example.py renamed to chapters/euclidean_algorithm/code/python/euclidean_example.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def euclid_sub(a, b):
2424

2525
return a
2626

27-
print euclid_mod(64 * 67, 64 * 81)
28-
print euclid_sub(128 * 12, 128 * 77)
27+
def main():
28+
print(euclid_mod(64 * 67, 64 * 81))
29+
print(euclid_sub(128 * 12, 128 * 77))
2930

31+
main()

chapters/euclidean_algorithm/euclidean.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
1515
[import:20-33, lang="c_cpp"](code/c++/euclidean.cpp)
1616
{% sample lang="js" %}
1717
[import:15-29, lang="javascript"](code/javascript/euclidean_example.js)
18-
{% sample lang="py2" %}
19-
[import:14-25, lang="python"](code/python2/euclidean_example.py)
18+
{% sample lang="py" %}
19+
[import:14-25, lang="python"](code/python/euclidean_example.py)
2020
{% sample lang="haskell" %}
2121
[import:3-11, lang="haskell"](code/haskell/euclidean_example.hs)
2222
{% sample lang="rs" %}
@@ -48,8 +48,8 @@ Modern implementations, though, often use the modulus operator (%) like so
4848
[import:7-17, lang="c_cpp"](code/c++/euclidean.cpp)
4949
{% sample lang="js" %}
5050
[import:1-13, lang="javascript"](code/javascript/euclidean_example.js)
51-
{% sample lang="py2" %}
52-
[import:1-12, lang="python"](code/python2/euclidean_example.py)
51+
{% sample lang="py" %}
52+
[import:1-12, lang="python"](code/python/euclidean_example.py)
5353
{% sample lang="haskell" %}
5454
[import:13-24, lang="haskell"](code/haskell/euclidean_example.hs)
5555
{% sample lang="rs" %}
@@ -91,9 +91,9 @@ Program.cs
9191
{% sample lang="js" %}
9292
### JavaScript
9393
[import, lang="javascript"](code/javascript/euclidean_example.js)
94-
{% sample lang="py2" %}
94+
{% sample lang="py" %}
9595
### Python
96-
[import, lang="python"](code/python2/euclidean_example.py)
96+
[import, lang="python"](code/python/euclidean_example.py)
9797
{% sample lang="haskell" %}
9898
### Haskell
9999
[import, lang="haskell"](code/haskell/euclidean_example.hs)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Submitted by Chinmaya Mahesh (chin123)
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"math"
8+
"math/rand"
9+
"time"
10+
)
11+
12+
func inCircle(x, y float64) bool {
13+
return x*x+y*y < 1.0 // the radius of an unit circle is 1.0
14+
}
15+
16+
func monteCarlo(samples int) {
17+
count := 0
18+
s := rand.NewSource(time.Now().UnixNano())
19+
r := rand.New(s)
20+
21+
for i := 0; i < samples; i++ {
22+
x, y := r.Float64(), r.Float64()
23+
24+
if inCircle(x, y) {
25+
count += 1
26+
}
27+
}
28+
29+
estimate := 4.0 * float64(count) / float64(samples)
30+
31+
fmt.Println("The estimate of pi is", estimate)
32+
fmt.Printf("Which has an error of %f%%\n", 100*math.Abs(math.Pi-estimate)/math.Pi)
33+
}
34+
35+
func main() {
36+
monteCarlo(10000000)
37+
}

chapters/monte_carlo/monte_carlo.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ each point is tested to see whether it's in the circle or not:
4747
[import:7-9, lang:"rust"](code/rust/monte_carlo.rs)
4848
{% sample lang="d" %}
4949
[import:2-5, lang:"d"](code/rust/monte_carlo.d)
50+
{% sample lang="go" %}
51+
[import:12-14, lang:"go"](code/go/monteCarlo.go)
5052
{% endmethod %}
5153

5254
If it's in the circle, we increase an internal count by one, and in the end,
@@ -93,6 +95,9 @@ Feel free to submit your version via pull request, and thanks for reading!
9395
### D
9496
{%sample lang="d" %}
9597
[import, lang:"d"](code/d/monte_carlo.d)
98+
### Go
99+
{%sample lang="go" %}
100+
[import, lang:"go"](code/go/monteCarlo.go)
96101
{% endmethod %}
97102

98103

chapters/physics_solvers/verlet/code/python2/verlet.py renamed to chapters/physics_solvers/verlet/code/python/verlet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ def main():
5252
sim = Verlet(Ball(pos = 5.0, acc = -10))
5353
sim.run()
5454

55-
print "Verlet:", sim.time
55+
print("Verlet:", sim.time)
5656

5757
sim = Stormer_Verlet(Ball(pos = 5.0, acc = -10))
5858
sim.run()
5959

60-
print "Stormer Verlet:", sim.time
60+
print("Stormer Verlet:", sim.time)
6161

6262
sim = Velocity_Verlet(Ball(pos = 5.0, acc = -10))
6363
sim.run()
6464

65-
print "Velocity Verlet:", sim.time
65+
print("Velocity Verlet:", sim.time)
6666

6767

6868
if __name__ == "__main__":

chapters/physics_solvers/verlet/verlet.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ Here is what it looks like in code:
3838
[import:3-16, lang:"c_cpp"](code/c/verlet.c)
3939
{% sample lang="java" %}
4040
[import:2-18, lang:"java"](code/java/verlet.java)
41-
{% sample lang="py2" %}
42-
[import:28-33, lang:"python"](code/python2/verlet.py)
41+
{% sample lang="py" %}
42+
[import:28-33, lang:"python"](code/python/verlet.py)
4343
{% sample lang="hs" %}
4444
Unfortunately, this has not yet been implemented in haskell, so here's Julia code:
4545
[import:1-13, lang:"julia"](code/julia/verlet.jl)
@@ -84,8 +84,8 @@ Here's what it looks like in code:
8484
[import:18-33, lang:"c_cpp"](code/c/verlet.c)
8585
{% sample lang="java" %}
8686
[import:21-40, lang:"java"](code/java/verlet.java)
87-
{% sample lang="py2" %}
88-
[import:35-42, lang:"python"](code/python2/verlet.py)
87+
{% sample lang="py" %}
88+
[import:35-42, lang:"python"](code/python/verlet.py)
8989
{% sample lang="hs" %}
9090
Unfortunately, this has not yet been implemented in scratch, so here's Julia code:
9191
[import:15-31, lang:"julia"](code/julia/verlet.jl)
@@ -141,8 +141,8 @@ Here is the velocity Verlet method in code:
141141
[import:35-46, lang:"c_cpp"](code/c/verlet.c)
142142
{% sample lang="java" %}
143143
[import:43-57, lang:"java"](code/java/verlet.java)
144-
{% sample lang="py2" %}
145-
[import:44-48, lang:"python"](code/python2/verlet.py)
144+
{% sample lang="py" %}
145+
[import:44-48, lang:"python"](code/python/verlet.py)
146146
{% sample lang="hs" %}
147147
Unfortunately, this has not yet been implemented in haskell, so here's Julia code:
148148
[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
181181
{% sample lang="java" %}
182182
### Java
183183
[import, lang:"java"](code/java/verlet.java)
184-
{% sample lang="py2" %}
184+
{% sample lang="py" %}
185185
### Python
186-
[import, lang:"python"](code/python2/verlet.py)
186+
[import, lang:"python"](code/python/verlet.py)
187187
{% sample lang="hs" %}
188188
### Haskell
189189
[import, lang:"haskell"](code/haskell/verlet.hs)

chapters/sorting_searching/bogo/bogo_sort.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ In code, it looks something like this:
2727
[import:1-16, lang:"javascript"](code/js/bogo.js)
2828
{% sample lang="hs" %}
2929
[import, lang:"haskell"](code/haskell/bogoSort.hs)
30+
{% sample lang="m" %}
31+
[import, lang:"matlab"](code/matlab/bogosort.m)
3032
{% sample lang="cpp" %}
3133
[import, lang:"c_cpp"](code/c++/bogosort.cpp)
3234
{% sample lang="rs" %}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function main()
2+
array = floor( rand(1,7)*100 );
3+
disp('Before Sorting:')
4+
disp(array)
5+
6+
array = bogo_sort(array);
7+
disp('After Sorting')
8+
disp(array)
9+
end
10+
11+
function retval = is_sorted(array)
12+
for i=1:length(array)-1
13+
if array(i) > array(i+1)
14+
retval = false;
15+
return
16+
end
17+
end
18+
retval = true;
19+
end
20+
21+
function sorted_array = bogo_sort(array)
22+
while ~is_sorted(array)
23+
% create a list of random permutation indices
24+
i = randperm(length(array));
25+
array = array(i);
26+
end
27+
sorted_array = array;
28+
end
29+
30+

chapters/sorting_searching/bubble/bubble_sort.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
1818
[import:2-12, lang:"java"](code/java/bubble.java)
1919
{% sample lang="js" %}
2020
[import:1-11, lang:"javascript"](code/js/bubble.js)
21+
{% sample lang="py" %}
22+
[import:4-9, lang:"python"](code/python/bubblesort.py)
23+
{% sample lang="m" %}
24+
[import:11-23, lang:"matlab"](code/matlab/bubblesort.m)
2125
{% sample lang="hs" %}
2226
[import, lang:"haskell"](code/haskell/bubbleSort.hs)
2327
{% sample lang="cpp" %}
@@ -28,6 +32,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
2832
[import:3-18, lang:"d"](code/d/bubble_sort.d)
2933
{% sample lang="go" %}
3034
[import:7-21, lang:"go"](code/go/bubbleSort.go)
35+
{% sample lang="racket" %}
36+
[import:5-19, lang:"racket"](code/racket/bubbleSort.rkt)
3137
{% endmethod %}
3238

3339
... And that's it for the simplest bubble sort method.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function main()
2+
array = floor( rand(1,7)*100 );
3+
disp('Before Sorting:')
4+
disp(array)
5+
6+
array = bubble_sort(array);
7+
disp('After Sorting')
8+
disp(array)
9+
end
10+
11+
function sorted_array = bubble_sort(array)
12+
for i=1:length(array)
13+
for j=1:length(array)-i
14+
if array(j) > array(j+1)
15+
% swap elements in the list
16+
temp = array(j);
17+
array(j) = array(j+1);
18+
array(j+1) = temp;
19+
end
20+
end
21+
end
22+
sorted_array = array
23+
end
24+
25+
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
#!usr/bin/python3
2-
3-
#import section
41
import random
52

63

74
def bubble_sort(array):
8-
len_array = len(array)
9-
for i in range(len_array):
10-
for j in range(len_array - i - 1):
11-
if(array[j] > array[j+1]):
12-
array[j], array[j+1] = array[j+1], array[j] #swap elements in the list
13-
5+
len_array = len(array)
6+
for i in range(len_array):
7+
for j in range(len_array - i - 1):
8+
if(array[j] > array[j+1]):
9+
array[j], array[j+1] = array[j+1], array[j] #swap elements in the list
1410

1511
def main():
16-
number = [random.randint(0, 10000) for _ in range(10)]
17-
print("Before Sorting {}".format(number))
18-
bubble_sort(number)
19-
print("After Sorting {}".format(number))
20-
12+
number = [random.randint(0, 1000) for _ in range(10)]
13+
print("Before Sorting {}".format(number))
14+
bubble_sort(number)
15+
print("After Sorting {}".format(number))
2116

22-
if __name__ == "__main__":
23-
main()
17+
main()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#lang racket
2+
3+
(provide bubbleSort)
4+
5+
6+
(define bubbleSort
7+
(case-lambda [(l) (bubbleSort l (length l))]
8+
[(l n) (if (= n 1)
9+
l
10+
(bubbleSort (pass l 0 n) (- n 1)))]))
11+
12+
; a single pass, if this is the nth pass, then we know that the (n - 1) last elements are already sorted
13+
(define (pass l counter n)
14+
(let ([x (first l)]
15+
[y (second l)]
16+
[r (drop l 2)])
17+
(cond [(= (- n counter) 2) (cons (min x y) (cons (max x y) r))]
18+
[(cons (min x y) (pass (cons (max x y) r) (+ counter 1) n))])))
19+
20+
21+
((lambda (x) (display (bubbleSort x))) (read))

0 commit comments

Comments
 (0)