Skip to content

Commit

Permalink
Fix flake8 issues (keon#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankit167 authored Mar 2, 2022
1 parent 617d2d5 commit 87eae9a
Show file tree
Hide file tree
Showing 28 changed files with 665 additions and 468 deletions.
5 changes: 3 additions & 2 deletions algorithms/tree/is_balanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __get_depth(root):
"""
if root is None:
return 0
left = __get_depth(root.left)
left = __get_depth(root.left)
right = __get_depth(root.right)
if abs(left-right) > 1 or -1 in [left, right]:
return -1
Expand All @@ -28,7 +28,8 @@ def __get_depth(root):
# """
# left = max_height(root.left)
# right = max_height(root.right)
# return abs(left-right) <= 1 and is_balanced(root.left) and is_balanced(root.right)
# return abs(left-right) <= 1 and is_balanced(root.left) and
# is_balanced(root.right)

# def max_height(root):
# if root is None:
Expand Down
14 changes: 7 additions & 7 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.todo',
'sphinx.ext.coverage',
'sphinx.ext.mathjax',
'sphinx.ext.ifconfig',
'sphinx.ext.viewcode',
'sphinx.ext.githubpages']
'sphinx.ext.doctest',
'sphinx.ext.todo',
'sphinx.ext.coverage',
'sphinx.ext.mathjax',
'sphinx.ext.ifconfig',
'sphinx.ext.viewcode',
'sphinx.ext.githubpages']

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
import io
import re
from setuptools import find_packages, setup


Expand Down
90 changes: 56 additions & 34 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
flatten_iter, flatten,
garage,
josephus,
longest_non_repeat_v1, longest_non_repeat_v2, get_longest_non_repeat_v1, get_longest_non_repeat_v2,
longest_non_repeat_v1, longest_non_repeat_v2,
get_longest_non_repeat_v1, get_longest_non_repeat_v2,
Interval, merge_intervals,
missing_ranges,
move_zeros,
Expand Down Expand Up @@ -51,12 +52,14 @@ def test_delete_nth_naive(self):
[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, 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),
[1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1],
n=0),
[])

def test_delete_nth(self):
Expand All @@ -65,11 +68,13 @@ def test_delete_nth(self):
[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),
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),
self.assertListEqual(delete_nth([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4,
5, 3, 1], n=0),
[])


Expand Down Expand Up @@ -169,9 +174,8 @@ def test_longest_non_repeat_v2(self):

string = "asjrgapa"
self.assertEqual(longest_non_repeat_v2(string), 6)

def test_get_longest_non_repeat_v1(self):

string = "abcabcbb"
self.assertEqual(get_longest_non_repeat_v1(string), (3, 'abc'))

Expand All @@ -188,7 +192,6 @@ def test_get_longest_non_repeat_v1(self):
self.assertEqual(get_longest_non_repeat_v1(string), (6, 'sjrgap'))

def test_get_longest_non_repeat_v2(self):

string = "abcabcbb"
self.assertEqual(get_longest_non_repeat_v2(string), (3, 'abc'))

Expand All @@ -209,9 +212,12 @@ class TestMaxOnesIndex(unittest.TestCase):

def test_max_ones_index(self):

self.assertEqual(9, max_ones_index([1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1]))
self.assertEqual(3, max_ones_index([1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1]))
self.assertEqual(-1, max_ones_index([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
self.assertEqual(9, max_ones_index([1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1,
1, 1]))
self.assertEqual(3, max_ones_index([1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1,
1, 1]))
self.assertEqual(-1, max_ones_index([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1]))


class TestMergeInterval(unittest.TestCase):
Expand Down Expand Up @@ -255,7 +261,8 @@ 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]),
self.assertListEqual(move_zeros([0, 34, 'rahul', [], None, 0,
True, 0]),
[34, 'rahul', [], None, True, 0, 0, 0])


Expand Down Expand Up @@ -297,31 +304,31 @@ 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])
[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])
[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])
[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])
[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])
[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])
[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])
[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])
[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])
[1, 2, 3, 4, 5, 6, 7])
self.assertListEqual(rotate_v3([1, 2], k=111), [2, 1])


Expand Down Expand Up @@ -363,14 +370,16 @@ class TestTrimmean(unittest.TestCase):
def test_trimmean(self):

self.assertEqual(trimmean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 20), 5.5)
self.assertEqual(trimmean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 20), 6.0)
self.assertEqual(trimmean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 20),
6.0)


class TestTop1(unittest.TestCase):

def test_top_1(self):
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])
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):
Expand All @@ -385,21 +394,34 @@ class TestNSum(unittest.TestCase):

def test_n_sum(self):
self.assertEqual(n_sum(2, [-3, 5, 2, 3, 8, -9], 6), []) # noqa: E501
self.assertEqual(n_sum(3, [-5, -4, -3, -2, -1, 0, 1, 2, 3], 0), sorted([[-5,2,3],[-2,0,2],[-4,1,3],[-3,1,2],[-1,0,1],[-2,-1,3],[-3,0,3]])) # noqa: E501
self.assertEqual(n_sum(3, [-1,0,1,2,-1,-4], 0), sorted([[-1,-1,2],[-1,0,1]])) # noqa: E501
self.assertEqual(n_sum(4, [1, 0, -1, 0, -2, 2], 0), sorted([[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]])) # noqa: E501
self.assertEqual(n_sum(3, [-5, -4, -3, -2, -1, 0, 1, 2, 3], 0),
sorted([[-5, 2, 3], [-2, 0, 2], [-4, 1, 3],
[-3, 1, 2], [-1, 0, 1], [-2, -1, 3],
[-3, 0, 3]])) # noqa: E501
self.assertEqual(n_sum(3, [-1, 0, 1, 2, -1, -4], 0),
sorted([[-1, -1, 2], [-1, 0, 1]])) # noqa: E501
self.assertEqual(n_sum(4, [1, 0, -1, 0, -2, 2], 0),
sorted([[-2, -1, 1, 2], [-2, 0, 0, 2],
[-1, 0, 0, 1]])) # noqa: E501
self.assertEqual(n_sum(4, [7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 4, -3, -2], 10), sorted([[-6, 2, 7, 7], [-6, 3, 6, 7], [-6, 4, 5, 7], [-6, 4, 6, 6], [-5, 1, 7, 7], [-5, 2, 6, 7], [-5, 3, 5, 7], [-5, 3, 6, 6], [-5, 4, 4, 7], [-5, 4, 5, 6], [-4, 0, 7, 7], [-4, 1, 6, 7], [-4, 2, 5, 7], [-4, 2, 6, 6], [-4, 3, 4, 7], [-4, 3, 5, 6], [-4, 4, 4, 6], [-3, -1, 7, 7], [-3, 0, 6, 7], [-3, 1, 5, 7], [-3, 1, 6, 6], [-3, 2, 4, 7], [-3, 2, 5, 6], [-3, 3, 4, 6], [-3, 4, 4, 5], [-2, -2, 7, 7], [-2, -1, 6, 7], [-2, 0, 5, 7], [-2, 0, 6, 6], [-2, 1, 4, 7], [-2, 1, 5, 6], [-2, 2, 3, 7], [-2, 2, 4, 6], [-2, 3, 4, 5], [-1, 0, 4, 7], [-1, 0, 5, 6], [-1, 1, 3, 7], [-1, 1, 4, 6], [-1, 2, 3, 6], [-1, 2, 4, 5], [-1, 3, 4, 4], [0, 1, 2, 7], [0, 1, 3, 6], [0, 1, 4, 5], [0, 2, 3, 5], [0, 2, 4, 4], [1, 2, 3, 4]])) # noqa: E501

self.assertEqual(n_sum(2, [[-3, 0], [-2, 1], [2, 2], [3, 3], [8, 4], [-9, 5]], 0, # noqa: E501
self.assertEqual(n_sum(2, [[-3, 0], [-2, 1], [2, 2], [3, 3], [8, 4],
[-9, 5]], 0, # noqa: E501
sum_closure=lambda a, b: a[0] + b[0]), # noqa: E501
[[[-3, 0], [3, 3]], [[-2, 1], [2, 2]]]) # noqa: E501
self.assertEqual(n_sum(2, [[-3, 0], [-2, 1], [2, 2], [3, 3], [8, 4], [-9, 5]], [0, 3], # noqa: E501
sum_closure=lambda a, b: [a[0] + b[0], a[1] + b[1]], # noqa: E501
same_closure=lambda a, b: a[0] == b[0] and a[1] == b[1]), # noqa: E501
self.assertEqual(n_sum(2, [[-3, 0], [-2, 1], [2, 2], [3, 3], [8, 4],
[-9, 5]], [0, 3], # noqa: E501
sum_closure=lambda a, b: [a[0] + b[0],
a[1] + b[1]], # noqa: E501
same_closure=lambda a, b: a[0] == b[0]
and a[1] == b[1]), # noqa: E501
[[[-3, 0], [3, 3]], [[-2, 1], [2, 2]]]) # noqa: E501
self.assertEqual(n_sum(2, [[-3, 0], [-2, 1], [2, 2], [3, 3], [8, 4], [-9, 5]], -5, # noqa: E501
sum_closure=lambda a, b: [a[0] + b[1], a[1] + b[0]], # noqa: E501
compare_closure=lambda a, b: -1 if a[0] < b else 1 if a[0] > b else 0), # noqa: E501
self.assertEqual(n_sum(2, [[-3, 0], [-2, 1], [2, 2], [3, 3],
[8, 4], [-9, 5]], -5, # noqa: E501
sum_closure=lambda a, b: [a[0] + b[1],
a[1] + b[0]], # noqa: E501
compare_closure=lambda a, b: -1 if a[0] < b
else 1 if a[0] > b else 0), # noqa: E501
[[[-9, 5], [8, 4]]]) # noqa: E501


Expand Down
22 changes: 11 additions & 11 deletions tests/test_automata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@

class TestDFA(unittest.TestCase):
def test_DFA(self):

transitions = {
'a': {'1': 'a', '0': 'b'},
'b': {'1': 'b', '0': 'a'}
}

final=['a']
final = ['a']
start = 'a'

self.assertEqual(False, DFA(transitions, start, final, "000111100"))
self.assertEqual(True, DFA(transitions, start, final, "111000011"))

transitions1 = {
'0': {'0': '1', '1': '0'},
'1': {'0': '2', '1': '0'},
'2': {'0': '2', '1': '3'},
'3': {'0': '3', '1': '3'}
}
}

final1 = ['0', '1', '2']
start1 = '0'

self.assertEqual(False, DFA(transitions1, start1, final1, "0001111"))
self.assertEqual(True, DFA(transitions1, start1, final1, "01010101"))

transitions2 = {
'0': {'a': '0', 'b': '1'},
'1': {'a': '0', 'b': '2'},
'2': {'a': '3', 'b': '2'},
'3': {'a': '3', 'b': '3'}
}
final2=['3']

final2 = ['3']
start2 = '0'

self.assertEqual(False, DFA(transitions2, start2, final2, "aaabbb"))
self.assertEqual(True, DFA(transitions2, start2, final2, "baabba"))



if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit 87eae9a

Please sign in to comment.