Skip to content

Commit 2f361dd

Browse files
aig031goswami-rahul
authored andcommitted
created limit.py in /algorithms/arrays (keon#353)
* 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 * Create limit.py * Update __init__.py * Update test_array.py * Update README.md * Update README_CN.md * Update README_GE.md * Update README_JP.md * Update README_KR.md * Update test_array.py * Update limit.py * Update test_array.py
1 parent 5fd35ca commit 2f361dd

8 files changed

Lines changed: 46 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ If you want to uninstall algorithms, it is as simple as:
6060
- [flatten](algorithms/arrays/flatten.py)
6161
- [garage](algorithms/arrays/garage.py)
6262
- [josephus_problem](algorithms/arrays/josephus.py)
63+
- [limit](algorithms/arrays/limit.py)
6364
- [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
6465
- [max_ones_index](algorithms/arrays/max_ones_index.py)
6566
- [merge_intervals](algorithms/arrays/merge_intervals.py)

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pip3 uninstall -y algorithms
7878
- [garage:停车场](algorithms/arrays/garage.py)
7979
- [josephus_problem: 约瑟夫问题](algorithms/arrays/josephus.py)
8080
- [max_ones_index](algorithms/arrays/max_ones_index.py)
81+
- [limit](algorithms/arrays/limit.py)
8182
- [longest_non_repeat:最长不重复子串](algorithms/arrays/longest_non_repeat.py/)
8283
- [merge_intervals:合并重叠间隔](algorithms/arrays/merge_intervals.py)
8384
- [missing_ranges:遗失的范围](algorithms/arrays/missing_ranges.py)

README_GE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
6767
- [flatten](algorithms/arrays/flatten.py)
6868
- [garage](algorithms/arrays/garage.py)
6969
- [josephus_problem](algorithms/arrays/josephus.py)
70+
- [limit](algorithms/arrays/limit.py)
7071
- [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
7172
- [max_ones_index](algorithms/arrays/max_ones_index.py)
7273
- [merge_intervals](algorithms/arrays/merge_intervals.py)

README_JP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ if __name__ == "__main__":
6161
- [flatten](algorithms/arrays/flatten.py)
6262
- [garage](algorithms/arrays/garage.py)
6363
- [josephus_problem](algorithms/arrays/josephus.py)
64+
- [limit](algorithms/arrays/limit.py)
6465
- [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
6566
- [max_ones_index](algorithms/arrays/max_ones_index.py)
6667
- [merge_intervals](algorithms/arrays/merge_intervals.py)

README_KR.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ if __name__ == "__main__":
6060
- [flatten](algorithms/arrays/flatten.py)
6161
- [garage](algorithms/arrays/garage.py)
6262
- [josephus_problem](algorithms/arrays/josephus.py)
63+
- [limit](algorithms/arrays/limit.py)
6364
- [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
6465
- [max_ones_index](algorithms/arrays/max_ones_index.py)
6566
- [merge_intervals](algorithms/arrays/merge_intervals.py)

algorithms/arrays/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
from .trimmean import *
1515
from .top_1 import *
1616
from .two_sum import *
17+
from .limit import *

algorithms/arrays/limit.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Sometimes you need to limit array result to use. Such as you only need the value over
3+
10 or, you need value under than 100. By use this algorithms, you can limit your array
4+
to specific value
5+
6+
If array, Min, Max value was given, it returns array that contains values of given array
7+
which was larger than Min, and lower than Max. You need to give 'unlimit' to use only Min
8+
or Max.
9+
10+
ex) limit([1,2,3,4,5], None, 3) = [1,2,3]
11+
12+
Complexity = O(n)
13+
"""
14+
15+
def limit(arr, min_lim = None, max_lim = None):
16+
result = []
17+
if min_lim == None:
18+
for i in arr:
19+
if i<= max_lim:
20+
result.append(i)
21+
elif max_lim == None:
22+
for i in arr:
23+
if i >= min_lim:
24+
result.append(i)
25+
else:
26+
for i in arr:
27+
if i >= min_lim and i <= max_lim:
28+
result.append(i)
29+
30+
return result

tests/test_array.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
two_sum,
1515
max_ones_index,
1616
trimmean,
17-
top_1
17+
top_1,
18+
limit
1819
)
1920

2021
import unittest
@@ -322,6 +323,14 @@ def test_top_1(self):
322323
self.assertListEqual(top_1([1 , 1, 2, 2, 3]), [1, 2])
323324
self.assertListEqual(top_1([1, 2, 3, 324, 234, 23, 23, 1, 23, 23]), [23])
324325

326+
class TestLimit(unittest.TestCase):
327+
328+
def test_limit(self):
329+
self.assertListEqual(limit([1, 2, 3, 4, 5], 2, 4), [2, 3, 4])
330+
self.assertListEqual(limit([1, 2, 3, 4, 5], 2), [2, 3, 4, 5])
331+
self.assertListEqual(limit([1, 2, 3, 4, 5], None, 4), [1, 2, 3, 4])
332+
333+
325334

326335

327336
if __name__ == '__main__':

0 commit comments

Comments
 (0)