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 2, 2018
1 parent 62b6b8f commit 5487d54
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 34 deletions.
5 changes: 5 additions & 0 deletions Python/01-matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@

import collections

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


class Solution(object):
def updateMatrix(self, matrix):
Expand Down
20 changes: 14 additions & 6 deletions Python/1-bit-and-2-bit-characters.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
# 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:
#
# 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):
"""
Expand Down
6 changes: 6 additions & 0 deletions Python/132-pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
7 changes: 5 additions & 2 deletions Python/2-keys-keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -21,6 +23,7 @@
# Note:
# The n will be in the range [1, 1000].


class Solution(object):
def minSteps(self, n):
"""
Expand Down
26 changes: 17 additions & 9 deletions Python/24-game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
Expand Down
12 changes: 7 additions & 5 deletions Python/3sum-closest.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
1 change: 1 addition & 0 deletions Python/3sum-smaller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Time: O(n^2)
# Space: O(1)


class Solution:
# @param {integer[]} nums
# @param {integer} target
Expand Down
1 change: 1 addition & 0 deletions Python/3sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# A solution set is:
# (-1, 0, 1)
# (-1, -1, 2)

import collections


Expand Down
20 changes: 14 additions & 6 deletions Python/4-keys-keyboard.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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]

6 changes: 4 additions & 2 deletions Python/4sum-ii.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand Down
15 changes: 11 additions & 4 deletions Python/4sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -18,6 +19,11 @@

import collections

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


# Two pointer solution. (1356ms)
class Solution(object):
Expand Down Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 5487d54

Please sign in to comment.