Skip to content

Commit 14ef902

Browse files
committed
style: refactor to func
1 parent a13803f commit 14ef902

14 files changed

+166
-193
lines changed

containermaxwater.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
class Solution:
2-
def containerWithMaxWater(self, height):
3-
mx, l = height[0], len(height)
4-
for i in range(1, l - 2):
5-
j = l - i
6-
w = j - i
7-
h = min(height[i], height[j])
8-
mx = max(w * h, mx)
9-
return mx
1+
def container_with_max_water(height):
2+
ans, count = height[0], len(height)
3+
for i in range(1, count - 2):
4+
j = count - i
5+
w = j - i
6+
h = min(height[i], height[j])
7+
ans = max(w * h, ans)
8+
return ans
109

1110

1211
if __name__ == "__main__":
13-
s = Solution()
14-
print(s.containerWithMaxWater([1, 8, 6, 2, 5, 4, 8, 3, 7]))
12+
print(container_with_max_water([1, 8, 6, 2, 5, 4, 8, 3, 7]))
1513
# => 49

evalrpn.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
class Solution(object):
2-
def evalRPN(self, tokens):
3-
stak = []
4-
for i in range(len(tokens)):
5-
if not tokens[i] in "+-*/":
6-
stak.append(int(tokens[i]))
7-
continue
8-
a, b = stak.pop(), stak.pop()
9-
if tokens[i] == "+":
10-
stak.append(b + a)
11-
elif tokens[i] == "-":
12-
stak.append(b - a)
13-
elif tokens[i] == "*":
14-
stak.append(b * a)
15-
elif tokens[i] == "/":
16-
stak.append(int(b / a))
17-
return stak.pop()
1+
def eval_rpn(tokens):
2+
stack = []
3+
for i in range(len(tokens)):
4+
if tokens[i] not in "+-*/":
5+
stack.append(int(tokens[i]))
6+
continue
7+
a, b = stack.pop(), stack.pop()
8+
if tokens[i] == "+":
9+
stack.append(b + a)
10+
elif tokens[i] == "-":
11+
stack.append(b - a)
12+
elif tokens[i] == "*":
13+
stack.append(b * a)
14+
elif tokens[i] == "/":
15+
stack.append(int(b / a))
16+
return stack.pop()
1817

1918

2019
if __name__ == "__main__":
21-
s = Solution()
22-
print(s.evalRPN(["2", "1", "+", "3", "*"]))
20+
print(eval_rpn(["2", "1", "+", "3", "*"]))
2321
# => 9 => (2+1)*3
2422
print(
25-
s.evalRPN(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"])
23+
eval_rpn(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"])
2624
)
2725
# => 22
28-
print(s.evalRPN(["4", "-2", "/", "2", "-3", "-", "-"]))
26+
print(eval_rpn(["4", "-2", "/", "2", "-3", "-", "-"]))
2927
# => -7

groupanagrams.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
class Solution:
2-
def groupAnagrams(self, strs):
3-
groups = {}
4-
for s in strs:
5-
s1 = "".join(sorted(s))
6-
if not s1 in groups:
7-
groups[s1] = []
8-
groups[s1].append(s)
9-
return groups.values()
1+
def group_anagrams(strings):
2+
groups = {}
3+
for s in strings:
4+
s1 = "".join(sorted(s))
5+
if s1 not in groups:
6+
groups[s1] = []
7+
groups[s1].append(s)
8+
return groups.values()
109

1110

1211
if __name__ == "__main__":
13-
s = Solution()
14-
print(s.groupAnagrams(["abc", "bac", "def", "fed", "efd", "ghi"]))
12+
print(group_anagrams(["abc", "bac", "def", "fed", "efd", "ghi"]))
1513
# => [['abc', 'bac'], ['def', 'fed', 'efd'], ['ghi']]

isanagram.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
class Solution:
2-
def isAnagram(self, s, t):
3-
if len(s) != len(t):
1+
def is_anagram(s, t):
2+
if len(s) != len(t):
3+
return False
4+
cc = {}
5+
for c in s:
6+
cc[c] = cc[c] + 1 if c in cc else 1
7+
for c in t:
8+
if c not in cc:
49
return False
5-
cc = {}
6-
for c in s:
7-
cc[c] = cc[c] + 1 if c in cc else 1
8-
for c in t:
9-
if c not in cc:
10-
return False
11-
cc[c] -= 1
12-
if cc[c] == 0:
13-
del cc[c]
14-
return True
10+
cc[c] -= 1
11+
if cc[c] == 0:
12+
del cc[c]
13+
return True
1514

16-
# one line solution:
17-
def isAnagram2(self, s, t):
18-
return len(s) == len(t) and sorted(s) == sorted(t)
15+
16+
# one line solution:
17+
def is_anagram2(s, t):
18+
return len(s) == len(t) and sorted(s) == sorted(t)
1919

2020

2121
if __name__ == "__main__":
22-
s = Solution()
23-
print(s.isAnagram("anagram", "nagaram"))
22+
print(is_anagram("anagram", "nagaram"))
2423
# => True

longestconsecutiveseq.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
class Solution:
2-
def longestConsecutive(self, nums):
3-
l, nums = len(nums), sorted(nums)
4-
c, mx = 1, 1
5-
for i, n in enumerate(nums):
6-
if i < l - 1 and n + 1 == nums[i + 1]:
7-
c += 1
8-
else:
9-
c, mx = 1, max(c, mx)
10-
return mx
1+
def longest_consecutive(nums):
2+
count, nums = len(nums), sorted(nums)
3+
c, mx = 1, 1
4+
for i, n in enumerate(nums):
5+
if i < count - 1 and n + 1 == nums[i + 1]:
6+
c += 1
7+
else:
8+
c, mx = 1, max(c, mx)
9+
return mx
1110

1211

1312
if __name__ == "__main__":
14-
s = Solution()
15-
print(s.longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]))
13+
print(longest_consecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]))
1614
# => 9 => [0-8]

longestsubstrlen.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
class Solution:
2-
def lengthOfLongestSubstring(self, s):
3-
left, right, maxl, win = 0, 0, 0, {}
4-
while right < len(s):
5-
if not win.get(s[right]):
6-
win[s[right]] = (True,)
7-
maxl = max(right - left + 1, maxl)
8-
else:
9-
while s[left] != s[right]:
10-
win[s[left]], left = False, left + 1
11-
left += 1
12-
right += 1
13-
return maxl
1+
def length_of_longest_substring(s):
2+
left, right, max_left, win = 0, 0, 0, {}
3+
while right < len(s):
4+
if not win.get(s[right]):
5+
win[s[right]] = (True,)
6+
max_left = max(right - left + 1, max_left)
7+
else:
8+
while s[left] != s[right]:
9+
win[s[left]], left = False, left + 1
10+
left += 1
11+
right += 1
12+
return max_left
1413

1514

1615
if __name__ == "__main__":
17-
s = Solution()
18-
print(s.lengthOfLongestSubstring("abcdabecbb"))
16+
print(length_of_longest_substring("abcdabecbb"))
1917
# => 5 => cdabe

productexceptself.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
class Solution:
2-
def productExceptSelf(self, nums):
3-
l = len(nums)
4-
res = [1] * l
5-
pre, post, zero = 1, 1, 0
6-
for i in range(l):
7-
if nums[i] == 0:
8-
zero += 1
9-
if zero == 2:
10-
return [0] * l
11-
res[i], pre = pre, pre * nums[i]
12-
for i in range(l - 1, -1, -1):
13-
res[i], post = res[i] * post, post * nums[i]
14-
return res
1+
def product_except_self(nums):
2+
count = len(nums)
3+
res = [1] * count
4+
pre, post, zero = 1, 1, 0
5+
for i in range(count):
6+
if nums[i] == 0:
7+
zero += 1
8+
if zero == 2:
9+
return [0] * count
10+
res[i], pre = pre, pre * nums[i]
11+
for i in range(count - 1, -1, -1):
12+
res[i], post = res[i] * post, post * nums[i]
13+
return res
1514

1615

1716
if __name__ == "__main__":
18-
s = Solution()
19-
print(s.productExceptSelf([1, 2, 3]))
17+
print(product_except_self([1, 2, 3]))
2018
# => [6, 3, 2]
21-
print(s.productExceptSelf([1, 2, 3, 0, 0]))
19+
print(product_except_self([1, 2, 3, 0, 0]))
2220
# => [0, 0, 0, 0, 0]

stockmaxprofit.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
class Solution:
2-
def stockMaxProfit(self, prices):
3-
l, profit, minp = len(prices), 0, prices[0]
4-
for i in range(1, l - 1):
5-
if prices[i] < minp:
6-
minp = prices[i]
7-
else:
8-
profit = max(prices[i] - minp, profit)
9-
return profit
1+
def stock_max_profit(prices):
2+
profit, min_price = 0, prices[0]
3+
for i in range(1, len(prices) - 1):
4+
if prices[i] < min_price:
5+
min_price = prices[i]
6+
else:
7+
profit = max(prices[i] - min_price, profit)
8+
return profit
109

1110

1211
if __name__ == "__main__":
13-
s = Solution()
14-
print(s.stockMaxProfit([7, 1, 5, 3, 6, 4]))
12+
print(stock_max_profit([7, 1, 5, 3, 6, 4]))
1513
# => 5 (1->6)

substrpermute.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
class Solution:
2-
def checkStrPermuteSub(self, s1, s2):
3-
s1c, s2c = {}, {}
4-
for c in s1:
5-
s1c[c] = s1c.get(c, 0) + 1
6-
for c in s2:
7-
if c in s1c:
8-
s2c[c] = s2c.get(c, 0) + 1
9-
if s1c == s2c:
10-
return True
11-
else:
12-
s2c = {}
13-
return False
1+
def check_str_permute_sub(s1, s2):
2+
s1c, s2c = {}, {}
3+
for c in s1:
4+
s1c[c] = s1c.get(c, 0) + 1
5+
for c in s2:
6+
if c in s1c:
7+
s2c[c] = s2c.get(c, 0) + 1
8+
if s1c == s2c:
9+
return True
10+
else:
11+
s2c = {}
12+
return False
1413

1514

1615
if __name__ == "__main__":
17-
s = Solution()
18-
print(s.checkStrPermuteSub("abcd", "zdcbax"))
16+
print(check_str_permute_sub("abcd", "zdcbax"))
1917
# => True

threesumzero.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
class Solution:
2-
def threeSumZero(self, nums: list[int]):
3-
res = set() # a+b+c = 0 => c = -a-b
4-
l = len(nums)
5-
for i, a in enumerate(nums):
6-
for j in range(i + 1, l):
7-
b = nums[j]
8-
try:
9-
k = nums.index(-a - b, j + 1)
10-
ll = sorted([a, b, nums[k]])
11-
res.add(tuple(ll))
12-
except:
13-
pass
14-
return list(res)
1+
def three_sum_zero(nums: list[int]):
2+
res = set() # a+b+c = 0 => c = -a-b
3+
count = len(nums)
4+
for i, a in enumerate(nums):
5+
for j in range(i + 1, count):
6+
b = nums[j]
7+
try:
8+
k = nums.index(-a - b, j + 1)
9+
pair = sorted([a, b, nums[k]])
10+
res.add(tuple(pair))
11+
finally:
12+
pass
13+
return list(res)
1514

1615

1716
if __name__ == "__main__":
18-
s = Solution()
19-
print(s.threeSumZero([0, 0, 0, -1, 1]))
17+
print(three_sum_zero([0, 0, 0, -1, 1]))
2018
# => [(0, 0, 0), (-1, 0, 1)]

0 commit comments

Comments
 (0)