From 5487d5452810030a7f7b700d0d992b185c3834a7 Mon Sep 17 00:00:00 2001 From: Allen Liu Date: Tue, 3 Apr 2018 00:27:19 +0800 Subject: [PATCH] Fix flake8 related issues --- Python/01-matrix.py | 5 +++++ Python/1-bit-and-2-bit-characters.py | 20 ++++++++++++++------ Python/132-pattern.py | 6 ++++++ Python/2-keys-keyboard.py | 7 +++++-- Python/24-game.py | 26 +++++++++++++++++--------- Python/3sum-closest.py | 12 +++++++----- Python/3sum-smaller.py | 1 + Python/3sum.py | 1 + Python/4-keys-keyboard.py | 20 ++++++++++++++------ Python/4sum-ii.py | 6 ++++-- Python/4sum.py | 15 +++++++++++---- 11 files changed, 85 insertions(+), 34 deletions(-) diff --git a/Python/01-matrix.py b/Python/01-matrix.py index 46351da5a..479a22a42 100644 --- a/Python/01-matrix.py +++ b/Python/01-matrix.py @@ -35,6 +35,11 @@ import collections +try: + xrange # Python 2 +except NameError: + xrange = range # Python 3 + class Solution(object): def updateMatrix(self, matrix): diff --git a/Python/1-bit-and-2-bit-characters.py b/Python/1-bit-and-2-bit-characters.py index a92f26b67..4aa5410d5 100644 --- a/Python/1-bit-and-2-bit-characters.py +++ b/Python/1-bit-and-2-bit-characters.py @@ -1,25 +1,27 @@ # Time: O(n) # Space: O(1) -# We have two special characters. The first character can be represented by one bit 0. +# We have two special characters. The first character can be represented +# by one bit 0. # The second character can be represented by two bits (10 or 11). # -# Now given a string represented by several bits. Return whether the last character must +# Now given a string represented by several bits. +# Return whether the last character must # be a one-bit character or not. The given string will always end with a zero. # # Example 1: -# Input: +# Input: # bits = [1, 0, 0] # Output: True -# Explanation: +# Explanation: # The only way to decode it is two-bit character and one-bit character. # So the last character is one-bit character. # # Example 2: -# Input: +# Input: # bits = [1, 1, 1, 0] # Output: False -# Explanation: +# Explanation: # The only way to decode it is two-bit character and two-bit character. # So the last character is NOT one-bit character. # Note: @@ -27,6 +29,12 @@ # 1 <= len(bits) <= 1000. # bits[i] is always 0 or 1. +try: + xrange # Python 2 +except NameError: + xrange = range # Python 3 + + class Solution(object): def isOneBitCharacter(self, bits): """ diff --git a/Python/132-pattern.py b/Python/132-pattern.py index 0ad03e480..15e0aecbc 100644 --- a/Python/132-pattern.py +++ b/Python/132-pattern.py @@ -27,6 +27,12 @@ # # Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0]. +try: + xrange # Python 2 +except NameError: + xrange = range # Python 3 + + class Solution(object): def find132pattern(self, nums): """ diff --git a/Python/2-keys-keyboard.py b/Python/2-keys-keyboard.py index 24282da4d..220adbe6a 100644 --- a/Python/2-keys-keyboard.py +++ b/Python/2-keys-keyboard.py @@ -4,10 +4,12 @@ # Initially on a notepad only one character 'A' is present. # You can perform two operations on this notepad for each step: # -# Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). +# Copy All: You can copy all the characters present on the notepad +# (partial copy is not allowed). # Paste: You can paste the characters which are copied last time. # Given a number n. -# You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. +# You have to get exactly n 'A' on the notepad by performing the minimum +# number of steps permitted. # Output the minimum number of steps to get n 'A'. # # Example 1: @@ -21,6 +23,7 @@ # Note: # The n will be in the range [1, 1000]. + class Solution(object): def minSteps(self, n): """ diff --git a/Python/24-game.py b/Python/24-game.py index bd91f330a..995ada2f7 100644 --- a/Python/24-game.py +++ b/Python/24-game.py @@ -14,11 +14,21 @@ # Note: # The division operator / represents real division, not integer division. # For example, 4 / (1 - 2/3) = 12. -# Every operation done is between two numbers. In particular, we cannot use - as a unary operator. -# For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed. -# You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12. +# Every operation done is between two numbers. In particular, +# we cannot use - as a unary operator. +# For example, with [1, 1, 1, 1] as input, +# the expression -1 - 1 - 1 - 1 is not allowed. +# You cannot concatenate numbers together. For example, +# if the input is [1, 2, 1, 2], we cannot write this as 12 + 12. from operator import add, sub, mul, truediv +from fractions import Fraction + +try: + xrange # Python 2 +except NameError: + xrange = range # Python 3 + class Solution(object): def judgePoint24(self, nums): @@ -47,9 +57,6 @@ def judgePoint24(self, nums): # Time: O(n^3 * 4^n) = O(1), n = 4 # Space: O(n^2) = O(1) -from fractions import Fraction -from operator import add, sub, mul, div - class Solution2(object): def judgePoint24(self, nums): """ @@ -59,15 +66,16 @@ def judgePoint24(self, nums): def dfs(nums): if len(nums) == 1: return nums[0] == 24 - ops = [add, sub, mul, div] + ops = [add, sub, mul, truediv] for i in xrange(len(nums)): for j in xrange(len(nums)): if i == j: continue - next_nums = [nums[k] for k in xrange(len(nums)) if i != k != j] + next_nums = [nums[k] for k in xrange(len(nums)) + if i != k != j] for op in ops: if ((op is add or op is mul) and j > i) or \ - (op == div and nums[j] == 0): + (op == truediv and nums[j] == 0): continue next_nums.append(op(nums[i], nums[j])) if dfs(next_nums): diff --git a/Python/3sum-closest.py b/Python/3sum-closest.py index 742e1fa57..037308299 100644 --- a/Python/3sum-closest.py +++ b/Python/3sum-closest.py @@ -1,9 +1,10 @@ # Time: O(n^2) # Space: O(1) -# -# Given an array S of n integers, -# find three integers in S such that the sum is closest to a given number, target. -# Return the sum of the three integers. + +# Given an array S of n integers, +# find three integers in S such that the sum is closest to a given number, +# target. +# Return the sum of the three integers. # You may assume that each input would have exactly one solution. # # For example, given array S = {-1 2 1 -4}, and target = 1. @@ -36,6 +37,7 @@ def threeSumClosest(self, nums, target): i += 1 return result + if __name__ == '__main__': result = Solution().threeSumClosest([-1, 2, 1, -4], 1) - print result + print(result) diff --git a/Python/3sum-smaller.py b/Python/3sum-smaller.py index bcc9b533b..010ce9788 100644 --- a/Python/3sum-smaller.py +++ b/Python/3sum-smaller.py @@ -1,6 +1,7 @@ # Time: O(n^2) # Space: O(1) + class Solution: # @param {integer[]} nums # @param {integer} target diff --git a/Python/3sum.py b/Python/3sum.py index 66958d1c6..9418463fd 100644 --- a/Python/3sum.py +++ b/Python/3sum.py @@ -13,6 +13,7 @@ # A solution set is: # (-1, 0, 1) # (-1, -1, 2) + import collections diff --git a/Python/4-keys-keyboard.py b/Python/4-keys-keyboard.py index 38d212b56..45edbdf56 100644 --- a/Python/4-keys-keyboard.py +++ b/Python/4-keys-keyboard.py @@ -1,20 +1,28 @@ # Time: O(1) # Space: O(1) +try: + xrange # Python 2 +except NameError: + xrange = range # Python 3 + + class Solution(object): def maxA(self, N): """ :type N: int :rtype: int """ - if N < 7: return N - if N == 10: return 20 # the following rule doesn't hold when N = 10 + if N < 7: + return N + if N == 10: + return 20 # the following rule doesn't hold when N = 10 - n = N // 5 + 1 # n3 + n4 increases one every 5 keys + n = N // 5 + 1 # n3 + n4 increases one every 5 keys # (1) n = n3 + n4 # (2) N + 1 = 4 * n3 + 5 * n4 # 5 x (1) - (2) => 5*n - N - 1 = n3 - n3 = 5*n - N - 1 + n3 = 5*n - N - 1 n4 = n - n3 return 3**n3 * 4**n4 @@ -27,9 +35,9 @@ def maxA(self, N): :type N: int :rtype: int """ - if N < 7: return N + if N < 7: + return N dp = range(N+1) for i in xrange(7, N+1): dp[i % 6] = max(dp[(i-4) % 6]*3, dp[(i-5) % 6]*4) return dp[N % 6] - diff --git a/Python/4sum-ii.py b/Python/4sum-ii.py index a11bede5a..7ca13bab2 100644 --- a/Python/4sum-ii.py +++ b/Python/4sum-ii.py @@ -5,8 +5,10 @@ # compute how many tuples (i, j, k, l) there are # such that A[i] + B[j] + C[k] + D[l] is zero. # -# To make problem a bit easier, all A, B, C, D have same length of N where 0 <= N <= 500. -# All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. +# To make problem a bit easier, all A, B, C, D have same length of N +# where 0 <= N <= 500. +# All integers are in the range of -228 to 228 - 1 and the result +# is guaranteed to be at most 231 - 1. # # Example: # diff --git a/Python/4sum.py b/Python/4sum.py index 6f137b1ef..d2695e838 100644 --- a/Python/4sum.py +++ b/Python/4sum.py @@ -6,7 +6,8 @@ # Find all unique quadruplets in the array which gives the sum of target. # # Note: -# Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a <= b <= c <= d) +# Elements in a quadruplet (a,b,c,d) must be in non-descending order. +# (ie, a <= b <= c <= d) # The solution set must not contain duplicate quadruplets. # For example, given array S = {1 0 -1 0 -2 2}, and target = 0. # @@ -18,6 +19,11 @@ import collections +try: + xrange # Python 2 +except NameError: + xrange = range # Python 3 + # Two pointer solution. (1356ms) class Solution(object): @@ -104,9 +110,10 @@ def fourSum(self, nums, target): for i in lookup.keys(): if target - i in lookup: for x in lookup[i]: - for y in lookup[target -i]: + for y in lookup[target - i]: [a, b], [c, d] = x, y - if a is not c and a is not d and b is not c and b is not d: + if a is not c and a is not d and \ + b is not c and b is not d: quad = sorted([nums[a], nums[b], nums[c], nums[d]]) if quad not in result: result.append(quad) @@ -115,4 +122,4 @@ def fourSum(self, nums, target): if __name__ == '__main__': result = Solution().fourSum([1, 0, -1, 0, -2, 2], 0) - print result + print(result)