Repository: keon/algorithms
Pull request #353 (2018-06-12 05:31:32)

created limit.py in /algorithms/arrays
(Put an `X` inside the `[ ]` to denote check mark `[X]`.)


- [X] **If creating a new file :**
  - [X] added links to it in the README files ?
  - [X] included tests with it ?
  - [X] added description (overview of algorithm, time and space compleixty, and possible edge case) in docstrings ?

- [ ] **if done some changes :**
  - [ ] wrote short description in the PR explaining what the changes do ?
  - [ ] Fixes #[issue number] if related to any issue

- [ ] **other**


coveralls: ## Pull Request Test Coverage Report for [Build 626](https://coveralls.io/builds/17439957)

* **19** of **19**   **(100.0%)**  changed or added relevant lines in **2** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage increased (+**0.1%**) to **70.713%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/17439957/badge)](https://coveralls.io/builds/17439957) |
| :-- | --: |
| Change from base [Build 621](https://coveralls.io/builds/17415977): |  0.1% |
| Covered Lines: | 3571 |
| Relevant Lines: | 5050 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/MANIFEST.in b/MANIFEST.in
index daf8a52..50af9b9 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,4 +1,3 @@
 include README.md
 include LICENSE
 include algorithms/*
-include tests/*
diff --git a/README.md b/README.md
index d85ec44..c03bece 100644
--- a/README.md
+++ b/README.md
@@ -60,6 +60,7 @@ If you want to uninstall algorithms, it is as simple as:
     - [flatten](algorithms/arrays/flatten.py)
     - [garage](algorithms/arrays/garage.py)
     - [josephus_problem](algorithms/arrays/josephus.py)
+    - [limit](algorithms/arrays/limit.py)
     - [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
     - [max_ones_index](algorithms/arrays/max_ones_index.py)
     - [merge_intervals](algorithms/arrays/merge_intervals.py)
diff --git a/README_CN.md b/README_CN.md
index b675f6f..42f7b36 100644
--- a/README_CN.md
+++ b/README_CN.md
@@ -78,6 +78,7 @@ pip3 uninstall -y algorithms
     - [garage：停车场](algorithms/arrays/garage.py)
     - [josephus_problem: 约瑟夫问题](algorithms/arrays/josephus.py)
     - [max_ones_index](algorithms/arrays/max_ones_index.py)
+    - [limit](algorithms/arrays/limit.py)
     - [longest_non_repeat：最长不重复子串](algorithms/arrays/longest_non_repeat.py/) 
     - [merge_intervals：合并重叠间隔](algorithms/arrays/merge_intervals.py)
     - [missing_ranges：遗失的范围](algorithms/arrays/missing_ranges.py)
diff --git a/README_GE.md b/README_GE.md
index e21dca3..f9f4757 100644
--- a/README_GE.md
+++ b/README_GE.md
@@ -67,6 +67,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
     - [flatten](algorithms/arrays/flatten.py)
     - [garage](algorithms/arrays/garage.py)
     - [josephus_problem](algorithms/arrays/josephus.py)
+    - [limit](algorithms/arrays/limit.py)
     - [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
     - [max_ones_index](algorithms/arrays/max_ones_index.py)
     - [merge_intervals](algorithms/arrays/merge_intervals.py)
diff --git a/README_JP.md b/README_JP.md
index 636d79f..571c70f 100644
--- a/README_JP.md
+++ b/README_JP.md
@@ -61,6 +61,7 @@ if __name__ == "__main__":
     - [flatten](algorithms/arrays/flatten.py)
     - [garage](algorithms/arrays/garage.py)
     - [josephus_problem](algorithms/arrays/josephus.py)
+    - [limit](algorithms/arrays/limit.py)
     - [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
     - [max_ones_index](algorithms/arrays/max_ones_index.py)
     - [merge_intervals](algorithms/arrays/merge_intervals.py)
diff --git a/README_KR.md b/README_KR.md
index b38cdd4..773babd 100644
--- a/README_KR.md
+++ b/README_KR.md
@@ -60,6 +60,7 @@ if __name__ == "__main__":
     - [flatten](algorithms/arrays/flatten.py)
     - [garage](algorithms/arrays/garage.py)
     - [josephus_problem](algorithms/arrays/josephus.py)
+    - [limit](algorithms/arrays/limit.py)
     - [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
     - [max_ones_index](algorithms/arrays/max_ones_index.py)
     - [merge_intervals](algorithms/arrays/merge_intervals.py)
diff --git a/algorithms/arrays/__init__.py b/algorithms/arrays/__init__.py
index 7e57f63..c0d8b8f 100644
--- a/algorithms/arrays/__init__.py
+++ b/algorithms/arrays/__init__.py
@@ -14,3 +14,4 @@ from .three_sum import *
 from .trimmean import *
 from .top_1 import *
 from .two_sum import *
+from .limit import *
diff --git a/algorithms/arrays/limit.py b/algorithms/arrays/limit.py
new file mode 100644
index 0000000..adcc577
--- /dev/null
+++ b/algorithms/arrays/limit.py
@@ -0,0 +1,30 @@
+"""
+Sometimes you need to limit array result to use. Such as you only need the value over
+10 or, you need value under than 100. By use this algorithms, you can limit your array
+to specific value
+
+If array, Min, Max value was given, it returns array that contains values of given array
+which was larger than Min, and lower than Max. You need to give 'unlimit' to use only Min
+or Max.
+
+ex) limit([1,2,3,4,5], None, 3) = [1,2,3]
+
+Complexity = O(n)
+"""
+
+def limit(arr, min_lim = None, max_lim = None):
+    result = []
+    if min_lim == None:
+        for i in arr:
+            if i<= max_lim:
+                result.append(i)
+    elif max_lim == None:
+        for i in arr:
+            if i >= min_lim:
+                result.append(i)
+    else:
+        for i in arr:
+            if i >= min_lim and i <= max_lim:
+                result.append(i)
+
+    return result


Test patch:
diff --git a/tests/test_array.py b/tests/test_array.py
index e3f6f6d..6002adb 100644
--- a/tests/test_array.py
+++ b/tests/test_array.py
@@ -14,7 +14,8 @@ from algorithms.arrays import (
     two_sum,
     max_ones_index,
     trimmean,
-    top_1
+    top_1,
+    limit
 )
 
 import unittest
@@ -322,6 +323,14 @@ class TestTop1(unittest.TestCase):
         self.assertListEqual(top_1([1 , 1, 2, 2, 3]), [1, 2])
         self.assertListEqual(top_1([1, 2, 3, 324, 234, 23, 23, 1, 23, 23]), [23])
 
+class TestLimit(unittest.TestCase):
+    
+    def test_limit(self):
+        self.assertListEqual(limit([1, 2, 3, 4, 5], 2, 4), [2, 3, 4])
+        self.assertListEqual(limit([1, 2, 3, 4, 5], 2), [2, 3, 4, 5])
+        self.assertListEqual(limit([1, 2, 3, 4, 5], None, 4), [1, 2, 3, 4])
+
+        
 
 
 if __name__ == '__main__':