Skip to content

Commit 5982b36

Browse files
aig031keon
authored andcommitted
top_1.py & trimmean.py created (keon#345)
* Create top_1.py * Create trimmean.py * Rename top_1.py to algorithms/arrays/top_1.py * Rename trimmean.py to algorithms/arrays/trimmean.py * Update __init__.py * Update README.md * Update README_CN.md * Update README_CN.md * Update README_GE.md * Update README_JP.md * Update README_KR.md * Update test_array.py * Update test_array.py * Update test_array.py
1 parent 601202b commit 5982b36

9 files changed

Lines changed: 83 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ If you want to uninstall algorithms, it is as simple as:
6868
   - [rotate](algorithms/arrays/rotate.py)
6969
- [summarize_ranges](algorithms/arrays/summarize_ranges.py)
7070
- [three_sum](algorithms/arrays/three_sum.py)
71+
- [trimmean](algorithms/arrays/trimmean.py)
72+
- [top_1](algorithms/arrays/top_1.py)
7173
- [two_sum](algorithms/arrays/two_sum.py)
7274
- [move_zeros](algorithms/arrays/move_zeros.py)
7375
- [backtrack](algorithms/backtrack)

README_CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ pip3 uninstall -y algorithms
8585
- [rotate:反转数组](algorithms/arrays/rotate.py)
8686
- [summarize_ranges:数组范围](algorithms/arrays/summarize_ranges.py)
8787
- [three_sum:三数和为零](algorithms/arrays/three_sum.py)
88+
- [trimmean](algorithms/arrays/trimmean.py)
89+
- [top_1](algorithms/arrays/top_1.py)
8890
- [two_sum:两数和](algorithms/arrays/two_sum.py)
8991
- [move_zeros: 0后置问题](algorithms/arrays/move_zeros.py)
9092
- [backtrack:回溯](algorithms/backtrack)

README_GE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
7575
   - [rotate](algorithms/arrays/rotate.py)
7676
- [summarize_ranges](algorithms/arrays/summarize_ranges.py)
7777
- [three_sum](algorithms/arrays/three_sum.py)
78+
- [trimmean](algorithms/arrays/trimmean.py)
79+
- [top_1](algorithms/arrays/top_1.py)
7880
- [two_sum](algorithms/arrays/two_sum.py)
7981
- [move_zeros](algorithms/arrays/move_zeros.py)
8082
- [backtrack](algorithms/backtrack)

README_JP.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ if __name__ == "__main__":
6969
   - [rotate](algorithms/arrays/rotate.py)
7070
- [summarize_ranges](algorithms/arrays/summarize_ranges.py)
7171
- [three_sum](algorithms/arrays/three_sum.py)
72+
- [trimmean](algorithms/arrays/trimmean.py)
73+
- [top_1](algorithms/arrays/top_1.py)
7274
- [two_sum](algorithms/arrays/two_sum.py)
7375
- [move_zeros](algorithms/arrays/move_zeros.py)
7476
- [backtrack : バックトラッキング](algorithms/backtrack)

README_KR.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ if __name__ == "__main__":
6868
   - [rotate](algorithms/arrays/rotate.py)
6969
- [summarize_ranges](algorithms/arrays/summarize_ranges.py)
7070
- [three_sum](algorithms/arrays/three_sum.py)
71+
- [trimmean](algorithms/arrays/trimmean.py)
72+
- [top_1](algorithms/arrays/top_1.py)
7173
- [two_sum](algorithms/arrays/two_sum.py)
7274
- [move_zeros](algorithms/arrays/move_zeros.py)
7375
- [backtrack : 백트래킹](algorithms/backtrack)

algorithms/arrays/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@
1111
from .rotate import *
1212
from .summarize_ranges import *
1313
from .three_sum import *
14+
from .trimmean import *
15+
from .top_1 import *
1416
from .two_sum import *

algorithms/arrays/top_1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
this algorithms receive array and check most_frequent_value(a.k.a mode). Also, sometimes it can be have numerous most_frequent_value,
3+
so this funtion returns list. This result can be used as finding representative value on array.
4+
5+
This algorithms get array, and make dictionary of it, find most frequent count, and make result list.
6+
7+
For example) top_1([1, 1, 2, 2, 3, 4]) will return [1, 2]
8+
9+
Complexity: O(n)
10+
"""
11+
def top_1(arr):
12+
values = {}
13+
#reserve each value which first appears on keys
14+
#reserve how many time each value appears by index number on values
15+
result = []
16+
f_val = 0
17+
18+
for i in arr:
19+
if i in values:
20+
values[i] += 1
21+
else:
22+
values[i] = 1
23+
24+
f_val = max(values.values())
25+
26+
for i in values.keys():
27+
if values[i] == f_val:
28+
result.append(i)
29+
else:
30+
continue
31+
32+
return result

algorithms/arrays/trimmean.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
When make reliable means, we need to neglect best and worst value. For example, when making average score on athletes we need this option.
3+
So, this algorithms, fix some percentage to neglect when making mean. For example, if you suggest 20%, it will neglect best 10% value, and
4+
worst 10% value.
5+
6+
This algorithm gets array and percentage to neglect. After sorted, if index of array is larger or smaller or wanted ratio, we don't
7+
compute it.
8+
9+
Compleity: O(n)
10+
"""
11+
def trimmean(arr, per):
12+
ratio = per/200
13+
# /100 for easy calculation by *, and /2 for easy adaption to best and worst parts.
14+
cal_sum = 0
15+
# sum value to be calculated to trimmean.
16+
arr.sort()
17+
neg_val = int(len(arr)*ratio)
18+
arr = arr[neg_val:len(arr)-neg_val]
19+
for i in arr:
20+
cal_sum += i
21+
#print(cal_sum, len(arr))
22+
return cal_sum/len(arr)

tests/test_array.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
summarize_ranges,
1313
three_sum,
1414
two_sum,
15-
max_ones_index
15+
max_ones_index,
16+
trimmean,
17+
top_1
1618
)
1719

1820
import unittest
@@ -306,6 +308,20 @@ def test_two_sum(self):
306308
self.assertTupleEqual((0, 3), two_sum([-3, 5, 2, 3, 8, -9], target=0))
307309

308310
self.assertIsNone(two_sum([-3, 5, 2, 3, 8, -9], target=6))
311+
312+
class TestTrimmean(unittest.TestCase):
313+
314+
def test_trimmean(self):
315+
316+
self.assertEqual(trimmean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 20), 5.5)
317+
self.assertEqual(trimmean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 20), 6.0)
318+
319+
class TestTop1(unittest.TestCase):
320+
321+
def test_top_1(self):
322+
self.assertListEqual(top_1([1 , 1, 2, 2, 3]), [1, 2])
323+
self.assertListEqual(top_1([1, 2, 3, 324, 234, 23, 23, 1, 23, 23]), [23])
324+
309325

310326

311327
if __name__ == '__main__':

0 commit comments

Comments
 (0)