Skip to content

Commit

Permalink
Fix flake8 related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Apr 1, 2018
1 parent 9495772 commit 65b3ea4
Show file tree
Hide file tree
Showing 131 changed files with 792 additions and 374 deletions.
9 changes: 6 additions & 3 deletions Python/01-matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
# The distance between two adjacent cells is 1.
#
# Example 1:
# Example 1:
#
# Input:
# 0 0 0
Expand All @@ -16,7 +16,7 @@
# 0 1 0
# 0 0 0
#
# Example 2:
# Example 2:
#
# Input:
# 0 0 0
Expand All @@ -33,6 +33,9 @@
# There are at least one 0 in the given matrix.
# The cells are adjacent in only four directions: up, down, left and right.

import collections


class Solution(object):
def updateMatrix(self, matrix):
"""
Expand All @@ -57,7 +60,7 @@ def updateMatrix(self, matrix):
continue
queue.append((i, j))
matrix[i][j] = matrix[cell[0]][cell[1]]+1

return matrix


Expand Down
3 changes: 3 additions & 0 deletions Python/4sum-ii.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
# 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

import collections


class Solution(object):
def fourSumCount(self, A, B, C, D):
"""
Expand Down
9 changes: 6 additions & 3 deletions Python/4sum.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Time: O(n^3)
# Space: O(1)

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

import collections


# Two pointer solution. (1356ms)
class Solution(object):
def fourSum(self, nums, target):
Expand Down Expand Up @@ -62,7 +65,7 @@ def fourSum(self, nums, target):
"""
nums, result, lookup = sorted(nums), [], collections.defaultdict(list)
for i in xrange(0, len(nums) - 1):
for j in xrange(i + 1, len(nums)):
for j in xrange(i + 1, len(nums)):
is_duplicated = False
for [x, y] in lookup[nums[i] + nums[j]]:
if nums[x] == nums[i]:
Expand Down Expand Up @@ -95,7 +98,7 @@ def fourSum(self, nums, target):
"""
nums, result, lookup = sorted(nums), [], collections.defaultdict(list)
for i in xrange(0, len(nums) - 1):
for j in xrange(i + 1, len(nums)):
for j in xrange(i + 1, len(nums)):
lookup[nums[i] + nums[j]].append([i, j])

for i in lookup.keys():
Expand Down
11 changes: 7 additions & 4 deletions Python/accounts-merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
# The accounts themselves can be returned in any order.
#
# Example 1:
# Input:
# Input:
# accounts = [["John", "[email protected]", "[email protected]"],
# ["John", "[email protected]"],
# ["John", "[email protected]", "[email protected]"],
# ["Mary", "[email protected]"]]
# Output: [["John", '[email protected]', '[email protected]', '[email protected]'],
# ["John", "[email protected]"], ["Mary", "[email protected]"]]
#
# Explanation:
# Explanation:
# The first and third John's are the same person as they have the common email "[email protected]".
# The second John and Mary are different people as none of their email addresses are used by other accounts.
# We could return these lists in any order,
# for example the answer [['Mary', '[email protected]'],
# ['John', '[email protected]'],
# ['John', '[email protected]'],
# ['John', '[email protected]', '[email protected]', '[email protected]']]
# would still be accepted.
#
Expand All @@ -39,14 +39,17 @@
# The length of accounts[i] will be in the range [1, 10].
# The length of accounts[i][j] will be in the range [1, 30].

import collections


class UnionFind(object):
def __init__(self):
self.set = []

def get_id(self):
self.set.append(len(self.set))
return len(self.set)-1

def find_set(self, x):
if self.set[x] != x:
self.set[x] = self.find_set(self.set[x]) # path compression.
Expand Down
5 changes: 4 additions & 1 deletion Python/add-bold-tag-in-string.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Time: O(n * d * l), l is the average string length
# Space: O(n)

import collections


# 59ms
class Solution(object):
def addBoldTag(self, s, dict):
Expand All @@ -25,7 +28,7 @@ def addBoldTag(self, s, dict):
result.append("</b>");
return "".join(result)


# Time: O(n * l), l is the average string length
# Space: O(t) , t is the size of trie
# trie solution, 439ms
Expand Down
45 changes: 27 additions & 18 deletions Python/alien-dictionary.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
# Time: O(n)
# Space: O(|V|+|E|) = O(26 + 26^2) = O(1)

import collections

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


# BFS solution.
class Solution(object):
def alienOrder(self, words):
"""
:type words: List[str]
:rtype: str
"""
result, zero_in_degree_queue, in_degree, out_degree = [], collections.deque(), {}, {}
nodes = sets.Set()
result, in_degree, out_degree = [], {}, {}
zero_in_degree_queue = collections.deque()
nodes = set()
for word in words:
for c in word:
nodes.add(c)

for i in xrange(1, len(words)):
if len(words[i-1]) > len(words[i]) and \
words[i-1][:len(words[i])] == words[i]:
return ""
if (len(words[i-1]) > len(words[i]) and
words[i-1][:len(words[i])] == words[i]):
return ""
self.findEdges(words[i - 1], words[i], in_degree, out_degree)

for node in nodes:
if node not in in_degree:
zero_in_degree_queue.append(node)

while zero_in_degree_queue:
precedence = zero_in_degree_queue.popleft()
result.append(precedence)

if precedence in out_degree:
for c in out_degree[precedence]:
in_degree[c].discard(precedence)
if not in_degree[c]:
zero_in_degree_queue.append(c)

del out_degree[precedence]

if out_degree:
return ""

Expand All @@ -48,9 +57,9 @@ def findEdges(self, word1, word2, in_degree, out_degree):
for i in xrange(str_len):
if word1[i] != word2[i]:
if word2[i] not in in_degree:
in_degree[word2[i]] = sets.Set()
in_degree[word2[i]] = set()
if word1[i] not in out_degree:
out_degree[word1[i]] = sets.Set()
out_degree[word1[i]] = set()
in_degree[word2[i]].add(word1[i])
out_degree[word1[i]].add(word2[i])
break
Expand All @@ -64,16 +73,16 @@ def alienOrder(self, words):
:rtype: str
"""
# Find ancestors of each node by DFS.
nodes, ancestors = sets.Set(), {}
nodes, ancestors = set(), {}
for i in xrange(len(words)):
for c in words[i]:
nodes.add(c)
for node in nodes:
ancestors[node] = []
for i in xrange(1, len(words)):
if len(words[i-1]) > len(words[i]) and \
words[i-1][:len(words[i])] == words[i]:
return ""
if (len(words[i-1]) > len(words[i]) and
words[i-1][:len(words[i])] == words[i]):
return ""
self.findEdges(words[i - 1], words[i], ancestors)

# Output topological order by DFS.
Expand All @@ -82,7 +91,7 @@ def alienOrder(self, words):
for node in nodes:
if self.topSortDFS(node, node, ancestors, visited, result):
return ""

return "".join(result)


Expand Down
3 changes: 3 additions & 0 deletions Python/arithmetic-slices-ii-subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
# [2,4,6,8,10]
# [2,6,10]

import collections


class Solution(object):
def numberOfArithmeticSlices(self, A):
"""
Expand Down
3 changes: 3 additions & 0 deletions Python/arranging-coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#
# Because the 4th row is incomplete, we return 3.

import math


class Solution(object):
def arrangeCoins(self, n):
"""
Expand Down
24 changes: 14 additions & 10 deletions Python/basic-calculator-iv.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# have a leading coefficient or unary operator like "2x" or "-x".
#
# Expressions are evaluated in the usual order:
# brackets first, then multiplication, then addition and subtraction.
# brackets first, then multiplication, then addition and subtraction.
# For example, expression = "1 + 2 * 3" has an answer of ["7"].
#
# The format of the output is as follows:
Expand All @@ -26,9 +26,9 @@
# counting multiplicity. (For example, "a*a*b*c" has degree 4.)
# We write the largest degree terms of our answer first,
# breaking ties by lexicographic order ignoring the leading coefficient of the term.
# - The leading coefficient of the term is placed directly to the left with an asterisk separating it
# - The leading coefficient of the term is placed directly to the left with an asterisk separating it
# from the variables (if they exist.) A leading coefficient of 1 is still printed.
# - An example of a well formatted answer is ["-2*a*a*a", "3*a*a*b", "3*b*b", "4*a", "5*c", "-6"]
# - An example of a well formatted answer is ["-2*a*a*a", "3*a*a*b", "3*b*b", "4*a", "5*c", "-6"]
# - Terms (including constant terms) with coefficient 0 are not included.
# For example, an expression of "0" has an output of [].
#
Expand All @@ -52,7 +52,7 @@
#
# Input: expression = "((a - b) * (b - c) + (c - a)) * ((a - b) + (b - c) * (c - a))",
# evalvars = [], evalints = []
# Output:
# Output:
# ["-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",
# "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",
# "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"]
Expand All @@ -61,6 +61,10 @@
# - expression will have length in range [1, 1000].
# - evalvars, evalints will have equal lengths in range [0, 1000].

import collections
import itertools


class Poly(collections.Counter):
def __init__(self, expr=None):
if expr is None:
Expand Down Expand Up @@ -94,9 +98,9 @@ def merge(k1, k2):
i += 1
else:
result.append(k2[j])
j += 1
j += 1
return result

result = Poly()
for k1, v1 in self.items():
for k2, v2 in other.items():
Expand All @@ -119,8 +123,8 @@ def to_list(self):
return ["*".join((str(v),) + k) \
for k, v in sorted(self.items(), key=lambda(k, _): (-len(k), k)) \
if v]


class Solution(object):
def basicCalculatorIV(self, expression, evalvars, evalints):
"""
Expand All @@ -138,7 +142,7 @@ def compute(operands, operators):
operands.append(left - right)
elif op == '*':
operands.append(left * right)

def parse(s):
if not s:
return Poly()
Expand All @@ -163,6 +167,6 @@ def parse(s):
while operators:
compute(operands, operators)
return operands[-1]

lookup = dict(itertools.izip(evalvars, evalints))
return parse(expression).eval(lookup).to_list()
3 changes: 3 additions & 0 deletions Python/binary-tree-vertical-order-traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# self.left = None
# self.right = None

import collections


# BFS + hash solution.
class Solution(object):
def verticalOrder(self, root):
Expand Down
3 changes: 3 additions & 0 deletions Python/bold-words-in-string.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Time: O(n * l), n is the length of S, l is the average length of words
# Space: O(t) , t is the size of trie

import collections


class Solution(object):
def boldWords(self, words, S):
"""
Expand Down
5 changes: 4 additions & 1 deletion Python/brick-wall.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# in which case the line will obviously cross no bricks.
#
# Example:
# Input:
# Input:
# [[1,2,2,1],
# [3,1,2],
# [1,3,2],
Expand All @@ -30,6 +30,9 @@
# The height of wall is in range [1,10,000].
# Total number of bricks of the wall won't exceed 20,000.

import collections


class Solution(object):
def leastBricks(self, wall):
"""
Expand Down
Loading

0 comments on commit 65b3ea4

Please sign in to comment.