Skip to content

Commit 5a2f816

Browse files
committed
refactor
1 parent fa1bec3 commit 5a2f816

14 files changed

+123
-68
lines changed

containermaxwater.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
"""Container with maximum water
2+
"""
3+
4+
15
def container_with_max_water(height):
26
ans, count = height[0], len(height)
37
for i in range(1, count - 2):
48
j = count - i
5-
w = j - i
6-
h = min(height[i], height[j])
7-
ans = max(w * h, ans)
9+
wide = j - i
10+
tall = min(height[i], height[j])
11+
ans = max(wide * tall, ans)
812
return ans
913

1014

evalrpn.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
def eval_rpn(tokens):
1+
"""Eval Reverse Polish Notation
2+
"""
3+
4+
5+
def eval_rpn(tokens: list[str]) -> int:
26
stack = []
3-
for i in range(len(tokens)):
4-
if tokens[i] not in "+-*/":
5-
stack.append(int(tokens[i]))
7+
for i, token in enumerate(tokens):
8+
if token not in "+-*/":
9+
stack.append(int(token))
610
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))
11+
right, left = stack.pop(), stack.pop()
12+
if token == "+":
13+
stack.append(left + right)
14+
elif token == "-":
15+
stack.append(left - right)
16+
elif token == "*":
17+
stack.append(left * right)
18+
elif token == "/":
19+
stack.append(int(left / right))
1620
return stack.pop()
1721

1822

groupanagrams.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
"""Group Anagrams together
2+
"""
3+
4+
15
def group_anagrams(strings):
26
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)
7+
for string in strings:
8+
sort = "".join(sorted(string))
9+
if sort not in groups:
10+
groups[sort] = []
11+
groups[sort].append(string)
812
return groups.values()
913

1014

isanagram.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
def is_anagram(s, t):
2-
if len(s) != len(t):
1+
"""Is one string anagram of another
2+
"""
3+
4+
5+
def is_anagram(string, target):
6+
if len(string) != len(target):
37
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:
8+
counts = {}
9+
for char in string:
10+
counts[char] = counts.get(char, 0) + 1
11+
for char in target:
12+
if char not in counts:
913
return False
10-
cc[c] -= 1
11-
if cc[c] == 0:
12-
del cc[c]
14+
counts[char] -= 1
15+
if counts[char] == 0:
16+
del counts[char]
1317
return True
1418

1519

longestconsecutiveseq.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
"""Longest consecutive sequence in a list
2+
"""
3+
4+
15
def longest_consecutive(nums):
26
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+
run, ans = 1, 1
8+
for i, num in enumerate(nums):
9+
if i < count - 1 and num + 1 == nums[i + 1]:
10+
run += 1
711
else:
8-
c, mx = 1, max(c, mx)
9-
return mx
12+
run, ans = 1, max(run, ans)
13+
return ans
1014

1115

1216
if __name__ == "__main__":

longestsubstrlen.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
def length_of_longest_substring(s):
1+
"""Length of longest non-repeating substring
2+
"""
3+
4+
5+
def length_of_longest_substring(string):
26
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,)
7+
while right < len(string):
8+
if not win.get(string[right]):
9+
win[string[right]] = (True,)
610
max_left = max(right - left + 1, max_left)
711
else:
8-
while s[left] != s[right]:
9-
win[s[left]], left = False, left + 1
12+
while string[left] != string[right]:
13+
win[string[left]], left = False, left + 1
1014
left += 1
1115
right += 1
1216
return max_left

productexceptself.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""Product of all elements in list except itself
2+
"""
3+
4+
15
def product_except_self(nums):
26
count = len(nums)
37
res = [1] * count

stockmaxprofit.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""Max profit of a single transaction in stock sales
2+
"""
3+
4+
15
def stock_max_profit(prices):
26
profit, min_price = 0, prices[0]
37
for i in range(1, len(prices) - 1):

substrpermute.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
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:
1+
"""Check if a string contains permutation of another string
2+
"""
3+
4+
5+
def check_str_permute_sub(string1, string2):
6+
s1count, s2count = {}, {}
7+
for char in string1:
8+
s1count[char] = s1count.get(char, 0) + 1
9+
for char in string2:
10+
if char in s1count:
11+
s2count[char] = s2count.get(char, 0) + 1
12+
if s1count == s2count:
913
return True
1014
else:
11-
s2c = {}
15+
s2count = {}
1216
return False
1317

1418

threesumzero.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
"""Three unique items in a list that sum to zero
2+
"""
3+
4+
15
def three_sum_zero(nums: list[int]):
26
res = set() # a+b+c = 0 => c = -a-b
37
count = len(nums)
4-
for i, a in enumerate(nums):
8+
for i in range(count):
59
for j in range(i + 1, count):
6-
b = nums[j]
710
try:
8-
k = nums.index(-a - b, j + 1)
9-
pair = sorted([a, b, nums[k]])
11+
k = nums.index(-nums[i] - nums[j], j + 1)
12+
pair = sorted([nums[i], nums[j], nums[k]])
1013
res.add(tuple(pair))
11-
finally:
14+
except ValueError:
1215
pass
1316
return list(res)
1417

0 commit comments

Comments
 (0)