Skip to content

Commit

Permalink
Merge pull request keon#211 from goswami-rahul/move-tests
Browse files Browse the repository at this point in the history
separate tests in array/ module
  • Loading branch information
keon authored Apr 11, 2018
2 parents cc1e41a + 91ad51e commit 2b79795
Show file tree
Hide file tree
Showing 27 changed files with 365 additions and 381 deletions.
15 changes: 15 additions & 0 deletions .cache/v/cache/lastfailed
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__/
*.py[cod]
.idea/
.cache/
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ 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
- flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics
# 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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
Empty file added array/__init__.py
Empty file.
41 changes: 0 additions & 41 deletions array/circular_counter.py

This file was deleted.

36 changes: 0 additions & 36 deletions array/delete_nth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
which leads to [1,2,3,1,2,3]
"""
import collections
import unittest


# Time complexity O(n^2)
Expand All @@ -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()

65 changes: 9 additions & 56 deletions array/flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
"""
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):
Expand All @@ -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()
18 changes: 0 additions & 18 deletions array/garage.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
2 3 0 1 4
0 3 2 1 4
"""
import unittest


def garage(initial, final):
Expand All @@ -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()
20 changes: 20 additions & 0 deletions array/josephus_problem.py
Original file line number Diff line number Diff line change
@@ -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
31 changes: 0 additions & 31 deletions array/longest_non_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Note that the answer must be a substring,
"pwke" is a subsequence and not a substring.
"""
import unittest


def longest_non_repeat(string):
Expand Down Expand Up @@ -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()
25 changes: 0 additions & 25 deletions array/merge_intervals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Given a collection of intervals, merge all overlapping intervals.
"""
import unittest


class Interval:
Expand Down Expand Up @@ -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()
Loading

0 comments on commit 2b79795

Please sign in to comment.