Julia samples (#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
This commit is contained in:
Massoud Afrashteh
2019-11-12 18:49:21 +03:30
committed by Aditya Bhargava
parent 30bbe9cf01
commit 9dc7611411
14 changed files with 255 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
using Test
function sum(arr)
total = 0
for x in arr
total += x
end
return total
end
@test sum([1,2,3,4]) == 10

View File

@@ -0,0 +1,10 @@
using Test
function sum(arr)
if isempty(arr)
return 0
end
return arr[1] + sum(arr[2:end])
end
@test sum([1,2,3,4]) == 10

View File

@@ -0,0 +1,10 @@
using Test
function count(arr)
if isempty(arr)
return 0
end
return 1 + count(arr[2:end])
end
@test count([1,2,3,4]) == 4

View File

@@ -0,0 +1,11 @@
using Test
function max_(arr)
if length(arr) == 1
return arr[1]
end
sub_max = max_(arr[2:end])
return arr[1] > sub_max ? arr[1] : sub_max
end
@test max_([1,5,8,20,9,10]) == 20

View File

@@ -0,0 +1,11 @@
using Test
function quick_sort(arr)
if length(arr) < 2 return arr end
pivot = arr[1]
less = [i for i in arr[2:end] if i <= pivot]
greater = [i for i in arr[2:end] if i > pivot]
return vcat(quick_sort(less), [pivot], quick_sort(greater))
end
@test quick_sort([3,5,2,1,4]) == [1,2,3,4,5]