Skip to content

Added flood fill in Coconut #743

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

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a18e7cc
Adding flood fill in Coconut
Amaras Aug 5, 2020
7d2739b
Correcting copy-paste fails
Amaras Aug 5, 2020
5da3ae8
Flood Fill in C (#741)
Gathros Aug 6, 2020
d21dacd
Huffman Encoding Scratch Implementation (#682)
dovisutu Sep 10, 2020
aa08db0
Update monte_carlo.py (#751)
DougBR Sep 10, 2020
a4dbc2a
Fixed infinite loop issue in "euclidean_example.py" (#749)
valdaarhun Sep 11, 2020
9dcc06f
fix both error of issue 739 (#756)
Oct 12, 2020
627aed9
Add Flood Fill in Python (#753)
lazyprop Oct 12, 2020
fee7e83
Remove color function from flood fill because it is redundant (#766)
leios Oct 15, 2020
85f8e9a
Added Bogosort in Coconut (#735)
Amaras Oct 15, 2020
18944ea
Added Bubblesort in Coconut (#736)
Amaras Oct 15, 2020
f1aa057
Add Scheme alongside Racket in EditorConfig (#779)
berquist Nov 4, 2020
7bc0e22
Added CSS for printing (#777)
0xJonas Nov 5, 2020
91d5625
Added Cooley-Tukey in Lisp
0xJonas Nov 11, 2020
5a6ebd0
Make Euclid's algorithm in Nim more idiomatic (#784)
berquist Nov 11, 2020
153317d
Make Verlet integration in Nim more idiomatic (#782)
berquist Nov 11, 2020
0ca5ac6
Make Bogosort in Nim more idiomatic (#783)
berquist Nov 11, 2020
7ada7da
Formatting and refactoring
0xJonas Dec 27, 2020
c72caaf
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
70822e9
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
9d0a5bb
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
2793edf
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
8426216
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
e92bcb7
Update contents/cooley_tukey/code/clisp/fft.lisp
0xJonas Dec 28, 2020
e631b57
Updated line number
0xJonas Dec 28, 2020
02dd353
Merge pull request #786 from 0xJonas/cooley-tukey-lisp
Trashtalk217 Jan 1, 2021
f9a6eda
Add Computus in Nim (#780)
berquist Jan 2, 2021
b49f034
Domain Coloring in Python using Matplotlib (#788)
0xJonas Jan 4, 2021
9d7349a
adding gif to show people how to change languages (#797)
leios Jan 25, 2021
5be74ec
Adding flood fill in Coconut
Amaras Aug 5, 2020
1949a10
Correcting copy-paste fails
Amaras Aug 5, 2020
a5c0f6b
Addressed part of Trashtalk's review
Amaras Feb 4, 2021
db00314
Merge remote-tracking branch 'origin/flood_fill_coconut' into flood_f…
Amaras Feb 4, 2021
f312994
Got closer to Julia's code logic + reworked the line numbers
Amaras Feb 4, 2021
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 .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ indent_size = 4
indent_style = space
indent_size = 4

# Racket
[*.rkt]
# Racket/Scheme
[*.{rkt,ss,scm}]
indent_style = space
indent_size = 2

Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- James Goytia
- Amaras
- Jonathan Dönszelmann
- Ishaan Verma
- Delphi1024
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
The Arcane Algorithm Archive is a collaborative effort to create a guide for all important algorithms in all languages.
This goal is obviously too ambitious for a book of any size, but it is a great project to learn from and work on and will hopefully become an incredible resource for programmers in the future.

To change the language, please use the UI at the top of the site:

<p>
<img class="center" src="res/languages.gif" style="width:50%" />
</p>

Here are some essential links:

- Book / website: <https://www.algorithm-archive.org/>
Expand Down
4 changes: 4 additions & 0 deletions contents/bogo_sort/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ In code, it looks something like this:
[import:12-16, lang:"scala"](code/scala/bogo.scala)
{% sample lang="go" %}
[import:27-31, lang:"go"](code/go/bogo_sort.go)
{% sample lang="coco" %}
[import:6-8, lang:"coconut"](code/coconut/bogo.coco)
{% endmethod %}

That's it.
Expand Down Expand Up @@ -137,6 +139,8 @@ We are done here!
[import, lang:"scala"](code/scala/bogo.scala)
{% sample lang="go" %}
[import, lang:"go"](code/go/bogo_sort.go)
{% sample lang="coco" %}
[import, lang:"coconut"](code/coconut/bogo.coco)
{% endmethod %}


Expand Down
15 changes: 15 additions & 0 deletions contents/bogo_sort/code/coconut/bogo.coco
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import random


is_sorted = l -> (l, l[1:]) |*> zip |> map$(t -> t[0] <= t[1]) |> all

def bogo_sort(a):
while not is_sorted(a):
random.shuffle(a)


if __name__ == '__main__':
a = [1, 3, 2, 4]
print('Unsorted:', a)
bogo_sort(a)
print('Sorted:', a)
33 changes: 16 additions & 17 deletions contents/bogo_sort/code/nim/bogo_sort.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ import random
randomize()

proc print_array(a: openArray[int]) =
for n in 0 .. len(a)-1:
echo a[n]
for n in 0 .. len(a)-1:
echo a[n]

proc is_sorted(a: openArray[int]): bool =
for n in 1 .. len(a)-1:
if a[n] > a[n-1]:
return false

return true
func is_sorted(a: openArray[int]): bool =
result = true
for n in 1 .. len(a)-1:
if a[n] > a[n-1]:
result = false
break

proc bogo_sort(a: var openArray[int]) =
while not is_sorted(a):
shuffle(a)

while not is_sorted(a):
shuffle(a)

var x: array[10,int] = [32,32,64,16,128,8,256,4,512,2]

print_array(x)
bogo_sort(x)
echo "\n"
print_array(x)
when isMainModule:
var x = [32, 32, 64, 16, 128, 8, 256, 4, 512, 2]
print_array(x)
bogo_sort(x)
echo "\n"
print_array(x)
4 changes: 4 additions & 0 deletions contents/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:1-6, lang:"coffeescript"](code/coffeescript/bubblesort.coffee)
{% sample lang="r" %}
[import:1-12, lang:"r"](code/r/bubble_sort.R)
{% sample lang="coco" %}
[import:3-10, lang:"coconut"](code/coconut/bubblesort.coco)
{% endmethod %}

... And that's it for the simplest bubble sort method.
Expand Down Expand Up @@ -159,6 +161,8 @@ The code snippet was taken from this [Scratch project](https://scratch.mit.edu/p
[import, lang:"coffeescript"](code/coffeescript/bubblesort.coffee)
{% sample lang="r" %}
[import, lang:"r"](code/r/bubble_sort.R)
{% sample lang="coco" %}
[import, lang:"coconut"](code/coconut/bubblesort.coco)
{% endmethod %}

<script>
Expand Down
16 changes: 16 additions & 0 deletions contents/bubble_sort/code/coconut/bubblesort.coco
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import random

def swap_till(x, []) = [x]

addpattern def swap_till(x, y is int) = [min(x, y), max(x, y)]

addpattern def swap_till(x, [y] + rest) =
[min(x, y)] + swap_till(max(x, y), rest)

bubble_sort = reduce$((x, y) -> swap_till(y, x))

if __name__ == '__main__':
array = range(10) |> map$(-> random.randint(0, 1000)) |> list
print('Before sorting:', array)
array = bubble_sort(array) |> list
print('After sorting:', array)
45 changes: 45 additions & 0 deletions contents/computus/code/nim/gauss_easter.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import strformat

func computus(year: int, servois: bool = false): string =
let
# Year's position on the 19 year metonic cycle
a = year mod 19
# Century index
k = year div 100
# Shift of metonic cycle, add a day offset every 300 years
p = (13 + 8 * k) div 25
# Correction for non-observed leap days
q = k div 4
# Correction to starting point of calculation each century
m = (15 - p + k - q) mod 30
# Number of days from March 21st until the full moon
d = (19 * a + m) mod 30
# Returning of user wants value for Servois' table
if servois:
return $((21 + d) mod 31)
let
# Find the next Sunday
# Century-based offset in weekly calculation
n = (4 + k - q) mod 7
# Correction for leap days
b = year mod 4
c = year mod 7
# Days from d to next Sunday
temp_e = (2 * b + 4 * c + 6 * d + n) mod 7
# Historical corrections for April 26 and 25
e = if (d == 29 and temp_e == 6) or (d == 28 and temp_e == 6 and a > 10):
-1
else:
temp_e
# Determination of the correct month for Easter
if (22 + d + e) > 31:
result = "April {d + e - 9}".fmt
else:
result = "March {22 + d + e}".fmt

when isMainModule:
echo "The following are the dates of the Paschal full moon (using Servois "
echo "notation) and the date of Easter for 2020-2030 AD:"
echo "Year Servois number Easter"
for year in 2020..2030:
echo "{year} {computus(year, true):14} {computus(year, false):6}".fmt
2 changes: 2 additions & 0 deletions contents/computus/computus.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ For now, we have the code outputting a tuple of $$d$$ and $$e$$, so users can us
[import, lang:"cpp"](code/c++/gauss_easter.cpp)
{% sample lang="lisp" %}
[import, lang:"lisp"](code/clisp/gauss-easter.lisp)
{% sample lang="nim" %}
[import, lang:"nim"](code/nim/gauss_easter.nim)
{% endmethod %}


Expand Down
113 changes: 113 additions & 0 deletions contents/cooley_tukey/code/clisp/fft.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@

(defun coefficient (time-index freq-index dft-len)
"Calculates a single twiddle factor for the Fourier Transform."
(exp (- (/ (* #c(0 1) 2.0 pi time-index freq-index)
dft-len))))

(defun dft (data)
"Performs the Discrete Fourier Transform"
(let ((dft-len (length data)))
(loop for freq-index from 0 below dft-len collect
(loop for time-index from 0 below dft-len sum
(* (coefficient time-index freq-index dft-len) (elt data time-index))))))

(defun merge-sub-ffts (evens odds)
"Combines the FFTs of the even and odd indices."
(let* ((fft-length (+ (length evens) (length odds)))
;; Calculate coefficients for the odd indices.
(twiddle-factors (loop for i from 0 below (length odds)
collect (coefficient 1.0 i fft-length)))
;; Multiply values with coefficients.
(odd-terms (mapcar #'* odds twiddle-factors)))
;; Combine the two FFTs.
(concatenate 'list
(mapcar #'+ evens odd-terms)
(mapcar #'- evens odd-terms))))

(defun cooley-tukey-rec (data)
"Performs the Fourier Transform using the recursive Cooley-Tukey method."
(if (<= (length data) 1)
data
(loop
for i from 0 below (length data)
;; Split even and odd indexed elements into two seperate lists.
if (evenp i)
collect (elt data i) into evens
else
collect (elt data i) into odds
finally
;; Calculate the Fourier Transform for the two smaller lists and
;; combine them into the Fourier Transform of the full input.
(return (merge-sub-ffts (cooley-tukey-rec evens)
(cooley-tukey-rec odds))))))

(defun reverse-bits (value num-bits)
"Reverses the bits of a value"
(if (= num-bits 1)
value
;; Split bits into two parts.
(let* ((num-low-bits (floor (/ num-bits 2)))
(num-high-bits (- num-bits num-low-bits))
(bit-mask (- (expt 2 num-low-bits) 1))
(lower-half (logand value bit-mask))
(upper-half (ash value (- num-low-bits))))
;; Reverse the bits of each part, then swap the results.
(logior (ash (reverse-bits lower-half num-low-bits) num-high-bits)
(reverse-bits upper-half num-high-bits)))))

(defun bit-shuffle-indices (data)
"Rearanges the elements in a list according to their bit-reversed indices."
(loop
with num-bits = (floor (log (length data) 2))
for i from 0 below (length data)
collect (elt data (reverse-bits i num-bits))))

(defun butterfly (a b coeff)
"Calculates a single butterfly."
(values (+ a (* coeff b)) (- a (* coeff b))))

(defun butterfly-group (data start stride)
"Calculates a single group of butterflies."
(dotimes (i stride)
;; Take two elements which are stride apart and perform a butterfly on them.
(let* ((first-elt-index (+ start i))
(second-elt-index (+ start i stride))
(first-elt (elt data first-elt-index))
(second-elt (elt data second-elt-index))
(coeff (coefficient 1.0 i (* 2 stride))))
(multiple-value-bind (sum difference) (butterfly first-elt second-elt coeff)
;; Write results back into the list.
(setf (elt data first-elt-index) sum)
(setf (elt data second-elt-index) difference)))))

(defun cooley-tukey-iter (data)
"Performs the Fourier Transform using the iterative Cooley-Tukey method."
(loop
;; Bit-shuffle indices.
with shuffled-data = (bit-shuffle-indices data)
for stride = 1 then (* 2 stride)
while (< stride (length shuffled-data))
do
;; Compute butterfly groups for the current stride.
(loop for i from 0 below (length shuffled-data) by (* 2 stride) do
(butterfly-group shuffled-data i stride))
finally (return shuffled-data)))

(defun approx-eql (list1 list2)
(let ((diffs (mapcar #'(lambda (e1 e2) (abs (- e1 e2)))
list1
list2)))
(loop for d in diffs always (< d 1e-9))))

(defun test-fft (data)
(let ((dft-result (dft data))
(rec-result (cooley-tukey-rec data))
(iter-result (cooley-tukey-iter data)))
(format T "~&DFT and recursive Cooley-Tukey approx. equal: ~a"
(approx-eql dft-result rec-result))
(format T "~&DFT and iterative Cooley-Tukey approx. equal: ~a"
(approx-eql dft-result iter-result))
(format T "~&Recursive Cooley-Tukey and iterative Cooley-Tukey approx. equal: ~a"
(approx-eql rec-result iter-result))))

(test-fft '(0.0 0.25 0.5 0.75 0.0 -0.25 -0.5 -0.75))
6 changes: 6 additions & 0 deletions contents/cooley_tukey/cooley_tukey.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ For some reason, though, putting code to this transformation really helped me fi
[import:3-15, lang:"javascript"](code/javascript/fft.js)
{% sample lang="rs" %}
[import:24-37, lang:"rust"](code/rust/fft.rs)
{% sample lang="lisp" %}
[import:2-12, lang:"lisp"](code/clisp/fft.lisp)
{% endmethod %}

In this function, we define `n` to be a set of integers from $$0 \rightarrow N-1$$ and arrange them to be a column.
Expand Down Expand Up @@ -142,6 +144,8 @@ In the end, the code looks like:
[import:17-39, lang="javascript"](code/javascript/fft.js)
{% sample lang="rs" %}
[import:39-55, lang:"rust"](code/rust/fft.rs)
{% sample lang="lisp" %}
[import:14-42, lang:"lisp"](code/clisp/fft.lisp)
{% endmethod %}

As a side note, we are enforcing that the array must be a power of 2 for the operation to work.
Expand Down Expand Up @@ -255,6 +259,8 @@ Some rather impressive scratch code was submitted by Jie and can be found here:
[import, lang:"javascript"](code/javascript/fft.js)
{% sample lang="rs" %}
[import, lang:"rust"](code/rust/fft.rs)
{% sample lang="lisp" %}
[import, lang:"lisp"](code/clisp/fft.lisp)
{% endmethod %}

<script>
Expand Down
Loading