Skip to content

Commit 9dc7611

Browse files
coditoriegonSchiele
authored andcommitted
Julia samples (egonSchiele#108)
* Add julialang binary search sample * Add unit test * Add julialang selection sort sample * Change julia suffix to jl * Add recursion and quick sort with tests * add quick sort * Add hash table samples * Add BFS * Changed file names * Add dijkstras * Add Dijkstras test
1 parent 30bbe9c commit 9dc7611

File tree

14 files changed

+255
-0
lines changed

14 files changed

+255
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Test
2+
3+
function find_smallest(arr)
4+
smallest = arr[1]
5+
smallest_index = 1
6+
for i = 1:length(arr)
7+
if arr[i] < smallest
8+
smallest = arr[i]
9+
smallest_index = i
10+
end
11+
end
12+
return smallest_index
13+
end
14+
15+
function selection_sort(arr)
16+
new_arr = Array{Int64}(undef,0)
17+
for i = 1:length(arr)
18+
smallest = find_smallest(arr)
19+
append!(new_arr,arr[smallest])
20+
deleteat!(arr,smallest)
21+
end
22+
return new_arr
23+
end
24+
25+
arr = [5,3,6,2,10]
26+
@test selection_sort(arr) == [2,3,5,6,10]

03_recursion/julia/01_countdown.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Test
2+
using Suppressor
3+
4+
function countdown(i)
5+
println(i)
6+
if i <= 0
7+
return
8+
end
9+
countdown(i-1)
10+
end
11+
12+
result = @capture_out(countdown(10)) # return stdout result
13+
@test result == "10
14+
9
15+
8
16+
7
17+
6
18+
5
19+
4
20+
3
21+
2
22+
1
23+
0
24+
"

03_recursion/julia/02_greet.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Test
2+
using Suppressor
3+
4+
function greet2(name)
5+
println("how are you, ",name,"?")
6+
end
7+
8+
function bye()
9+
println("ok bye!")
10+
end
11+
12+
function greet(name)
13+
println("hello, ",name,"!")
14+
greet2(name)
15+
println("getting ready to say bye...")
16+
bye()
17+
end
18+
19+
result = @capture_out greet("adit")
20+
@test result == "hello, adit!
21+
how are you, adit?
22+
getting ready to say bye...
23+
ok bye!
24+
"

03_recursion/julia/03_factorial.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Test
2+
3+
function factorial(x)
4+
if x == 1
5+
return 1
6+
end
7+
return x * factorial(x-1)
8+
end
9+
10+
@test factorial(5) == 120

04_quicksort/julia/01_loop_sum.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Test
2+
3+
function sum(arr)
4+
total = 0
5+
for x in arr
6+
total += x
7+
end
8+
return total
9+
end
10+
11+
@test sum([1,2,3,4]) == 10
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Test
2+
3+
function sum(arr)
4+
if isempty(arr)
5+
return 0
6+
end
7+
return arr[1] + sum(arr[2:end])
8+
end
9+
10+
@test sum([1,2,3,4]) == 10
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Test
2+
3+
function count(arr)
4+
if isempty(arr)
5+
return 0
6+
end
7+
return 1 + count(arr[2:end])
8+
end
9+
10+
@test count([1,2,3,4]) == 4
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Test
2+
3+
function max_(arr)
4+
if length(arr) == 1
5+
return arr[1]
6+
end
7+
sub_max = max_(arr[2:end])
8+
return arr[1] > sub_max ? arr[1] : sub_max
9+
end
10+
11+
@test max_([1,5,8,20,9,10]) == 20

04_quicksort/julia/05_quick_sort.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Test
2+
3+
function quick_sort(arr)
4+
if length(arr) < 2 return arr end
5+
pivot = arr[1]
6+
less = [i for i in arr[2:end] if i <= pivot]
7+
greater = [i for i in arr[2:end] if i > pivot]
8+
return vcat(quick_sort(less), [pivot], quick_sort(greater))
9+
end
10+
11+
@test quick_sort([3,5,2,1,4]) == [1,2,3,4,5]

0 commit comments

Comments
 (0)