-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master 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: master
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.
print("Creating " + file_name + "in java and python dir...") | ||
with open("python/" + file_name + ".py", 'w'): | ||
print(f"Creating {file_name}in java and python dir...") | ||
with open(f"python/{file_name}.py", 'w'): | ||
pass | ||
with open("java/" + file_name + ".java", 'w'): | ||
with open(f"java/{file_name}.java", 'w'): |
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.
Lines 9-12
refactored with the following changes:
- Use f-string instead of string concatenation [×6] (
use-fstring-for-concatenation
)
temp_s = '#'.join('{}'.format(s)) | ||
temp_s = '#'.join(f'{s}') |
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.longestPalindrome
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
flag = True if x < 0 else False | ||
flag = x < 0 | ||
if flag: | ||
x = -x | ||
x = str(x)[::-1] | ||
|
||
if flag: | ||
x = "-" + x | ||
x = f"-{x}" | ||
|
||
value = 2 ** 31 | ||
x = int(x) | ||
if -value <= x < value: | ||
return x | ||
return 0 | ||
return x if -value <= x < value 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.reverse
refactored with the following changes:
- Simplify boolean if expression (
boolean-if-exp-identity
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
if pos < ls and str[pos] == '-': | ||
sign = -1 | ||
pos += 1 | ||
elif pos < ls and str[pos] == '+': | ||
pos += 1 | ||
if pos < ls: | ||
if str[pos] == '-': | ||
sign = -1 | ||
pos += 1 | ||
elif str[pos] == '+': | ||
pos += 1 | ||
while pos < ls and ord(str[pos]) >= ord('0') and ord(str[pos]) <= ord('9'): | ||
num = ord(str[pos]) - ord('0') | ||
if result > max_int / 10 or ( result == max_int / 10 and num >= 8): | ||
if sign == -1: | ||
return min_int | ||
return max_int | ||
return min_int if sign == -1 else max_int |
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.myAtoi
refactored with the following changes:
- Lift repeated conditional into its own if statement (
lift-duplicated-conditional
) - Swap positions of nested conditionals (
swap-nested-ifs
) - Lift code into else after jump in control flow (
reintroduce-else
) - Hoist nested repeated code outside conditional statements (
hoist-similar-statement-from-if
) - Replace if statement with if expression (
assign-if-exp
)
if (x == x[::-1]): | ||
return True | ||
return False | ||
return x == x[::-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.isPalindrome
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
for j in range(i + 1, ls): | ||
# append ascending order pair | ||
if nums[i] < nums[j]: | ||
pair.append([i,j]) | ||
pair.extend([i,j] for j in range(i + 1, ls) if nums[i] < nums[j]) | ||
pos = 0 | ||
if len(pair) > 0: | ||
if pair: |
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.nextPermutation
refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend
) - Simplify sequence length comparison (
simplify-len-comparison
)
This removes the following comments ( why? ):
# append ascending order pair
else: | ||
if len(stack) > 0: | ||
data[i] = 1 | ||
data[stack.pop(-1)] = 1 | ||
elif stack: | ||
data[i] = 1 | ||
data[stack.pop(-1)] = 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.longestValidParentheses
refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Simplify sequence length comparison (
simplify-len-comparison
)
l, r = int(0), len(nums) - 1 | ||
l, r = 0, len(nums) - 1 | ||
while l < r: | ||
mid = int((l + r) / 2) | ||
if nums[mid] < target: | ||
l = mid + 1 | ||
else: | ||
r = mid | ||
if nums[l] < target: | ||
return l + 1 | ||
return l | ||
return l + 1 if nums[l] < target else l |
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.searchInsert
refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
) - Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
for j in range(9): | ||
if board[i][j] == '.': | ||
empty.append(9 * i + j) | ||
empty.extend(9 * i + j for j in range(9) if board[i][j] == '.') |
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.solveSudoku
refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend
)
step = 0 | ||
for index in range(start + 1, end): | ||
if height[index] > 0: | ||
step += height[index] | ||
step = sum( | ||
height[index] for index in range(start + 1, end) if height[index] > 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.rain_water
refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension
)
if n < 0: | ||
return 1 / res | ||
return res | ||
return 1 / res if n < 0 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:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
board = [['.'] * n for t in range(n)] | ||
board = [['.'] * n for _ 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.solveNQueens
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
matrix[r_end][c_start:c_end + 1][::-1] +\ | ||
[matrix[j][c_start] for j in reversed(range(r_start + 1, r_end))] | ||
res = curr + self.get_spiralOrder(matrix, r_start + 1, r_end - 1, c_start + 1, c_end - 1) | ||
return res | ||
matrix[r_end][c_start:c_end + 1][::-1] +\ | ||
[matrix[j][c_start] for j in reversed(range(r_start + 1, r_end))] | ||
return curr + self.get_spiralOrder( | ||
matrix, r_start + 1, r_end - 1, c_start + 1, c_end - 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.get_spiralOrder
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if curr_int.start <= new_int.start: | ||
if curr_int.end > new_int.start: | ||
return True | ||
else: | ||
if curr_int.start <= new_int.end: | ||
return True | ||
return False | ||
return ( | ||
curr_int.start <= new_int.start | ||
and curr_int.end > new_int.start | ||
or curr_int.start > new_int.start | ||
and curr_int.start <= new_int.end | ||
) |
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.check_overlap
refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
python/058_Length_of_Last_Word.py
Outdated
if len(temp) == 0: | ||
return 0 | ||
else: | ||
return len(temp[-1]) No newline at end of file | ||
return len(temp[-1]) if temp 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.lengthOfLastWord
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Simplify sequence length comparison (
simplify-len-comparison
) - Swap if/else branches of if expression to remove negation (
swap-if-expression
)
while j < length: | ||
if nums[j] != nums[i]: | ||
break | ||
while j < length and nums[j] == nums[i]: | ||
j += 1 | ||
if j-i > 2: | ||
length -= j-i-2 | ||
for k in range(j-i-2): | ||
for _ in range(j-i-2): |
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.removeDuplicates
refactored with the following changes:
- Move a guard clause in a while statement's body into its test (
while-guard-to-condition
) - Replace unused for index with underscore (
for-index-underscore
) - Simplify logical expression using De Morgan identities (
de-morgan
)
for j in reversed(range(len(res))): | ||
res.append(res[j] + (1 << i)) | ||
res.extend(res[j] + (1 << i) for j in reversed(range(len(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.grayCode
refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend
)
if m == 1: | ||
return prev | ||
return head | ||
return prev if m == 1 else head |
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.reverseBetween
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
add1 = s[0:i] | ||
add1 = s[:i] | ||
add2 = s[i:i + j] | ||
add3 = s[i + j:i + j + k] | ||
add4 = s[i + j + k:] | ||
if self.isValid(add1) and self.isValid(add2) and \ | ||
self.isValid(add3) and self.isValid(add4): | ||
res.append(add1 + '.' + add2 + '.' + add3 + '.' + add4) | ||
self.isValid(add3) and self.isValid(add4): | ||
res.append(f'{add1}.{add2}.{add3}.{add4}') |
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.restoreIpAddresses
refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
) - Use f-string instead of string concatenation [×6] (
use-fstring-for-concatenation
)
if add[0] == '0': | ||
return False | ||
if int(add) <= 255: | ||
return True | ||
return False | ||
return False if add[0] == '0' else int(add) <= 255 |
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.isValid
refactored with the following changes:
- Lift code into else after jump in control flow [×2] (
reintroduce-else
) - Replace if statement with if expression [×2] (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
Branch
master
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
master
branch, then run:Help us improve this pull request!