Skip to content

Commit

Permalink
Fix flake8 related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Apr 21, 2018
1 parent 3b8f14f commit 49ee3ca
Show file tree
Hide file tree
Showing 431 changed files with 1,782 additions and 1,781 deletions.
41 changes: 18 additions & 23 deletions Python/balanced-binary-tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,32 @@
#
# Given a binary tree, determine if it is height-balanced.
#
# For this problem, a height-balanced binary tree is defined as a binary tree
# in which the depth of the two subtrees of every node never differ by more than 1.
# For this problem, a height-balanced binary tree is defined as a binary
# tree
# in which the depth of the two subtrees of every node never differ by more
# than 1.
#


# Definition for a binary tree node
class TreeNode:
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution:

class Solution(object):
# @param root, a tree node
# @return a boolean
def isBalanced(self, root):
return (self.getHeight(root) >= 0)

def getHeight(self, root):
if root is None:
return 0
left_height, right_height = self.getHeight(root.left), self.getHeight(root.right)
if left_height < 0 or right_height < 0 or abs(left_height - right_height) > 1:
return -1
return max(left_height, right_height) + 1

if __name__ == "__main__":
root = TreeNode(0)
root.left = TreeNode(1)
result = Solution().isBalanced(root)
print result

root.left.left = TreeNode(2)
result = Solution().isBalanced(root)
print result
def getHeight(root):
if root is None:
return 0
left_height, right_height = \
getHeight(root.left), getHeight(root.right)
if left_height < 0 or right_height < 0 or \
abs(left_height - right_height) > 1:
return -1
return max(left_height, right_height) + 1
return (getHeight(root) >= 0)
10 changes: 7 additions & 3 deletions Python/base-7.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
# Output: "-10"
# Note: The input will be in range of [-1e7, 1e7].


class Solution(object):
def convertToBase7(self, num):
if num < 0: return '-' + self.convertToBase7(-num)
if num < 0:
return '-' + self.convertToBase7(-num)
result = ''
while num:
result = str(num % 7) + result
Expand All @@ -27,6 +29,8 @@ def convertToBase7(self, num):
:type num: int
:rtype: str
"""
if num < 0: return '-' + self.convertToBase7(-num)
if num < 7: return str(num)
if num < 0:
return '-' + self.convertToBase7(-num)
if num < 7:
return str(num)
return self.convertToBase7(num // 7) + str(num % 7)
42 changes: 25 additions & 17 deletions Python/baseball-game.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,54 @@
# You're now a baseball game point recorder.
# Given a list of strings, each string can be one of the 4 following types:
#
# 1. Integer (one round's score): Directly represents the number of points you get in this round.
# 2. "+" (one round's score): Represents that the points you get in this round are
# the sum of the last two valid round's points.
# 3. "D" (one round's score): Represents that the points you get in this round are
# the doubled data of the last valid round's points.
# 4. "C" (an operation, which isn't a round's score): Represents the last valid round's points
# you get were invalid and should be removed.
#
# Each round's operation is permanent and could have an impact on the round before and the round after.
# 1. Integer (one round's score): Directly represents the number of points
# you get in this round.
# 2. "+" (one round's score): Represents that the points you get in
# this round are the sum of the last two valid
# round's points.
# 3. "D" (one round's score): Represents that the points you get in this round
# are the doubled data of the last valid round's
# points.
# 4. "C" (an operation, which isn't a round's score): Represents the last
# valid round's points you get were invalid and
# should be removed.
#
# Each round's operation is permanent and could have an impact on the round
# before and the round after.
# You need to return the sum of the points you could get in all the rounds.
#
# Example 1:
#
# Input: ["5","2","C","D","+"]
# Output: 30
# Explanation:
# Explanation:
# Round 1: You could get 5 points. The sum is: 5.
# Round 2: You could get 2 points. The sum is: 7.
# Operation 1: The round 2's data was invalid. The sum is: 5.
# Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
# Operation 1: The round 2's data was invalid. The sum is: 5.
# Round 3: You could get 10 points (the round 2's data has been removed).
# The sum is: 15.
# Round 4: You could get 5 + 10 = 15 points. The sum is: 30.
#
#
# Example 2:
#
# Input: ["5","-2","4","C","D","9","+","+"]
# Output: 27
# Explanation:
# Explanation:
# Round 1: You could get 5 points. The sum is: 5.
# Round 2: You could get -2 points. The sum is: 3.
# Round 3: You could get 4 points. The sum is: 7.
# Operation 1: The round 3's data is invalid. The sum is: 3.
# Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
# Operation 1: The round 3's data is invalid. The sum is: 3.
# Round 4: You could get -4 points (the round 3's data has been removed).
# The sum is: -1.
# Round 5: You could get 9 points. The sum is: 8.
# Round 6: You could get -4 + 9 = 5 points. The sum is 13.
# Round 7: You could get 9 + 5 = 14 points. The sum is 27.
#
# Note:
# The size of the input list will be between 1 and 1000.
# Every integer represented in the list will be between -30000 and 30000.



class Solution(object):
def calPoints(self, ops):
"""
Expand Down
6 changes: 3 additions & 3 deletions Python/basic-calculator-ii.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Implement a basic calculator to evaluate a simple expression string.
#
# The expression string contains only non-negative integers, +, -, *, /
# The expression string contains only non-negative integers, +, -, *, /
# operators and empty spaces . The integer division should truncate toward zero.
#
# You may assume that the given expression is always valid.
Expand Down Expand Up @@ -38,10 +38,10 @@ def calculate(self, s):
while operators[-1] != ')':
self.compute(operands, operators)
operators.pop()

while operators:
self.compute(operands, operators)

return operands[-1]

def compute(self, operands, operators):
Expand Down
6 changes: 3 additions & 3 deletions Python/basic-calculator-iii.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def calculate(self, s):
while operators[-1] != ')':
self.compute(operands, operators)
operators.pop()

while operators:
self.compute(operands, operators)

return operands[-1]

def compute(self, operands, operators):
Expand All @@ -63,4 +63,4 @@ def compute(self, operands, operators):
operands.append(left * right)
elif op == '/':
operands.append(left / right)

4 changes: 2 additions & 2 deletions Python/basic-calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def calculate(self, s):
while operators[-1] != ')':
self.compute(operands, operators)
operators.pop()

while operators:
self.compute(operands, operators)

return operands[-1]

def compute(self, operands, operators):
Expand Down
2 changes: 1 addition & 1 deletion Python/battleships-in-a-board.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Battleships can only be placed horizontally or vertically. In other words,
# they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column),
# where N can be of any size.
# At least one horizontal or vertical cell separates between two battleships -
# At least one horizontal or vertical cell separates between two battleships -
# there are no adjacent battleships.
#
# Example:
Expand Down
2 changes: 1 addition & 1 deletion Python/beautiful-arrangement-ii.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Given two integers n and k,
# you need to construct a list which contains n different positive integers ranging
# from 1 to n and obeys the following requirement:
# from 1 to n and obeys the following requirement:
# Suppose this list is [a1, a2, a3, ... , an],
# then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers.
#
Expand Down
4 changes: 2 additions & 2 deletions Python/beautiful-arrangement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Space: O(n)

# Suppose you have N integers from 1 to N.
# We define a beautiful arrangement as an array that is constructed by these N numbers successfully
# We define a beautiful arrangement as an array that is constructed by these N numbers successfully
# if one of the following is true for the ith position (1 <= i <= N) in this array:
#
# The number at the ith position is divisible by i.
Expand All @@ -12,7 +12,7 @@
# Example 1:
# Input: 2
# Output: 2
# Explanation:
# Explanation:
#
# The first beautiful arrangement is [1, 2]:
#
Expand Down
8 changes: 4 additions & 4 deletions Python/best-meeting-point.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def minTotalDistance(self, grid):
y = [j for row in grid for j, v in enumerate(row) if v == 1]
mid_x = self.findKthLargest(x, len(x) / 2 + 1)
mid_y = self.findKthLargest(y, len(y) / 2 + 1)

return sum([abs(mid_x-i) + abs(mid_y-j) \
for i, row in enumerate(grid) for j, v in enumerate(row) if v == 1])

def findKthLargest(self, nums, k):
left, right = 0, len(nums) - 1
while left <= right:
Expand All @@ -28,7 +28,7 @@ def findKthLargest(self, nums, k):
right = new_pivot_idx - 1
else: # new_pivot_idx < k - 1.
left = new_pivot_idx + 1

def PartitionAroundPivot(self, left, right, pivot_idx, nums):
pivot_value = nums[pivot_idx]
new_pivot_idx = left
Expand All @@ -37,6 +37,6 @@ def PartitionAroundPivot(self, left, right, pivot_idx, nums):
if nums[i] > pivot_value:
nums[i], nums[new_pivot_idx] = nums[new_pivot_idx], nums[i]
new_pivot_idx += 1

nums[right], nums[new_pivot_idx] = nums[new_pivot_idx], nums[right]
return new_pivot_idx
12 changes: 6 additions & 6 deletions Python/best-time-to-buy-and-sell-stock-ii.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Time: O(n)
# Space: O(1)
#
# Say you have an array for which the ith element is
# Say you have an array for which the ith element is
# the price of a given stock on day i.
#
# Design an algorithm to find the maximum profit.
# You may complete as many transactions as you like
# (ie, buy one and sell one share of the stock multiple times).
# However, you may not engage in multiple transactions at the same time
# Design an algorithm to find the maximum profit.
# You may complete as many transactions as you like
# (ie, buy one and sell one share of the stock multiple times).
# However, you may not engage in multiple transactions at the same time
# (ie, you must sell the stock before you buy again).


Expand All @@ -17,7 +17,7 @@ class Solution:
def maxProfit(self, prices):
profit = 0
for i in xrange(len(prices) - 1):
profit += max(0, prices[i + 1] - prices[i])
profit += max(0, prices[i + 1] - prices[i])
return profit

def maxProfit2(self, prices):
Expand Down
20 changes: 10 additions & 10 deletions Python/best-time-to-buy-and-sell-stock-iii.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Time: O(n)
# Space: O(1)
#
# Say you have an array for which the ith element
# Say you have an array for which the ith element
# is the price of a given stock on day i.
#
# Design an algorithm to find the maximum profit.
# Design an algorithm to find the maximum profit.
# You may complete at most two transactions.
#
# Note:
# You may not engage in multiple transactions at the same time
# You may not engage in multiple transactions at the same time
# (ie, you must sell the stock before you buy again).
#

Expand All @@ -26,15 +26,15 @@ def maxProfit(self, prices):
release1 = max(release1, hold1 + i)
hold1 = max(hold1, -i);
return release2

# Time: O(k * n)
# Space: O(k)
class Solution2:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
return self.maxAtMostKPairsProfit(prices, 2)

def maxAtMostKPairsProfit(self, prices, k):
max_buy = [float("-inf") for _ in xrange(k + 1)]
max_sell = [0 for _ in xrange(k + 1)]
Expand All @@ -47,7 +47,7 @@ def maxAtMostKPairsProfit(self, prices, k):
return max_sell[k]

# Time: O(n)
# Space: O(n)
# Space: O(n)
class Solution3:
# @param prices, a list of integer
# @return an integer
Expand All @@ -57,19 +57,19 @@ def maxProfit(self, prices):
min_price = min(min_price, price)
max_profit_from_left = max(max_profit_from_left, price - min_price)
max_profits_from_left.append(max_profit_from_left)

max_price, max_profit_from_right, max_profits_from_right = 0, 0, []
for i in reversed(range(len(prices))):
max_price = max(max_price, prices[i])
max_profit_from_right = max(max_profit_from_right, max_price - prices[i])
max_profits_from_right.insert(0, max_profit_from_right)

max_profit = 0
for i in range(len(prices)):
max_profit = max(max_profit, max_profits_from_left[i] + max_profits_from_right[i])

return max_profit

if __name__ == "__main__":
result = Solution().maxProfit([3, 2, 1, 4, 2, 5, 6])
print result
8 changes: 4 additions & 4 deletions Python/best-time-to-buy-and-sell-stock-iv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# Space: O(k)
#
# Say you have an array for which the ith element is the price of a given stock on day i.
#
#
# Design an algorithm to find the maximum profit. You may complete at most k transactions.
#
#
# Note:
# You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
#

class Solution:
# @return an integer as the maximum profit
# @return an integer as the maximum profit
def maxProfit(self, k, prices):
if k >= len(prices) / 2:
return self.maxAtMostNPairsProfit(prices)
Expand All @@ -20,7 +20,7 @@ def maxProfit(self, k, prices):
def maxAtMostNPairsProfit(self, prices):
profit = 0
for i in xrange(len(prices) - 1):
profit += max(0, prices[i + 1] - prices[i])
profit += max(0, prices[i + 1] - prices[i])
return profit

def maxAtMostKPairsProfit(self, prices, k):
Expand Down
Loading

0 comments on commit 49ee3ca

Please sign in to comment.