Repository: keon/algorithms
Pull request #211 (2018-04-10 18:16:24)

separate tests in array/ module
- refer #162 


Patch:
diff --git a/.cache/v/cache/lastfailed b/.cache/v/cache/lastfailed
new file mode 100644
index 0000000..52d1b51
--- /dev/null
+++ b/.cache/v/cache/lastfailed
@@ -0,0 +1,15 @@
+{
+  "array/test_array.py::TestDeleteNth": true,
+  "array/test_array.py::TestFlatten": true,
+  "array/test_array.py::TestGarage": true,
+  "array/test_array.py::TestJosephus": true,
+  "array/test_array.py::TestLongestNonRepeat": true,
+  "array/test_array.py::TestMergeInterval": true,
+  "array/test_array.py::TestMissingRanges": true,
+  "array/test_array.py::TestMoveZeros": true,
+  "array/test_array.py::TestPlusOne": true,
+  "array/test_array.py::TestRotateArray": true,
+  "array/test_array.py::TestSuite": true,
+  "array/test_array.py::TestSummaryRanges": true,
+  "array/test_array.py::TestThreeSum": true
+}
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index 060f62c..eb2aa3a 100755
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 __pycache__/
 *.py[cod]
 .idea/
+.cache/
diff --git a/.travis.yml b/.travis.yml
index 94bc744..0729d7e 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,7 +10,7 @@ matrix:
         - python: nightly
         - python: pypy3
 install:
-    #- pip install -r requirements.txt
+    - pip install -r requirements.txt
     - pip install flake8  # pytest  # add another testing frameworks later
 before_script:
     # stop the build if there are Python syntax errors or undefined names
@@ -18,7 +18,7 @@ before_script:
     # exit-zero treats all errors as warnings.  The GitHub editor is 127 chars wide
     - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
 script:
-    - true  # pytest --capture=sys  # add other tests here
+    - pytest
 notifications:
     on_success: change
-    on_failure: change  # `always` will be the setting once code changes slow down
+    on_failure: change  # `always` will be the setting once code changes slow down
\ No newline at end of file
diff --git a/README.md b/README.md
index 5ad04cd..538d13d 100644
--- a/README.md
+++ b/README.md
@@ -11,12 +11,17 @@ Minimal and clean example implementations of data structures and algorithms in P
 Thanks for your interest in contributing! There are many ways to contribute to this project. [Get started here](CONTRIBUTING.md)
 
 
+## Tests
+To run the tests, clone the repo and run :  
+ ```pytest```
+
 ## List of Implementations
 
 - [array](array)
-    - [circular_counter](array/circular_counter.py)
+    - [delete_nth](array/delete_nth.py)
     - [flatten](array/flatten.py)
     - [garage](array/garage.py)
+    - [josephus_problem](array/josephus_problem.py)
     - [longest_non_repeat](array/longest_non_repeat.py/)
     - [merge_intervals](array/merge_intervals.py)
     - [missing_ranges](array/missing_ranges.py)
@@ -26,7 +31,6 @@ Thanks for your interest in contributing! There are many ways to contribute to t
     - [three_sum](array/three_sum.py)
     - [two_sum](array/two_sum.py)
     - [move_zeros_to_end](array/move_zeros_to_end.py)
-    - [delete_nth](array/delete_nth.py)
 - [backtrack](backtrack)
     - [general_solution.md](backtrack/)
     - [anagram](backtrack/anagram.py)
diff --git a/README_CN.md b/README_CN.md
index 1c832ee..d1fd193 100644
--- a/README_CN.md
+++ b/README_CN.md
@@ -9,7 +9,7 @@ python版数据结构和算法实现的简约版小示例
 ## 实现列表
 
 - [array:数组](array)
-    - [circular_counter：约瑟夫环](array/circular_counter.py)
+    - [circular_counter：约瑟夫环](array/josephus_problem.py)
     - [flatten：数组降维](array/flatten.py)
     - [garage：停车场](array/garage.py)
     - [longest_non_repeat：最长不重复子串](array/longest_non_repeat.py/)
diff --git a/array/__init__.py b/array/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/array/circular_counter.py b/array/circular_counter.py
deleted file mode 100644
index 9d942dc..0000000
--- a/array/circular_counter.py
+++ /dev/null
@@ -1,41 +0,0 @@
-"""
-There are people sitting in a circular fashion,
-print every third member while removing them,
-the next counter starts immediately after the member is removed.
-Print till all the members are exhausted.
-
-For example:
-Input: consider 123456789 members sitting in a circular fashion,
-Output: 369485271
-"""
-
-
-def josepheus(int_list, skip):
-    skip = skip - 1  # list starts with 0 index
-    idx = 0
-    len_list = (len(int_list))
-    while len_list > 0:
-        idx = (skip+idx) % len_list  # hash index to every 3rd
-        yield int_list.pop(idx)
-        len_list -= 1
-
-import unittest
-
-class TestJosepheus(unittest.TestCase):
-    def test_josepheues(self):
-        a = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
-        josepheus_generator = josepheus(a, 3)
-        self.assertEqual(next(josepheus_generator), '3')
-        self.assertEqual(next(josepheus_generator), '6')
-        self.assertEqual(next(josepheus_generator), '9')
-        self.assertEqual(next(josepheus_generator), '4')
-        self.assertEqual(next(josepheus_generator), '8')
-        self.assertEqual(next(josepheus_generator), '5')
-        self.assertEqual(next(josepheus_generator), '2')
-        self.assertEqual(next(josepheus_generator), '7')
-        self.assertEqual(next(josepheus_generator), '1')
-        self.assertRaises(StopIteration, next, josepheus_generator)
-
-if __name__ == "__main__":
-    
-    unittest.main()
diff --git a/array/delete_nth.py b/array/delete_nth.py
index b44cdd3..8d768a4 100644
--- a/array/delete_nth.py
+++ b/array/delete_nth.py
@@ -7,7 +7,6 @@ drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times
 which leads to [1,2,3,1,2,3]
 """
 import collections
-import unittest
 
 
 # Time complexity O(n^2)
@@ -31,38 +30,3 @@ def delete_nth(array, n):
             counts[i] += 1
 
     return result
-
-
-class TestSuite(unittest.TestCase):
-
-    def test_delete_nth_naive(self):
-
-        self.assertListEqual(delete_nth_naive([20, 37, 20, 21, 37, 21, 21], n=1),
-                             [20, 37, 21])
-        self.assertListEqual(delete_nth_naive([1, 1, 3, 3, 7, 2, 2, 2, 2], n=3),
-                             [1, 1, 3, 3, 7, 2, 2, 2])
-        self.assertListEqual(delete_nth_naive([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=3),
-                             [1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5])
-        self.assertListEqual(delete_nth_naive([], n=5),
-                             [])
-        self.assertListEqual(delete_nth_naive([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=0),
-                             [])
-
-    def test_delete_nth(self):
-
-        self.assertListEqual(delete_nth([20, 37, 20, 21, 37, 21, 21], n=1),
-                             [20, 37, 21])
-        self.assertListEqual(delete_nth([1, 1, 3, 3, 7, 2, 2, 2, 2], n=3),
-                             [1, 1, 3, 3, 7, 2, 2, 2])
-        self.assertListEqual(delete_nth([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=3),
-                             [1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5])
-        self.assertListEqual(delete_nth([], n=5),
-                             [])
-        self.assertListEqual(delete_nth([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=0),
-                             [])
-
-
-if __name__ == '__main__':
-
-    unittest.main()
-
diff --git a/array/flatten.py b/array/flatten.py
index 56c3bb6..2b04746 100644
--- a/array/flatten.py
+++ b/array/flatten.py
@@ -5,16 +5,18 @@ produce a single resultant array.
 """
 from collections import Iterable
 
+
 # return list
-def flatten(inputArr, outputArr=None):
-    if outputArr is None:
-        outputArr = []
-    for ele in inputArr:
+def flatten(input_arr, output_arr=None):
+    if output_arr is None:
+        output_arr = []
+    for ele in input_arr:
         if isinstance(ele, Iterable):
-            flatten(ele, outputArr)
+            flatten(ele, output_arr)
         else:
-            outputArr.append(ele)
-    return outputArr
+            output_arr.append(ele)
+    return output_arr
+
 
 # returns iterator
 def flatten_iter(iterable):
@@ -27,52 +29,3 @@ def flatten_iter(iterable):
             yield from flatten(element)
         else:
             yield element
-
-import unittest
-
-class TestFlatten(unittest.TestCase):
-    def test_flatten(self):
-        nested_list = [2, 1, [3, [4, 5], 6], 7, [8]]
-        flattened = flatten(nested_list)
-        self.assertEqual(flattened, [2, 1, 3, 4, 5, 6, 7, 8])
-        
-        nested_list = [[3, [4, 5], 6], 7, [8]]
-        flattened = flatten(nested_list)
-        self.assertEqual(flattened, [3, 4, 5, 6, 7, 8])
-        
-        nested_list = [[], [8]]
-        flattened = flatten(nested_list)
-        self.assertEqual(flattened, [8])
-        
-    def test_flatten_iter(self):
-        nested_list = [2, 1, [3, [4, 5], 6], 7, [8]]
-        flattened = flatten_iter(nested_list)
-        self.assertEqual(next(flattened), 2)
-        self.assertEqual(next(flattened), 1)
-        self.assertEqual(next(flattened), 3)
-        self.assertEqual(next(flattened), 4)
-        self.assertEqual(next(flattened), 5)
-        self.assertEqual(next(flattened), 6)
-        self.assertEqual(next(flattened), 7)
-        self.assertEqual(next(flattened), 8)
-        self.assertRaises(StopIteration, next, flattened)
-        
-        nested_list = [[3, [4, 5], 6], 7, [8]]
-        flattened = flatten_iter(nested_list)
-        self.assertEqual(next(flattened), 3)
-        self.assertEqual(next(flattened), 4)
-        self.assertEqual(next(flattened), 5)
-        self.assertEqual(next(flattened), 6)
-        self.assertEqual(next(flattened), 7)
-        self.assertEqual(next(flattened), 8)
-        self.assertRaises(StopIteration, next, flattened)
-        
-        nested_list = [[], [8]]
-        flattened = flatten_iter(nested_list)
-        self.assertEqual(next(flattened), 8) 
-        self.assertRaises(StopIteration, next, flattened)
-
-
-if __name__ == "__main__":
-    
-    unittest.main()
diff --git a/array/garage.py b/array/garage.py
index 19b17ea..d590343 100644
--- a/array/garage.py
+++ b/array/garage.py
@@ -30,7 +30,6 @@ Sequence :
 2 3 0 1 4
 0 3 2 1 4
 """
-import unittest
 
 
 def garage(initial, final):
@@ -53,20 +52,3 @@ def garage(initial, final):
         steps += 1
 
     return steps, seq
-
-
-class TestSuite(unittest.TestCase):
-
-    def test_garage(self):
-
-        initial = [1, 2, 3, 0, 4]
-        final = [0, 3, 2, 1, 4]
-        steps, seq = garage(initial, final)
-
-        self.assertEqual(steps, 4)
-        self.assertListEqual(seq, [[0, 2, 3, 1, 4], [2, 0, 3, 1, 4], [2, 3, 0, 1, 4], [0, 3, 2, 1, 4]])
-
-
-if __name__ == "__main__":
-
-    unittest.main()
diff --git a/array/josephus_problem.py b/array/josephus_problem.py
new file mode 100644
index 0000000..f77c48a
--- /dev/null
+++ b/array/josephus_problem.py
@@ -0,0 +1,20 @@
+"""
+There are people sitting in a circular fashion,
+print every third member while removing them,
+the next counter starts immediately after the member is removed.
+Print till all the members are exhausted.
+
+For example:
+Input: consider 123456789 members sitting in a circular fashion,
+Output: 369485271
+"""
+
+
+def josephus(int_list, skip):
+    skip = skip - 1  # list starts with 0 index
+    idx = 0
+    len_list = (len(int_list))
+    while len_list > 0:
+        idx = (skip+idx) % len_list  # hash index to every 3rd
+        yield int_list.pop(idx)
+        len_list -= 1
diff --git a/array/longest_non_repeat.py b/array/longest_non_repeat.py
index 055df0f..43f2055 100644
--- a/array/longest_non_repeat.py
+++ b/array/longest_non_repeat.py
@@ -8,7 +8,6 @@ Given "pwwkew", the answer is "wke", with the length of 3.
 Note that the answer must be a substring,
 "pwke" is a subsequence and not a substring.
 """
-import unittest
 
 
 def longest_non_repeat(string):
@@ -45,33 +44,3 @@ def longest_non_repeat_two(string):
             max_len = max(max_len, index - start + 1)
         used_char[char] = index
     return max_len
-
-
-class TestLongestNonRepeat(unittest.TestCase):
-
-    def test_longest_non_repeat(self):
-
-        string = "abcabcbb"
-        self.assertEqual(longest_non_repeat(string), 3)
-
-        string = "bbbbb"
-        self.assertEqual(longest_non_repeat(string), 1)
-        
-        string = "pwwkew"
-        self.assertEqual(longest_non_repeat(string), 3)
-
-    def test_longest_non_repeat_two(self):
-        
-        string = "abcabcbb"
-        self.assertEqual(longest_non_repeat_two(string), 3)
-
-        string = "bbbbb"
-        self.assertEqual(longest_non_repeat_two(string), 1)
-        
-        string = "pwwkew"
-        self.assertEqual(longest_non_repeat_two(string), 3)
-        
-
-if __name__ == "__main__":
-    
-    unittest.main()
diff --git a/array/merge_intervals.py b/array/merge_intervals.py
index faa3ea8..c657bb8 100644
--- a/array/merge_intervals.py
+++ b/array/merge_intervals.py
@@ -1,7 +1,6 @@
 """
 Given a collection of intervals, merge all overlapping intervals.
 """
-import unittest
 
 
 class Interval:
@@ -77,27 +76,3 @@ def merge_v2(intervals):
         else:
             out.append(i)
     return out
-
-
-class TestMergeInterval(unittest.TestCase):
-
-    def test_merge(self):
-        interval_list = [[1, 3], [2, 6], [8, 10], [15, 18]]
-        intervals = [Interval(i[0], i[1]) for i in interval_list]
-        merged_intervals = Interval.merge(intervals)
-        self.assertEqual(
-            merged_intervals,
-            [Interval(1, 6), Interval(8, 10), Interval(15, 18)]
-        )
-
-    def test_merge_v2(self):
-        interval_list = [[1, 3], [2, 6], [8, 10], [15, 18]]
-        merged_intervals = merge_v2(interval_list)
-        self.assertEqual(
-            merged_intervals,
-            [[1, 6], [8, 10], [15, 18]]
-        )
-
-
-if __name__ == "__main__":
-    unittest.main()
diff --git a/array/missing_ranges.py b/array/missing_ranges.py
index 10f14dc..68fcc43 100644
--- a/array/missing_ranges.py
+++ b/array/missing_ranges.py
@@ -2,7 +2,6 @@
 Find missing ranges between low and high in the given array.
 Ex) [3, 5] lo=1 hi=10 => answer: [(1, 2), (4, 4), (6, 10)]
 """
-import unittest
 
 
 def missing_ranges(arr, lo, hi):
@@ -22,21 +21,3 @@ def missing_ranges(arr, lo, hi):
         res.append((start, hi))
 
     return res
-
-
-class TestSuite(unittest.TestCase):
-
-    def test_missing_ranges(self):
-
-        arr = [3, 5, 10, 11, 12, 15, 19]
-
-        self.assertListEqual(missing_ranges(arr, 0, 20),
-                             [(0, 2), (4, 4), (6, 9), (13, 14), (16, 18), (20, 20)])
-
-        self.assertListEqual(missing_ranges(arr, 6, 100),
-                             [(6, 9), (13, 14), (16, 18), (20, 100)])
-
-
-if __name__ == '__main__':
-
-    unittest.main()
diff --git a/array/move_zeros_to_end.py b/array/move_zeros_to_end.py
index 54ffce4..b2acd75 100644
--- a/array/move_zeros_to_end.py
+++ b/array/move_zeros_to_end.py
@@ -6,7 +6,6 @@ preserving the order of the other elements.
 
 The time complexity of the below algorithm is O(n).
 """
-import unittest
 
 
 def move_zeros(array):
@@ -21,19 +20,3 @@ def move_zeros(array):
     
     result.extend([0] * zeros)
     return result
-
-
-class TestSuite(unittest.TestCase):
-
-    def test_move_zeros(self):
-
-        self.assertListEqual(move_zeros([False, 1, 0, 1, 2, 0, 1, 3, "a"]),
-                             [False, 1, 1, 2, 1, 3, "a", 0, 0])
-
-        self.assertListEqual(move_zeros([0, 34, 'rahul', [], None, 0, True, 0]),
-                             [34, 'rahul', [], None, True, 0, 0, 0])
-
-
-if __name__ == '__main__':
-
-    unittest.main()
diff --git a/array/plus_one.py b/array/plus_one.py
index 96fa206..9edb02f 100644
--- a/array/plus_one.py
+++ b/array/plus_one.py
@@ -5,7 +5,6 @@ plus one to the number.
 The digits are stored such that the most significant
 digit is at the head of the list.
 """
-import unittest
 
 
 def plus_one(digits):
@@ -47,35 +46,3 @@ def plus_one_v3(num_arr):
         if num_arr[idx]:
             return num_arr
     return [1] + num_arr
-
-
-class TestSuite(unittest.TestCase):
-
-    def test_plus_one(self):
-
-        self.assertListEqual(plus_one([0]), [1])
-        self.assertListEqual(plus_one([9]), [1, 0])
-        self.assertListEqual(plus_one([1, 0, 9]), [1, 1, 0])
-        self.assertListEqual(plus_one([9, 9, 8, 0, 0, 9]), [9, 9, 8, 0, 1, 0])
-        self.assertListEqual(plus_one([9, 9, 9, 9]), [1, 0, 0, 0, 0])
-
-    def test_plus_one_v2(self):
-
-        self.assertListEqual(plus_one_v2([0]), [1])
-        self.assertListEqual(plus_one_v2([9]), [1, 0])
-        self.assertListEqual(plus_one_v2([1, 0, 9]), [1, 1, 0])
-        self.assertListEqual(plus_one_v2([9, 9, 8, 0, 0, 9]), [9, 9, 8, 0, 1, 0])
-        self.assertListEqual(plus_one_v2([9, 9, 9, 9]), [1, 0, 0, 0, 0])
-
-    def test_plus_one_v3(self):
-
-        self.assertListEqual(plus_one_v3([0]), [1])
-        self.assertListEqual(plus_one_v3([9]), [1, 0])
-        self.assertListEqual(plus_one_v3([1, 0, 9]), [1, 1, 0])
-        self.assertListEqual(plus_one_v3([9, 9, 8, 0, 0, 9]), [9, 9, 8, 0, 1, 0])
-        self.assertListEqual(plus_one_v3([9, 9, 9, 9]), [1, 0, 0, 0, 0])
-
-
-if __name__ == '__main__':
-
-    unittest.main()
\ No newline at end of file
diff --git a/array/rotate_array.py b/array/rotate_array.py
index b5f9318..d90035b 100644
--- a/array/rotate_array.py
+++ b/array/rotate_array.py
@@ -8,7 +8,6 @@ Note:
 Try to come up as many solutions as you can,
 there are at least 3 different ways to solve this problem.
 """
-import unittest
 
 
 def rotate_v1(array, k):
@@ -60,32 +59,3 @@ def rotate_v3(array, k):
     length = len(array)
     k = k % length
     return array[length - k:] + array[:length - k]
-
-
-class TestSuite(unittest.TestCase):
-
-    def test_rotate_v1(self):
-
-        self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=3), [5, 6, 7, 1, 2, 3, 4])
-        self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=1), [7, 1, 2, 3, 4, 5, 6])
-        self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=7), [1, 2, 3, 4, 5, 6, 7])
-        self.assertListEqual(rotate_v1([1, 2], k=111), [2, 1])
-
-    def test_rotate_v2(self):
-
-        self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=3), [5, 6, 7, 1, 2, 3, 4])
-        self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=1), [7, 1, 2, 3, 4, 5, 6])
-        self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=7), [1, 2, 3, 4, 5, 6, 7])
-        self.assertListEqual(rotate_v2([1, 2], k=111), [2, 1])
-
-    def test_rotate_v3(self):
-
-        self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=3), [5, 6, 7, 1, 2, 3, 4])
-        self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=1), [7, 1, 2, 3, 4, 5, 6])
-        self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=7), [1, 2, 3, 4, 5, 6, 7])
-        self.assertListEqual(rotate_v3([1, 2], k=111), [2, 1])
-
-
-if __name__ == '__main__':
-
-    unittest.main()
diff --git a/array/summary_ranges.py b/array/summary_ranges.py
index 4f3e970..3ee3417 100644
--- a/array/summary_ranges.py
+++ b/array/summary_ranges.py
@@ -4,7 +4,6 @@ return the summary of its ranges.
 
 For example, given [0, 1, 2, 4, 5, 7], return [(0, 2), (4, 5), (7, 7)].
 """
-import unittest
 
 
 def summary_ranges(array):
@@ -26,20 +25,3 @@ def summary_ranges(array):
             res.append((num, num))
         i += 1
     return res
-
-
-class TestSuite(unittest.TestCase):
-
-    def test_summary_ranges(self):
-
-        self.assertListEqual(summary_ranges([0, 1, 2, 4, 5, 7]),
-                             [(0, 2), (4, 5), (7, 7)])
-        self.assertListEqual(summary_ranges([-5, -4, -3, 1, 2, 4, 5, 6]),
-                             [(-5, -3), (1, 2), (4, 6)])
-        self.assertListEqual(summary_ranges([-2, -1, 0, 1, 2]),
-                             [(-2, 2)])
-
-
-if __name__ == '__main__':
-
-    unittest.main()
\ No newline at end of file
diff --git a/array/three_sum.py b/array/three_sum.py
index 986534f..524b83e 100644
--- a/array/three_sum.py
+++ b/array/three_sum.py
@@ -13,7 +13,6 @@ A solution set is:
   (-1, -1, 2)
 }
 """
-import unittest
 
 
 def three_sum(array):
@@ -47,19 +46,3 @@ def three_sum(array):
                 l += 1
                 r -= 1
     return res
-
-
-class TestSuite(unittest.TestCase):
-
-    def test_three_sum(self):
-
-        self.assertSetEqual(three_sum([-1, 0, 1, 2, -1, -4]),
-                            {(-1, 0, 1), (-1, -1, 2)})
-
-        self.assertSetEqual(three_sum([-1, 3, 1, 2, -1, -4, -2]),
-                            {(-4, 1, 3), (-2, -1, 3), (-1, -1, 2)})
-
-
-if __name__ == "__main__":
-
-    unittest.main()
diff --git a/array/two_sum.py b/array/two_sum.py
index 720eead..2b6f1ed 100644
--- a/array/two_sum.py
+++ b/array/two_sum.py
@@ -11,7 +11,6 @@ Example:
     Because nums[0] + nums[1] = 2 + 7 = 9,
     return (0, 1)
 """
-import unittest
 
 
 def two_sum(array, target):
@@ -22,18 +21,3 @@ def two_sum(array, target):
         else:
             dic[target - num] = i
     return None
-
-
-class TestSuite(unittest.TestCase):
-
-    def test_two_sum(self):
-
-        self.assertTupleEqual((0, 2), two_sum([2, 11, 7, 9], target=9))
-        self.assertTupleEqual((0, 3), two_sum([-3, 5, 2, 3, 8, -9], target=0))
-
-        self.assertIsNone(two_sum([-3, 5, 2, 3, 8, -9], target=6))
-
-
-if __name__ == "__main__":
-
-    unittest.main()
diff --git a/backtrack/anagram.py b/backtrack/anagram.py
index 84cdb7a..862140f 100644
--- a/backtrack/anagram.py
+++ b/backtrack/anagram.py
@@ -11,6 +11,7 @@ def all_perms_iter(elements):
             for i in range(len(elements)):
                 yield perm[:i] + elements[0:1] + perm[i:]
 
+
 def all_perms(elements):
     """
         returns a list with the permuations.
@@ -24,26 +25,34 @@ def all_perms(elements):
                 tmp.append(perm[:i] + elements[0:1] + perm[i:])
         return tmp
 
-def anagram(s1,s2):
-    c1 = [0]*26
-    c2 = [0]*26
-    for i in range(len(s1)):
-        pos = ord(s1[i])-ord('a')
+
+def anagram(s1, s2):
+    c1 = [0] * 26
+    c2 = [0] * 26
+
+    for c in s1:
+        pos = ord(c)-ord('a')
         c1[pos] = c1[pos] + 1
-    for i in range(len(s2)):
-        pos = ord(s2[i])-ord('a')
+
+    for c in s2:
+        pos = ord(c)-ord('a')
         c2[pos] = c2[pos] + 1
+
     return c1 == c2
 
+
 class TestSuite (unittest.TestCase):
+
     def test_all_perms(self):
         perms = ['abc', 'bac', 'bca', 'acb', 'cab', 'cba']
         self.assertEqual(perms, all_perms("abc"))
+
     def test_all_perms_iter(self):
         it = all_perms_iter("abc")
         perms = ['abc', 'bac', 'bca', 'acb', 'cab', 'cba']
         for i in range(len(perms)):
             self.assertEqual(perms[i], next(it))
+
     def test_angram(self):
         self.assertTrue(anagram('apple', 'pleap'))
         self.assertFalse(anagram("apple", "cherry"))
diff --git a/backtrack/combination_sum.py b/backtrack/combination_sum.py
index 3ac87b6..8c67476 100644
--- a/backtrack/combination_sum.py
+++ b/backtrack/combination_sum.py
@@ -15,12 +15,14 @@ A solution set is:
 ]
 """
 
-def combinationSum(self, candidates, target):
+
+def combination_sum(self, candidates, target):
     res = []
     candidates.sort()
     self.dfs(candidates, target, 0, [], res)
     return res
 
+
 def dfs(self, nums, target, index, path, res):
     if target < 0:
         return  # backtracking
diff --git a/backtrack/expression_add_operators.py b/backtrack/expression_add_operators.py
index 2e1f450..f8f520f 100644
--- a/backtrack/expression_add_operators.py
+++ b/backtrack/expression_add_operators.py
@@ -22,13 +22,14 @@ def add_operator(num, target):
     helper(res, "", num, target, 0, 0, 0)
     return res
 
+
 def helper(res, path, num, target, pos, prev, multed):
     if pos == len(num):
-        if (target == prev):
+        if target == prev:
             res.append(path)
         return
     for i in range(pos, len(num)):
-        if i != pos and num[pos] == '0': # all digits have to be used
+        if i != pos and num[pos] == '0':  # all digits have to be used
             break
         cur = int(num[pos:i+1])
         if pos == 0:
diff --git a/backtrack/factor_combinations.py b/backtrack/factor_combinations.py
index 8bfbdce..d4de728 100644
--- a/backtrack/factor_combinations.py
+++ b/backtrack/factor_combinations.py
@@ -34,22 +34,23 @@ output:
 ]
 """
 
-# Iterative:
 
-def getFactors(self, n):
+# Iterative:
+def get_factors(n):
     todo, combis = [(n, 2, [])], []
     while todo:
         n, i, combi = todo.pop()
         while i * i <= n:
             if n % i == 0:
-                combis.append(combi + [i, n//i]),
-                todo.append( [n // i, i, combi+[i] ] )  # python3: n // i
+                combis.append(combi + [i, n//i])
+                todo.append((n//i, i, combi+[i]))
             i += 1
     return combis
 
+
 # Recursive:
+def get_factors_recur(n):
 
-def getFactors(self, n):
     def factor(n, i, combi, combis):
         while i * i <= n:
             if n % i == 0:
@@ -57,4 +58,5 @@ def getFactors(self, n):
                 factor(n//i, i, combi+[i], combis)
             i += 1
         return combis
+
     return factor(n, 2, [], [])
diff --git a/map/longest_common_subsequence.py b/map/longest_common_subsequence.py
index 247b238..631b19d 100644
--- a/map/longest_common_subsequence.py
+++ b/map/longest_common_subsequence.py
@@ -1,14 +1,14 @@
 """
 Given string a and b, with b containing all distinct characters,
-find the longest common subsequence's
+find the longest common sub sequence's length.
 
-length. Expected complexity O(nlogn).
+Expected complexity O(n logn).
 """
 
 
 def max_common_sub_string(s1, s2):
     # Assuming s2 has all unique chars
-    s2dic = {s2[i]: i for i in xrange(len(s2))}
+    s2dic = {s2[i]: i for i in range(len(s2))}
     maxr = 0
     subs = ''
     i = 0


Test patch:
diff --git a/array/test_array.py b/array/test_array.py
new file mode 100644
index 0000000..c02a41b
--- /dev/null
+++ b/array/test_array.py
@@ -0,0 +1,278 @@
+from array.delete_nth import delete_nth, delete_nth_naive
+from array.flatten import flatten, flatten_iter
+from array.garage import garage
+from array.josephus_problem import josephus
+from array.longest_non_repeat import longest_non_repeat, longest_non_repeat_two
+from array.merge_intervals import Interval, merge_v2
+from array.missing_ranges import missing_ranges
+from array.move_zeros_to_end import move_zeros
+from array.plus_one import plus_one, plus_one_v2, plus_one_v3
+from array.rotate_array import rotate_v1, rotate_v2, rotate_v3
+from array.summary_ranges import summary_ranges
+from array.three_sum import three_sum
+from array.two_sum import two_sum
+
+import unittest
+
+
+class TestJosephus(unittest.TestCase):
+
+    def test_josephus(self):
+
+        a = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
+        josephus_generator = josephus(a, 3)
+        self.assertEqual(next(josephus_generator), '3')
+        self.assertEqual(next(josephus_generator), '6')
+        self.assertEqual(next(josephus_generator), '9')
+        self.assertEqual(next(josephus_generator), '4')
+        self.assertEqual(next(josephus_generator), '8')
+        self.assertEqual(next(josephus_generator), '5')
+        self.assertEqual(next(josephus_generator), '2')
+        self.assertEqual(next(josephus_generator), '7')
+        self.assertEqual(next(josephus_generator), '1')
+        self.assertRaises(StopIteration, next, josephus_generator)
+
+
+class TestDeleteNth(unittest.TestCase):
+
+    def test_delete_nth_naive(self):
+
+        self.assertListEqual(delete_nth_naive([20, 37, 20, 21, 37, 21, 21], n=1),
+                             [20, 37, 21])
+        self.assertListEqual(delete_nth_naive([1, 1, 3, 3, 7, 2, 2, 2, 2], n=3),
+                             [1, 1, 3, 3, 7, 2, 2, 2])
+        self.assertListEqual(delete_nth_naive([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=3),
+                             [1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5])
+        self.assertListEqual(delete_nth_naive([], n=5),
+                             [])
+        self.assertListEqual(delete_nth_naive([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=0),
+                             [])
+
+    def test_delete_nth(self):
+
+        self.assertListEqual(delete_nth([20, 37, 20, 21, 37, 21, 21], n=1),
+                             [20, 37, 21])
+        self.assertListEqual(delete_nth([1, 1, 3, 3, 7, 2, 2, 2, 2], n=3),
+                             [1, 1, 3, 3, 7, 2, 2, 2])
+        self.assertListEqual(delete_nth([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=3),
+                             [1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5])
+        self.assertListEqual(delete_nth([], n=5),
+                             [])
+        self.assertListEqual(delete_nth([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=0),
+                             [])
+
+
+class TestFlatten(unittest.TestCase):
+
+    def test_flatten(self):
+
+        nested_list = [2, 1, [3, [4, 5], 6], 7, [8]]
+        flattened = flatten(nested_list)
+        self.assertEqual(flattened, [2, 1, 3, 4, 5, 6, 7, 8])
+
+        nested_list = [[3, [4, 5], 6], 7, [8]]
+        flattened = flatten(nested_list)
+        self.assertEqual(flattened, [3, 4, 5, 6, 7, 8])
+
+        nested_list = [[], [8]]
+        flattened = flatten(nested_list)
+        self.assertEqual(flattened, [8])
+
+    def test_flatten_iter(self):
+
+        nested_list = [2, 1, [3, [4, 5], 6], 7, [8]]
+        flattened = flatten_iter(nested_list)
+        self.assertEqual(next(flattened), 2)
+        self.assertEqual(next(flattened), 1)
+        self.assertEqual(next(flattened), 3)
+        self.assertEqual(next(flattened), 4)
+        self.assertEqual(next(flattened), 5)
+        self.assertEqual(next(flattened), 6)
+        self.assertEqual(next(flattened), 7)
+        self.assertEqual(next(flattened), 8)
+        self.assertRaises(StopIteration, next, flattened)
+
+        nested_list = [[3, [4, 5], 6], 7, [8]]
+        flattened = flatten_iter(nested_list)
+        self.assertEqual(next(flattened), 3)
+        self.assertEqual(next(flattened), 4)
+        self.assertEqual(next(flattened), 5)
+        self.assertEqual(next(flattened), 6)
+        self.assertEqual(next(flattened), 7)
+        self.assertEqual(next(flattened), 8)
+        self.assertRaises(StopIteration, next, flattened)
+
+        nested_list = [[], [8]]
+        flattened = flatten_iter(nested_list)
+        self.assertEqual(next(flattened), 8)
+        self.assertRaises(StopIteration, next, flattened)
+
+
+class TestGarage(unittest.TestCase):
+
+    def test_garage(self):
+
+        initial = [1, 2, 3, 0, 4]
+        final = [0, 3, 2, 1, 4]
+        steps, seq = garage(initial, final)
+
+        self.assertEqual(steps, 4)
+        self.assertListEqual(seq, [[0, 2, 3, 1, 4], [2, 0, 3, 1, 4], [2, 3, 0, 1, 4], [0, 3, 2, 1, 4]])
+
+
+class TestLongestNonRepeat(unittest.TestCase):
+
+    def test_longest_non_repeat(self):
+
+        string = "abcabcbb"
+        self.assertEqual(longest_non_repeat(string), 3)
+
+        string = "bbbbb"
+        self.assertEqual(longest_non_repeat(string), 1)
+
+        string = "pwwkew"
+        self.assertEqual(longest_non_repeat(string), 3)
+
+    def test_longest_non_repeat_two(self):
+
+        string = "abcabcbb"
+        self.assertEqual(longest_non_repeat_two(string), 3)
+
+        string = "bbbbb"
+        self.assertEqual(longest_non_repeat_two(string), 1)
+
+        string = "pwwkew"
+        self.assertEqual(longest_non_repeat_two(string), 3)
+
+
+class TestMergeInterval(unittest.TestCase):
+
+    def test_merge(self):
+        interval_list = [[1, 3], [2, 6], [8, 10], [15, 18]]
+        intervals = [Interval(i[0], i[1]) for i in interval_list]
+        merged_intervals = Interval.merge(intervals)
+        self.assertEqual(
+            merged_intervals,
+            [Interval(1, 6), Interval(8, 10), Interval(15, 18)]
+        )
+
+    def test_merge_v2(self):
+        interval_list = [[1, 3], [2, 6], [8, 10], [15, 18]]
+        merged_intervals = merge_v2(interval_list)
+        self.assertEqual(
+            merged_intervals,
+            [[1, 6], [8, 10], [15, 18]]
+        )
+
+
+class TestMissingRanges(unittest.TestCase):
+
+    def test_missing_ranges(self):
+
+        arr = [3, 5, 10, 11, 12, 15, 19]
+
+        self.assertListEqual(missing_ranges(arr, 0, 20),
+                             [(0, 2), (4, 4), (6, 9), (13, 14), (16, 18), (20, 20)])
+
+        self.assertListEqual(missing_ranges(arr, 6, 100),
+                             [(6, 9), (13, 14), (16, 18), (20, 100)])
+
+
+class TestMoveZeros(unittest.TestCase):
+
+    def test_move_zeros(self):
+
+        self.assertListEqual(move_zeros([False, 1, 0, 1, 2, 0, 1, 3, "a"]),
+                             [False, 1, 1, 2, 1, 3, "a", 0, 0])
+
+        self.assertListEqual(move_zeros([0, 34, 'rahul', [], None, 0, True, 0]),
+                             [34, 'rahul', [], None, True, 0, 0, 0])
+
+
+class TestPlusOne(unittest.TestCase):
+
+    def test_plus_one(self):
+
+        self.assertListEqual(plus_one([0]), [1])
+        self.assertListEqual(plus_one([9]), [1, 0])
+        self.assertListEqual(plus_one([1, 0, 9]), [1, 1, 0])
+        self.assertListEqual(plus_one([9, 9, 8, 0, 0, 9]), [9, 9, 8, 0, 1, 0])
+        self.assertListEqual(plus_one([9, 9, 9, 9]), [1, 0, 0, 0, 0])
+
+    def test_plus_one_v2(self):
+
+        self.assertListEqual(plus_one_v2([0]), [1])
+        self.assertListEqual(plus_one_v2([9]), [1, 0])
+        self.assertListEqual(plus_one_v2([1, 0, 9]), [1, 1, 0])
+        self.assertListEqual(plus_one_v2([9, 9, 8, 0, 0, 9]), [9, 9, 8, 0, 1, 0])
+        self.assertListEqual(plus_one_v2([9, 9, 9, 9]), [1, 0, 0, 0, 0])
+
+    def test_plus_one_v3(self):
+
+        self.assertListEqual(plus_one_v3([0]), [1])
+        self.assertListEqual(plus_one_v3([9]), [1, 0])
+        self.assertListEqual(plus_one_v3([1, 0, 9]), [1, 1, 0])
+        self.assertListEqual(plus_one_v3([9, 9, 8, 0, 0, 9]), [9, 9, 8, 0, 1, 0])
+        self.assertListEqual(plus_one_v3([9, 9, 9, 9]), [1, 0, 0, 0, 0])
+
+
+class TestRotateArray(unittest.TestCase):
+
+    def test_rotate_v1(self):
+
+        self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=3), [5, 6, 7, 1, 2, 3, 4])
+        self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=1), [7, 1, 2, 3, 4, 5, 6])
+        self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=7), [1, 2, 3, 4, 5, 6, 7])
+        self.assertListEqual(rotate_v1([1, 2], k=111), [2, 1])
+
+    def test_rotate_v2(self):
+
+        self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=3), [5, 6, 7, 1, 2, 3, 4])
+        self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=1), [7, 1, 2, 3, 4, 5, 6])
+        self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=7), [1, 2, 3, 4, 5, 6, 7])
+        self.assertListEqual(rotate_v2([1, 2], k=111), [2, 1])
+
+    def test_rotate_v3(self):
+
+        self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=3), [5, 6, 7, 1, 2, 3, 4])
+        self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=1), [7, 1, 2, 3, 4, 5, 6])
+        self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=7), [1, 2, 3, 4, 5, 6, 7])
+        self.assertListEqual(rotate_v3([1, 2], k=111), [2, 1])
+
+
+class TestSummaryRanges(unittest.TestCase):
+
+    def test_summary_ranges(self):
+
+        self.assertListEqual(summary_ranges([0, 1, 2, 4, 5, 7]),
+                             [(0, 2), (4, 5), (7, 7)])
+        self.assertListEqual(summary_ranges([-5, -4, -3, 1, 2, 4, 5, 6]),
+                             [(-5, -3), (1, 2), (4, 6)])
+        self.assertListEqual(summary_ranges([-2, -1, 0, 1, 2]),
+                             [(-2, 2)])
+
+
+class TestThreeSum(unittest.TestCase):
+
+    def test_three_sum(self):
+
+        self.assertSetEqual(three_sum([-1, 0, 1, 2, -1, -4]),
+                            {(-1, 0, 1), (-1, -1, 2)})
+
+        self.assertSetEqual(three_sum([-1, 3, 1, 2, -1, -4, -2]),
+                            {(-4, 1, 3), (-2, -1, 3), (-1, -1, 2)})
+
+
+class TestSuite(unittest.TestCase):
+
+    def test_two_sum(self):
+
+        self.assertTupleEqual((0, 2), two_sum([2, 11, 7, 9], target=9))
+        self.assertTupleEqual((0, 3), two_sum([-3, 5, 2, 3, 8, -9], target=0))
+
+        self.assertIsNone(two_sum([-3, 5, 2, 3, 8, -9], target=6))
+
+
+if __name__ == '__main__':
+
+    unittest.main()