Skip to content

Commit

Permalink
Mean,Mode,Median (TheAlgorithms#29)
Browse files Browse the repository at this point in the history
* Average mean

* Average Median

* Average mode
  • Loading branch information
ashwani-rathee committed Jun 13, 2021
1 parent f69fa9a commit 487b785
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 64 deletions.
7 changes: 7 additions & 0 deletions src/TheAlgorithms.jl
Expand Up @@ -34,6 +34,9 @@ export collatz_sequence
export euler_method
export is_armstrong
export line_length
export mean
export median
export mode
export SIR # TODO: make the name lowercase if possible
export surfarea_cube
export surfarea_sphere
Expand Down Expand Up @@ -69,6 +72,9 @@ include("math/abs.jl")
include("math/area_under_curve.jl")
include("math/armstrong_number.jl")
include("math/area.jl")
include("math/average_mean.jl")
include("math/average_median.jl")
include("math/average_mode.jl")
include("math/ceil_floor.jl")
include("math/collatz_sequence.jl")
include("math/euler_method.jl")
Expand All @@ -77,6 +83,7 @@ include("math/sir_model.jl")




# Includes: matrix
include("matrix/lu_decompose.jl") # used by determinant.jl
include("matrix/determinant.jl")
Expand Down
18 changes: 18 additions & 0 deletions src/math/average_mean.jl
@@ -0,0 +1,18 @@
"""
mean(nums)
Find mean of a vector of numbers
# Example
```julia
mean([3, 6, 9, 12, 15, 18, 21]) # returns 12.0
mean([5, 10, 15, 20, 25, 30, 35]) # returns 20.0
mean([1, 2, 3, 4, 5, 6, 7, 8]) # returns 4.5
```
Contributed By:- [Ashwani Rathee](https://github.com/ashwani-rathee)
"""
function mean(nums)
return sum(nums) / length(nums)
end
23 changes: 23 additions & 0 deletions src/math/average_median.jl
@@ -0,0 +1,23 @@
include("ceil_floor.jl")

"""
median(nums)
Finds median of a vector of numbers
# Example
```julia
median([2,1,3,4]) # returns 2.5
median([2, 70, 6, 50, 20, 8, 4]) # returns 8
median([0]) # returns 0
```
Contributed By:- [Ashwani Rathee](https://github.com/ashwani-rathee)
"""
function median(nums)
sorted = sort(nums)
len = length(sorted)
mid_index = Int(floor_val(len / 2))
return len % 2 == 0 ? (sorted[mid_index+1] + sorted[mid_index]) / 2 : sorted[mid_index+1]
end
43 changes: 43 additions & 0 deletions src/math/average_mode.jl
@@ -0,0 +1,43 @@
"""
mode(nums)
Finds mode of a vector of numbers
# Example
```julia
mode([2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2]) # returns [2]
mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2]) # returns [2]
mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2]) # returns [2, 4]
mode(["x", "y", "y", "z"]) # returns ["y"]
mode(["x", "x" , "y", "y", "z"]) # returns ["x", "y"]
```
Contributed By:- [Ashwani Rathee](https://github.com/ashwani-rathee)
"""
function mode(nums)
dict = Dict() # nums => Number of repetitions
result = [] # Array of the modes so far
max = 0 # Max of repetitions so far

for i in nums
# Add one to the dict[i] entry (create one if none)
if i in keys(dict)
dict[i] += 1
else
dict[i] = 1
end
# Result updated if no of repetitions of i >= max
if dict[i] >= max
if dict[i] > max
empty!(result)
max += 1
end
append!(result, [i])
end
end

return result
end
println(mode([1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17]))
println(mode((1, 1, 2, 4, 4)))
148 changes: 84 additions & 64 deletions test/math.jl
@@ -1,60 +1,5 @@
@testset "Math" begin

@testset "Math: Area Under Curve" begin
# Area by Trapazoid rule
@test trapazoidal_area(x -> 5, 12, 14, 1000) == 10.00000000000334
@test trapazoidal_area(x -> 9 * x^2, -4, 0, 1000) == 192.0000960000001
@test trapazoidal_area(x -> 9 * x^2, -4, 4, 1000) == 384.0007680000011
end

@testset "Math: Collatz Sequence" begin
@test collatz_sequence(3) == [3,10,5,16,8,4,2,1]
@test collatz_sequence(42) == [42,21,64,32,16,8,4,2,1]
@test collatz_sequence(5) == [5,16,8,4,2,1]
end

@testset "Math: Euler Method" begin
@test euler_method((x, t) -> x, 1, (0, 5))[1][end] == 143.33937864611195
end

@testset "Math: Line Length" begin
# Arc Lengths
@test line_length(x -> x, 0, 1, 10) == 1.4142135623730947
@test line_length(x -> 1, -5.5, 4.5) == 9.999999999999977
@test line_length(x -> sin(5 * x) + cos(10 * x) + 0.1 * x^2, 0, 10, 10000) == 69.53493003183544
end

@testset "Math: SIR Model" begin
# TODO: implement tests

#initian conditions
p = [0.5/7900000.0,0.33]
u0 = [7900000.0,10.0,0.0]
tspan = (0.0,140.0)

#solve
sir = ODEProblem(SIR,u0,tspan,p)
sol = solve(sir)

#plot
plot(sol)
end

@testset "Math: Armstrong Number" begin
x = 370 # an armstrong number
@test is_armstrong(x) == true
x = 225 # Not an armstrong number
@test is_armstrong(x) == false
x = -23 # Not an armstrong number
@test is_armstrong(x) == false
x = 153 # an armstrong number
@test is_armstrong(x) == true
x = 0 # an armstrong number
@test is_armstrong(x) == true
x = 12 # Not an armstrong number
@test is_armstrong(x) == false
end

@testset "Math: abs.jl(Absolute Value) " begin
@test abs_val(-100) == 100
@test abs_val(0) == 0
Expand All @@ -71,16 +16,12 @@ end
@test abs_min([-7,-3,6]) == -3
end

@testset "Math: ceil_floor" begin
@test ceil_val(1.3) == 2.0
@test ceil_val(2.0) == 2.0
@test ceil_val(-1.5) == -1.0

@test floor_val(1.3) == 1
@test floor_val(2.0) == 2.0
@test floor_val(-1.7) == -2.0
@testset "Math: Area Under Curve" begin
# Area by Trapazoid rule
@test trapazoidal_area(x -> 5, 12, 14, 1000) == 10.00000000000334
@test trapazoidal_area(x -> 9 * x^2, -4, 0, 1000) == 192.0000960000001
@test trapazoidal_area(x -> 9 * x^2, -4, 4, 1000) == 384.0007680000011
end

@testset "Math: Area" begin
@test surfarea_cube(1) == 6
@test surfarea_cube(3) == 54
Expand Down Expand Up @@ -132,4 +73,83 @@ end
@test_throws DomainError area_rhombus(1,-2)
@test_throws DomainError area_rhombus(-1,2)
end

@testset "Math: Armstrong Number" begin
x = 370 # an armstrong number
@test is_armstrong(x) == true
x = 225 # Not an armstrong number
@test is_armstrong(x) == false
x = -23 # Not an armstrong number
@test is_armstrong(x) == false
x = 153 # an armstrong number
@test is_armstrong(x) == true
x = 0 # an armstrong number
@test is_armstrong(x) == true
x = 12 # Not an armstrong number
@test is_armstrong(x) == false
end

@testset "Math: Average Mean" begin
@test mean([3, 6, 9, 12, 15, 18, 21]) == 12.0
@test mean([5, 10, 15, 20, 25, 30, 35]) == 20.0
@test mean([1, 2, 3, 4, 5, 6, 7, 8]) == 4.5
end

@testset "Math: Average Mode" begin
@test mode([2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2]) == [2]
@test mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2]) == [2]
@test mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2]) == [4, 2]
@test mode(["x", "y", "y", "z"]) == ["y"]
@test mode(["x", "x" , "y", "y", "z"]) == ["x", "y"]
end

@testset "Math: Average Median" begin
@test median([2,1,3,4]) == 2.5
@test median([2, 70, 6, 50, 20, 8, 4]) == 8
@test median([0]) == 0
end

@testset "Math: ceil_floor" begin
@test ceil_val(1.3) == 2.0
@test ceil_val(2.0) == 2.0
@test ceil_val(-1.5) == -1.0

@test floor_val(1.3) == 1
@test floor_val(2.0) == 2.0
@test floor_val(-1.7) == -2.0
end

@testset "Math: Collatz Sequence" begin
@test collatz_sequence(3) == [3,10,5,16,8,4,2,1]
@test collatz_sequence(42) == [42,21,64,32,16,8,4,2,1]
@test collatz_sequence(5) == [5,16,8,4,2,1]
end

@testset "Math: Euler Method" begin
@test euler_method((x, t) -> x, 1, (0, 5))[1][end] == 143.33937864611195
end

@testset "Math: Line Length" begin
# Arc Lengths
@test line_length(x -> x, 0, 1, 10) == 1.4142135623730947
@test line_length(x -> 1, -5.5, 4.5) == 9.999999999999977
@test line_length(x -> sin(5 * x) + cos(10 * x) + 0.1 * x^2, 0, 10, 10000) == 69.53493003183544
end

@testset "Math: SIR Model" begin
# TODO: implement tests

#initian conditions
p = [0.5/7900000.0,0.33]
u0 = [7900000.0,10.0,0.0]
tspan = (0.0,140.0)

#solve
sir = ODEProblem(SIR,u0,tspan,p)
sol = solve(sir)

#plot
plot(sol)
end

end

0 comments on commit 487b785

Please sign in to comment.