-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored main branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to GitHub API limits, only the first 60 comments can be shown.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.twoSum
:
cache = [[False] * (len(p) + 1) for i in range(len(s) + 1)] | ||
cache = [[False] * (len(p) + 1) for _ in range(len(s) + 1)] | ||
cache[len(s)][len(p)] = True | ||
|
||
for i in range(len(s), -1, -1): | ||
for j in range(len(p) - 1, -1 ,-1): | ||
match = i < len(s) and (s[i] == p[j] or p[j] == ".") | ||
|
||
if (j + 1) < len(p) and p[j + 1] == "*": | ||
cache[i][j] = cache[i][j + 2] | ||
if match: | ||
cache[i][j] = cache[i + 1][j] or cache[i][j] | ||
elif match: | ||
cache[i][j] = cache[i+1][j+1] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.isMatch
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
if not p and not q: return True | ||
if p and q and p.val == q.val: | ||
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) | ||
else: return False | ||
if p or q: | ||
return ( | ||
self.isSameTree(p.left, q.left) | ||
and self.isSameTree(p.right, q.right) | ||
if p and q and p.val == q.val | ||
else False | ||
) | ||
|
||
else: return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.isSameTree
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Swap if/else branches (
swap-if-else-branches
) - Split conditional into multiple branches (
split-or-ifs
) - Merge duplicate blocks in conditional (
merge-duplicate-blocks
) - Replace if statement with if expression (
assign-if-exp
) - Remove redundant conditional (
remove-redundant-if
)
|
||
while q: | ||
val = [] | ||
for i in range(len(q)): | ||
|
||
for _ in range(len(q)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.levelOrder
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
if not root: | ||
return 0 | ||
|
||
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) | ||
return ( | ||
1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) | ||
if root | ||
else 0 | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.maxDepth
refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression
) - Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function LRUCache.__init__
:
|
||
for i, a in enumerate(nums): | ||
if i > 0 and a == nums[i - 1]: | ||
continue | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.threeSum
:
if c == "+": | ||
if c == "*": | ||
stack.append(stack.pop() * stack.pop()) | ||
elif c == "+": | ||
stack.append(stack.pop() + stack.pop()) | ||
elif c == "-": | ||
a, b = stack.pop(), stack.pop() | ||
stack.append(b - a) | ||
elif c == "*": | ||
stack.append(stack.pop() * stack.pop()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.evalRPN
refactored with the following changes:
- Simplify conditional into switch-like form (
switch
)
|
||
for n in nums: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.maxProduct
:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.minCostConnectPoints
:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.minInterval
:
|
||
while q: | ||
rightSide = None | ||
qLen = len(q) | ||
|
||
for i in range(qLen): | ||
node = q.popleft() | ||
if node: | ||
|
||
for _ in range(qLen): | ||
if node := q.popleft(): | ||
rightSide = node | ||
q.append(node.left) | ||
q.append(node.right) | ||
if rightSide: | ||
res.append(rightSide.val) | ||
res.append(rightSide.val) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.rightSideView
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
) - Use named expression to simplify assignment and conditional (
use-named-expression
)
|
||
carry = 0 | ||
while l1 or l2 or carry: | ||
v1 = l1.val if l1 else 0 | ||
v2 = l2.val if l2 else 0 | ||
|
||
# new digit | ||
val = v1 + v2 + carry | ||
carry = val // 10 | ||
val = val % 10 | ||
cur.next = ListNode(val) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.addTwoNumbers
:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.isValid
:
|
||
while slow != fast: | ||
fast = self.sumSquareDigits(fast) | ||
fast = self.sumSquareDigits(fast) | ||
slow = self.sumSquareDigits(slow) | ||
|
||
return True if fast == 1 else False | ||
return fast == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.isHappy
refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
) - Simplify boolean if expression (
boolean-if-exp-identity
)
|
||
countS, countT = {}, {} | ||
|
||
for i in range(len(s)): | ||
countS[s[i]] = 1 + countS.get(s[i], 0) | ||
countT[t[i]] = 1 + countT.get(t[i], 0) | ||
for c in countS: | ||
if countS[c] != countT.get(c, 0): | ||
return False | ||
return True | ||
return all(value == countT.get(c, 0) for c, value in countS.items()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.isAnagram
refactored with the following changes:
- Use items() to directly unpack dictionary values (
use-dict-items
) - Use any() instead of for loop (
use-any
) - Invert any/all to simplify comparisons (
invert-any-all
)
res = len(nums) | ||
|
||
for i in range(len(nums)): | ||
res += (i - nums[i]) | ||
return res | ||
return len(nums) + sum((i - nums[i]) for i in range(len(nums))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.missingNumber
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
) - Convert for loop into call to sum() (
sum-comprehension
)
for i in range(nums.count(val)): # loop how many val is in the list | ||
for _ in range(nums.count(val)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.removeElement
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
This removes the following comments ( why? ):
# loop how many val is in the list
res = "" | ||
for s in strs: | ||
res += str(len(s)) + "#" + s | ||
return res | ||
return "".join(f"{len(s)}#{s}" for s in strs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.encode
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
) - Use str.join() instead of for loop (
use-join
) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
) - Remove unnecessary calls to
str()
from formatted values in f-strings (remove-str-from-fstring
)
if needle == "": return 0 | ||
if not needle: return 0 | ||
lps = [0] * len(needle) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.strStr
refactored with the following changes:
- Replaces an empty collection equality with a boolean operation (
simplify-empty-collection-comparison
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 1.68%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.lengthOfLongestSubstring
:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.maxCoins
:
return len(set(dsu.findParent(x) for x in range(n))) No newline at end of file | ||
return len({dsu.findParent(x) for x in range(n)}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.countComponents
refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
)
|
||
while l <= r: | ||
mid = (l + r) // 2 | ||
if target == nums[mid]: | ||
return mid | ||
|
||
# left sorted portion | ||
if nums[l] <= nums[mid]: | ||
if target > nums[mid] or target < nums[l]: | ||
l = mid + 1 | ||
else: | ||
r = mid - 1 | ||
# right sorted portion | ||
elif target < nums[mid] or target > nums[r]: | ||
r = mid - 1 | ||
else: | ||
if target < nums[mid] or target > nums[r]: | ||
r = mid - 1 | ||
else: | ||
l = mid + 1 | ||
l = mid + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.search
refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif
)
This removes the following comments ( why? ):
# right sorted portion
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.countBits
:
|
||
# base case | ||
if len(nums) == 1: | ||
return [nums[:]] # nums[:] is a deep copy | ||
for i in range(len(nums)): | ||
|
||
for _ in range(len(nums)): | ||
n = nums.pop(0) | ||
perms = self.permute(nums) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.permute
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
|
||
if sum(matchsticks) / 4 != length: | ||
return False | ||
matchsticks.sort(reverse=True) | ||
def backtrack(i): | ||
if i == len(matchsticks): | ||
return True | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.makesquare
:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function Solution.longestPalindrome
:
res = helper(x * x, n // 2) | ||
|
||
res = helper(x**2, n // 2) | ||
return x * res if n % 2 else res | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.myPow
refactored with the following changes:
- Replace x * x with x ** 2 (
square-identity
)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Solution.change
refactored with the following changes:
- Remove unreachable code (
remove-unreachable-code
)
This removes the following comments ( why? ):
# Time: O(n*m)
# Memory: O(n*m)
# Memory: O(n) where n = amount
# DYNAMIC PROGRAMMING
Branch
main
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
main
branch, then run:Help us improve this pull request!