top_1.py & trimmean.py created by aig031 · Pull Request #345 · keon/algorithms · GitHub
Skip to content
2 changes: 2 additions & 0 deletions README.md
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pip3 uninstall -y algorithms
- [rotate:反转数组](algorithms/arrays/rotate.py)
- [summarize_ranges:数组范围](algorithms/arrays/summarize_ranges.py)
- [three_sum:三数和为零](algorithms/arrays/three_sum.py)
- [trimmean](algorithms/arrays/trimmean.py)
- [top_1](algorithms/arrays/top_1.py)
- [two_sum:两数和](algorithms/arrays/two_sum.py)
- [move_zeros: 0后置问题](algorithms/arrays/move_zeros.py)
- [backtrack:回溯](algorithms/backtrack)
Expand Down
2 changes: 2 additions & 0 deletions README_GE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
   - [rotate](algorithms/arrays/rotate.py)
- [summarize_ranges](algorithms/arrays/summarize_ranges.py)
- [three_sum](algorithms/arrays/three_sum.py)
- [trimmean](algorithms/arrays/trimmean.py)
- [top_1](algorithms/arrays/top_1.py)
- [two_sum](algorithms/arrays/two_sum.py)
- [move_zeros](algorithms/arrays/move_zeros.py)
- [backtrack](algorithms/backtrack)
Expand Down
2 changes: 2 additions & 0 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ if __name__ == "__main__":
   - [rotate](algorithms/arrays/rotate.py)
- [summarize_ranges](algorithms/arrays/summarize_ranges.py)
- [three_sum](algorithms/arrays/three_sum.py)
- [trimmean](algorithms/arrays/trimmean.py)
- [top_1](algorithms/arrays/top_1.py)
- [two_sum](algorithms/arrays/two_sum.py)
- [move_zeros](algorithms/arrays/move_zeros.py)
- [backtrack : バックトラッキング](algorithms/backtrack)
Expand Down
2 changes: 2 additions & 0 deletions README_KR.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ if __name__ == "__main__":
   - [rotate](algorithms/arrays/rotate.py)
- [summarize_ranges](algorithms/arrays/summarize_ranges.py)
- [three_sum](algorithms/arrays/three_sum.py)
- [trimmean](algorithms/arrays/trimmean.py)
- [top_1](algorithms/arrays/top_1.py)
- [two_sum](algorithms/arrays/two_sum.py)
- [move_zeros](algorithms/arrays/move_zeros.py)
- [backtrack : 백트래킹](algorithms/backtrack)
Expand Down
2 changes: 2 additions & 0 deletions algorithms/arrays/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
from .rotate import *
from .summarize_ranges import *
from .three_sum import *
from .trimmean import *
from .top_1 import *
from .two_sum import *
32 changes: 32 additions & 0 deletions algorithms/arrays/top_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
this algorithms receive array and check most_frequent_value(a.k.a mode). Also, sometimes it can be have numerous most_frequent_value,
so this funtion returns list. This result can be used as finding representative value on array.

This algorithms get array, and make dictionary of it, find most frequent count, and make result list.

For example) top_1([1, 1, 2, 2, 3, 4]) will return [1, 2]

Complexity: O(n)
"""
def top_1(arr):
values = {}
#reserve each value which first appears on keys
#reserve how many time each value appears by index number on values
result = []
f_val = 0

for i in arr:
if i in values:
values[i] += 1
else:
values[i] = 1

f_val = max(values.values())

for i in values.keys():
if values[i] == f_val:
result.append(i)
else:
continue

return result
22 changes: 22 additions & 0 deletions algorithms/arrays/trimmean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
When make reliable means, we need to neglect best and worst value. For example, when making average score on athletes we need this option.
So, this algorithms, fix some percentage to neglect when making mean. For example, if you suggest 20%, it will neglect best 10% value, and
worst 10% value.

This algorithm gets array and percentage to neglect. After sorted, if index of array is larger or smaller or wanted ratio, we don't
compute it.

Compleity: O(n)
"""
def trimmean(arr, per):
ratio = per/200
# /100 for easy calculation by *, and /2 for easy adaption to best and worst parts.
cal_sum = 0
# sum value to be calculated to trimmean.
arr.sort()
neg_val = int(len(arr)*ratio)
arr = arr[neg_val:len(arr)-neg_val]
for i in arr:
cal_sum += i
#print(cal_sum, len(arr))
return cal_sum/len(arr)
18 changes: 17 additions & 1 deletion tests/test_array.py