Skip to content

Commit 65b3ea4

Browse files
author
cclauss
committed
Fix flake8 related issues
1 parent 9495772 commit 65b3ea4

File tree

131 files changed

+792
-374
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+792
-374
lines changed

Diff for: Python/01-matrix.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
55
# The distance between two adjacent cells is 1.
66
#
7-
# Example 1:
7+
# Example 1:
88
#
99
# Input:
1010
# 0 0 0
@@ -16,7 +16,7 @@
1616
# 0 1 0
1717
# 0 0 0
1818
#
19-
# Example 2:
19+
# Example 2:
2020
#
2121
# Input:
2222
# 0 0 0
@@ -33,6 +33,9 @@
3333
# There are at least one 0 in the given matrix.
3434
# The cells are adjacent in only four directions: up, down, left and right.
3535

36+
import collections
37+
38+
3639
class Solution(object):
3740
def updateMatrix(self, matrix):
3841
"""
@@ -57,7 +60,7 @@ def updateMatrix(self, matrix):
5760
continue
5861
queue.append((i, j))
5962
matrix[i][j] = matrix[cell[0]][cell[1]]+1
60-
63+
6164
return matrix
6265

6366

Diff for: Python/4sum-ii.py

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
# 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2525
# 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
2626

27+
import collections
28+
29+
2730
class Solution(object):
2831
def fourSumCount(self, A, B, C, D):
2932
"""

Diff for: Python/4sum.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Time: O(n^3)
22
# Space: O(1)
33

4-
# Given an array S of n integers,
4+
# Given an array S of n integers,
55
# are there elements a, b, c, and d in S such that a + b + c + d = target?
66
# Find all unique quadruplets in the array which gives the sum of target.
77
#
@@ -16,6 +16,9 @@
1616
# (-2, 0, 0, 2)
1717
#
1818

19+
import collections
20+
21+
1922
# Two pointer solution. (1356ms)
2023
class Solution(object):
2124
def fourSum(self, nums, target):
@@ -62,7 +65,7 @@ def fourSum(self, nums, target):
6265
"""
6366
nums, result, lookup = sorted(nums), [], collections.defaultdict(list)
6467
for i in xrange(0, len(nums) - 1):
65-
for j in xrange(i + 1, len(nums)):
68+
for j in xrange(i + 1, len(nums)):
6669
is_duplicated = False
6770
for [x, y] in lookup[nums[i] + nums[j]]:
6871
if nums[x] == nums[i]:
@@ -95,7 +98,7 @@ def fourSum(self, nums, target):
9598
"""
9699
nums, result, lookup = sorted(nums), [], collections.defaultdict(list)
97100
for i in xrange(0, len(nums) - 1):
98-
for j in xrange(i + 1, len(nums)):
101+
for j in xrange(i + 1, len(nums)):
99102
lookup[nums[i] + nums[j]].append([i, j])
100103

101104
for i in lookup.keys():

Diff for: Python/accounts-merge.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
# The accounts themselves can be returned in any order.
1717
#
1818
# Example 1:
19-
# Input:
19+
# Input:
2020
# accounts = [["John", "[email protected]", "[email protected]"],
2121
# ["John", "[email protected]"],
2222
2323
# ["Mary", "[email protected]"]]
2424
2525
# ["John", "[email protected]"], ["Mary", "[email protected]"]]
2626
#
27-
# Explanation:
27+
# Explanation:
2828
# The first and third John's are the same person as they have the common email "[email protected]".
2929
# The second John and Mary are different people as none of their email addresses are used by other accounts.
3030
# We could return these lists in any order,
3131
# for example the answer [['Mary', '[email protected]'],
32-
# ['John', '[email protected]'],
32+
# ['John', '[email protected]'],
3333
3434
# would still be accepted.
3535
#
@@ -39,14 +39,17 @@
3939
# The length of accounts[i] will be in the range [1, 10].
4040
# The length of accounts[i][j] will be in the range [1, 30].
4141

42+
import collections
43+
44+
4245
class UnionFind(object):
4346
def __init__(self):
4447
self.set = []
4548

4649
def get_id(self):
4750
self.set.append(len(self.set))
4851
return len(self.set)-1
49-
52+
5053
def find_set(self, x):
5154
if self.set[x] != x:
5255
self.set[x] = self.find_set(self.set[x]) # path compression.

Diff for: Python/add-bold-tag-in-string.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Time: O(n * d * l), l is the average string length
22
# Space: O(n)
33

4+
import collections
5+
6+
47
# 59ms
58
class Solution(object):
69
def addBoldTag(self, s, dict):
@@ -25,7 +28,7 @@ def addBoldTag(self, s, dict):
2528
result.append("</b>");
2629
return "".join(result)
2730

28-
31+
2932
# Time: O(n * l), l is the average string length
3033
# Space: O(t) , t is the size of trie
3134
# trie solution, 439ms

Diff for: Python/alien-dictionary.py

+27-18
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11
# Time: O(n)
22
# Space: O(|V|+|E|) = O(26 + 26^2) = O(1)
33

4+
import collections
5+
6+
try:
7+
xrange # Python 2
8+
except NameError:
9+
xrange = range # Python 3
10+
11+
412
# BFS solution.
513
class Solution(object):
614
def alienOrder(self, words):
715
"""
816
:type words: List[str]
917
:rtype: str
1018
"""
11-
result, zero_in_degree_queue, in_degree, out_degree = [], collections.deque(), {}, {}
12-
nodes = sets.Set()
19+
result, in_degree, out_degree = [], {}, {}
20+
zero_in_degree_queue = collections.deque()
21+
nodes = set()
1322
for word in words:
1423
for c in word:
1524
nodes.add(c)
16-
25+
1726
for i in xrange(1, len(words)):
18-
if len(words[i-1]) > len(words[i]) and \
19-
words[i-1][:len(words[i])] == words[i]:
20-
return ""
27+
if (len(words[i-1]) > len(words[i]) and
28+
words[i-1][:len(words[i])] == words[i]):
29+
return ""
2130
self.findEdges(words[i - 1], words[i], in_degree, out_degree)
22-
31+
2332
for node in nodes:
2433
if node not in in_degree:
2534
zero_in_degree_queue.append(node)
26-
35+
2736
while zero_in_degree_queue:
2837
precedence = zero_in_degree_queue.popleft()
2938
result.append(precedence)
30-
39+
3140
if precedence in out_degree:
3241
for c in out_degree[precedence]:
3342
in_degree[c].discard(precedence)
3443
if not in_degree[c]:
3544
zero_in_degree_queue.append(c)
36-
45+
3746
del out_degree[precedence]
38-
47+
3948
if out_degree:
4049
return ""
4150

@@ -48,9 +57,9 @@ def findEdges(self, word1, word2, in_degree, out_degree):
4857
for i in xrange(str_len):
4958
if word1[i] != word2[i]:
5059
if word2[i] not in in_degree:
51-
in_degree[word2[i]] = sets.Set()
60+
in_degree[word2[i]] = set()
5261
if word1[i] not in out_degree:
53-
out_degree[word1[i]] = sets.Set()
62+
out_degree[word1[i]] = set()
5463
in_degree[word2[i]].add(word1[i])
5564
out_degree[word1[i]].add(word2[i])
5665
break
@@ -64,16 +73,16 @@ def alienOrder(self, words):
6473
:rtype: str
6574
"""
6675
# Find ancestors of each node by DFS.
67-
nodes, ancestors = sets.Set(), {}
76+
nodes, ancestors = set(), {}
6877
for i in xrange(len(words)):
6978
for c in words[i]:
7079
nodes.add(c)
7180
for node in nodes:
7281
ancestors[node] = []
7382
for i in xrange(1, len(words)):
74-
if len(words[i-1]) > len(words[i]) and \
75-
words[i-1][:len(words[i])] == words[i]:
76-
return ""
83+
if (len(words[i-1]) > len(words[i]) and
84+
words[i-1][:len(words[i])] == words[i]):
85+
return ""
7786
self.findEdges(words[i - 1], words[i], ancestors)
7887

7988
# Output topological order by DFS.
@@ -82,7 +91,7 @@ def alienOrder(self, words):
8291
for node in nodes:
8392
if self.topSortDFS(node, node, ancestors, visited, result):
8493
return ""
85-
94+
8695
return "".join(result)
8796

8897

Diff for: Python/arithmetic-slices-ii-subsequence.py

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
# [2,4,6,8,10]
4343
# [2,6,10]
4444

45+
import collections
46+
47+
4548
class Solution(object):
4649
def numberOfArithmeticSlices(self, A):
4750
"""

Diff for: Python/arranging-coins.py

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#
3131
# Because the 4th row is incomplete, we return 3.
3232

33+
import math
34+
35+
3336
class Solution(object):
3437
def arrangeCoins(self, n):
3538
"""

Diff for: Python/basic-calculator-iv.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# have a leading coefficient or unary operator like "2x" or "-x".
1616
#
1717
# Expressions are evaluated in the usual order:
18-
# brackets first, then multiplication, then addition and subtraction.
18+
# brackets first, then multiplication, then addition and subtraction.
1919
# For example, expression = "1 + 2 * 3" has an answer of ["7"].
2020
#
2121
# The format of the output is as follows:
@@ -26,9 +26,9 @@
2626
# counting multiplicity. (For example, "a*a*b*c" has degree 4.)
2727
# We write the largest degree terms of our answer first,
2828
# breaking ties by lexicographic order ignoring the leading coefficient of the term.
29-
# - The leading coefficient of the term is placed directly to the left with an asterisk separating it
29+
# - The leading coefficient of the term is placed directly to the left with an asterisk separating it
3030
# from the variables (if they exist.) A leading coefficient of 1 is still printed.
31-
# - An example of a well formatted answer is ["-2*a*a*a", "3*a*a*b", "3*b*b", "4*a", "5*c", "-6"]
31+
# - An example of a well formatted answer is ["-2*a*a*a", "3*a*a*b", "3*b*b", "4*a", "5*c", "-6"]
3232
# - Terms (including constant terms) with coefficient 0 are not included.
3333
# For example, an expression of "0" has an output of [].
3434
#
@@ -52,7 +52,7 @@
5252
#
5353
# Input: expression = "((a - b) * (b - c) + (c - a)) * ((a - b) + (b - c) * (c - a))",
5454
# evalvars = [], evalints = []
55-
# Output:
55+
# Output:
5656
# ["-1*a*a*b*b","2*a*a*b*c","-1*a*a*c*c","1*a*b*b*b","-1*a*b*b*c","-1*a*b*c*c",
5757
# "1*a*c*c*c","-1*b*b*b*c","2*b*b*c*c","-1*b*c*c*c","2*a*a*b","-2*a*a*c","-2*a*b*b",
5858
# "2*a*c*c","1*b*b*b","-1*b*b*c","1*b*c*c","-1*c*c*c","-1*a*a","1*a*b","1*a*c","-1*b*c"]
@@ -61,6 +61,10 @@
6161
# - expression will have length in range [1, 1000].
6262
# - evalvars, evalints will have equal lengths in range [0, 1000].
6363

64+
import collections
65+
import itertools
66+
67+
6468
class Poly(collections.Counter):
6569
def __init__(self, expr=None):
6670
if expr is None:
@@ -94,9 +98,9 @@ def merge(k1, k2):
9498
i += 1
9599
else:
96100
result.append(k2[j])
97-
j += 1
101+
j += 1
98102
return result
99-
103+
100104
result = Poly()
101105
for k1, v1 in self.items():
102106
for k2, v2 in other.items():
@@ -119,8 +123,8 @@ def to_list(self):
119123
return ["*".join((str(v),) + k) \
120124
for k, v in sorted(self.items(), key=lambda(k, _): (-len(k), k)) \
121125
if v]
122-
123-
126+
127+
124128
class Solution(object):
125129
def basicCalculatorIV(self, expression, evalvars, evalints):
126130
"""
@@ -138,7 +142,7 @@ def compute(operands, operators):
138142
operands.append(left - right)
139143
elif op == '*':
140144
operands.append(left * right)
141-
145+
142146
def parse(s):
143147
if not s:
144148
return Poly()
@@ -163,6 +167,6 @@ def parse(s):
163167
while operators:
164168
compute(operands, operators)
165169
return operands[-1]
166-
170+
167171
lookup = dict(itertools.izip(evalvars, evalints))
168172
return parse(expression).eval(lookup).to_list()

Diff for: Python/binary-tree-vertical-order-traversal.py

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
# self.left = None
99
# self.right = None
1010

11+
import collections
12+
13+
1114
# BFS + hash solution.
1215
class Solution(object):
1316
def verticalOrder(self, root):

Diff for: Python/bold-words-in-string.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Time: O(n * l), n is the length of S, l is the average length of words
22
# Space: O(t) , t is the size of trie
33

4+
import collections
5+
6+
47
class Solution(object):
58
def boldWords(self, words, S):
69
"""

Diff for: Python/brick-wall.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# in which case the line will obviously cross no bricks.
1616
#
1717
# Example:
18-
# Input:
18+
# Input:
1919
# [[1,2,2,1],
2020
# [3,1,2],
2121
# [1,3,2],
@@ -30,6 +30,9 @@
3030
# The height of wall is in range [1,10,000].
3131
# Total number of bricks of the wall won't exceed 20,000.
3232

33+
import collections
34+
35+
3336
class Solution(object):
3437
def leastBricks(self, wall):
3538
"""

0 commit comments

Comments
 (0)