diff --git a/.github/linters/.flake8 b/.github/linters/.flake8 index ca4457b..31b8cfd 100644 --- a/.github/linters/.flake8 +++ b/.github/linters/.flake8 @@ -1,3 +1,3 @@ [flake8] max-line-length = 200 -extend-ignore = E203, E704 +extend-ignore = E203, E704, E741 diff --git a/arithmetic_expression.py b/arithmetic_expression.py index 7b49892..a2d6d70 100644 --- a/arithmetic_expression.py +++ b/arithmetic_expression.py @@ -3,8 +3,7 @@ from itertools import pairwise from math import factorial - -## METHOD 1 +# METHOD 1 class Reader: @@ -106,7 +105,7 @@ def consume_or(): return int(consume_brackets()) -## METHOD 2 +# METHOD 2 class PostfixConverter: @@ -120,7 +119,7 @@ def __init__( self.whitespace = whitespace self.open = {brackets[i]: brackets[i + 1] for i in range(0, len(brackets), 2)} self.close = {brackets[i + 1]: brackets[i] for i in range(0, len(brackets), 2)} - + operator_order = operator_order or [] precedence = {} for p, ops in enumerate(reversed(operator_order)): @@ -160,7 +159,7 @@ def convert(self, expression) -> list: p = self.precedence_unary[ch] elif ch in self.precedence: p = self.precedence[ch] - else: + else: yield ch prefix_context = False continue diff --git a/averages.py b/averages.py index ba736b1..45c91c0 100644 --- a/averages.py +++ b/averages.py @@ -1,6 +1,6 @@ import re import unittest -from math import nan, isnan +from math import isnan, nan from statistics import mean from typing import Optional @@ -197,7 +197,9 @@ def test_compute_average_expression(self): def test_dump_arguments(self): self.assertEqual(dump_arguments([("arg", 0.056)], accuracy=2), "arg=0.06") - self.assertEqual(dump_arguments([("a", 1), ("b", .24)], accuracy=1), "a=1.0 b=0.2") + self.assertEqual( + dump_arguments([("a", 1), ("b", 0.24)], accuracy=1), "a=1.0 b=0.2" + ) if __name__ == "__main__": diff --git a/dynamic-2d.py b/dynamic-2d.py index 107d895..c2a31b6 100644 --- a/dynamic-2d.py +++ b/dynamic-2d.py @@ -1,6 +1,6 @@ -import sys from functools import cache + def main(): n, m = (int(v) for v in input().split()) table = [[int(v) for v in input().split()] for _ in range(n)] @@ -14,12 +14,12 @@ def neighbours(x, y): def answer(x, y): if x == 0 and y == 1: return 0 - if not (1 <= x <= m and 1 <= y <=n): + if not (1 <= x <= m and 1 <= y <= n): return float("inf") return table[y - 1][x - 1] + min(answer(*n) for n in neighbours(x, y)) print(answer(m, n)) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/lc_0014_longest_common.py b/lc_0014_longest_common.py index 8654d1a..3e40e94 100644 --- a/lc_0014_longest_common.py +++ b/lc_0014_longest_common.py @@ -1,7 +1,7 @@ """14. Longest Common Prefix""" +import unittest from itertools import takewhile, zip_longest from typing import List -import unittest class Solution1: diff --git a/lc_0094_binary_in_order.py b/lc_0094_binary_in_order.py index 7ba4ab1..a5f4150 100644 --- a/lc_0094_binary_in_order.py +++ b/lc_0094_binary_in_order.py @@ -12,6 +12,9 @@ Memory Usage: 14.4 MB, less than 14.09% of Python3 online submissions for Binary Tree Inorder Traversal. """ +from typing import List, Optional + + # Definition for a binary tree node. class TreeNode: def __init__(self, val=0, left=None, right=None): @@ -19,8 +22,6 @@ def __init__(self, val=0, left=None, right=None): self.left = left self.right = right -from typing import List, Optional - class Solution: def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: diff --git a/lc_0212_word_search_original.py b/lc_0212_word_search_original.py index 9af792b..87555e0 100644 --- a/lc_0212_word_search_original.py +++ b/lc_0212_word_search_original.py @@ -1,8 +1,10 @@ +from collections import defaultdict, deque from typing import List + class Solution: def findWords(self, board: List[List[str]], words: List[str]) -> List[str]: - m, n = len(board), len(board[0]) + n = len(board[0]) B = n + 2 def g_enumerate(): @@ -13,7 +15,7 @@ def g_enumerate(): def neighbors(p): """List of neighboring cells""" - return (p + 1, p - 1, p + R, p - R) + return p + 1, p - 1, p + B, p - B grid = defaultdict(lambda: None) | dict(g_enumerate()) chars = set(grid.values()) diff --git a/lc_0329_longest_path_in_matrix.py b/lc_0329_longest_path_in_matrix.py index 6f9e00a..1ae105e 100644 --- a/lc_0329_longest_path_in_matrix.py +++ b/lc_0329_longest_path_in_matrix.py @@ -16,13 +16,14 @@ """ from collections import defaultdict +from itertools import count from typing import List class Solution: def longestIncreasingPath(self, matrix: List[List[int]]) -> int: m, n = len(matrix), len(matrix[0]) - R = n + 2 # 😕 Now unused + _ = n + 2 # 😕 Now unused # 😕 This has been rewritten back to regular pairs def m_enumerate(): diff --git a/lc_0354_envelopes.py b/lc_0354_envelopes.py index 17713c2..110afb3 100644 --- a/lc_0354_envelopes.py +++ b/lc_0354_envelopes.py @@ -1,7 +1,7 @@ +import unittest from bisect import bisect_left from math import inf from typing import List -import unittest class SolutionShort: diff --git a/lc_0431_nary_to_binary.py b/lc_0431_nary_to_binary.py index 08a0a68..4d6efb1 100644 --- a/lc_0431_nary_to_binary.py +++ b/lc_0431_nary_to_binary.py @@ -1,8 +1,8 @@ """431. Encode N-ary Tree to Binary Tree""" import operator -from typing import Optional import unittest +from typing import Optional class NAryNode: diff --git a/lc_0460_lfu_cache.py b/lc_0460_lfu_cache.py index 7a72734..57833ba 100644 --- a/lc_0460_lfu_cache.py +++ b/lc_0460_lfu_cache.py @@ -8,8 +8,8 @@ Memory Usage: 77.7 MB, less than 98.42% of Python3 online submissions for LFU Cache. """ -from collections import defaultdict import unittest +from collections import defaultdict def debug(*_): diff --git a/lc_0621_task_scheduler.py b/lc_0621_task_scheduler.py index 722b82e..228c1d0 100644 --- a/lc_0621_task_scheduler.py +++ b/lc_0621_task_scheduler.py @@ -1,7 +1,7 @@ +import unittest from collections import Counter from itertools import count from typing import List -import unittest class Solution: diff --git a/lc_0691_stickers.py b/lc_0691_stickers.py index f9c8ceb..b38183c 100644 --- a/lc_0691_stickers.py +++ b/lc_0691_stickers.py @@ -7,10 +7,10 @@ Memory Usage: 15.3 MB, less than 28.82% of Python3 online submissions for Stickers to Spell Word. """ +import unittest from collections import Counter from functools import cache from typing import List -import unittest def subtract(a, b, n=1): diff --git a/lc_0697_degree.py b/lc_0697_degree.py index f4c9f66..2f34488 100644 --- a/lc_0697_degree.py +++ b/lc_0697_degree.py @@ -7,9 +7,9 @@ """ +import unittest from collections import defaultdict from typing import List -import unittest class Solution: diff --git a/lc_0994_rotting_oranges.py b/lc_0994_rotting_oranges.py index c5e3d63..f9c9391 100644 --- a/lc_0994_rotting_oranges.py +++ b/lc_0994_rotting_oranges.py @@ -7,9 +7,9 @@ Memory Usage: 14.4 MB, less than 39.37% of Python3 online submissions for Rotting Oranges. """ +import unittest from itertools import count from typing import List -import unittest class Solution: diff --git a/lc_1192_critical_edges.py b/lc_1192_critical_edges.py index cf2971b..ec2a0a0 100644 --- a/lc_1192_critical_edges.py +++ b/lc_1192_critical_edges.py @@ -1,6 +1,8 @@ """1192. Critical Connections in a Network -There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections forming a network where connections[i] = [ai, bi] represents a connection between servers ai and bi. Any server can reach other servers directly or indirectly through the network. +There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections forming a network +where connections[i] = [ai, bi] represents a connection between servers ai and bi. Any server can reach other servers +directly or indirectly through the network. A critical connection is a connection that, if removed, will make some servers unable to reach some other server. diff --git a/lc_2132_stamp_grid.py b/lc_2132_stamp_grid.py index 5eb641b..9453cb2 100644 --- a/lc_2132_stamp_grid.py +++ b/lc_2132_stamp_grid.py @@ -9,10 +9,10 @@ """ -from itertools import chain -from typing import List import random import unittest +from itertools import chain +from typing import List def add_arrays(arrays): diff --git a/lc_template.py b/lc_template.py index ec0b725..4f3cf66 100644 --- a/lc_template.py +++ b/lc_template.py @@ -1,9 +1,10 @@ -import bisect -from itertools import * -from string import * -from collections import * +# import bisect import unittest +# from collections import * +# from itertools import * +# from string import * + # noinspection PyMethodMayBeStatic class MyTestCase(unittest.TestCase): @@ -14,12 +15,13 @@ def test_passing(self): assert 2 + 2 == 4 def test_failing1(self): -# assert 2 + 2 == 5 + # assert 2 + 2 == 5 pass - + def test_failing2(self): -# assert 2 + 2 == 5 + # assert 2 + 2 == 5 pass + if __name__ == "__main__": unittest.main() diff --git a/test_arithmetic_expression.py b/test_arithmetic_expression.py index 6a428d8..abc49fc 100644 --- a/test_arithmetic_expression.py +++ b/test_arithmetic_expression.py @@ -1,5 +1,16 @@ +import operator import unittest -from arithmetic_expression import * + +from arithmetic_expression import ( + ALL_OPS, + DIGITS, + Logger, + PostfixConverter, + Reader, + analyze1, + analyze2, + eval_postfix, +) class ReaderTests(unittest.TestCase): @@ -53,7 +64,7 @@ def check_conversions( try: true_value = eval(example) self.assertEqual(value, true_value) - except: + except Exception: pass def test_empty(self):