forked from TheAlgorithms/Julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Average mean * Average Median * Average mode
- Loading branch information
1 parent
f69fa9a
commit 487b785
Showing
5 changed files
with
175 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters