-
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.
| b = carry | ||
| if b > 0: return (a & mask) | ||
| else: return a No newline at end of file | ||
| return (a & mask) if b > 0 else a No newline at end of file |
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 getSum refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| # columns are the values of weights required | ||
|
|
||
| t = [[0 for i in range(cols)] for i in range(rows)] | ||
| t = [[0 for _ in range(cols)] for _ in range(rows)] |
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 knapsack refactored with the following changes:
- Replace unused for index with underscore [×2] (
for-index-underscore)
|
|
||
| if i + z[i] - 1 > r: | ||
| l = i | ||
| r = i + z[i] - 1 | ||
|
|
||
| res = [] | ||
| for i in range(size , len(text)): | ||
| if z[i] == size: | ||
| res.append(i - size - 1) | ||
| return res | ||
|
|
||
| return [i - size - 1 for i in range(size , len(text)) if z[i] == size] |
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 Z_Algorithm.z_function refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| rows = len(s2) + 1 | ||
|
|
||
| t = [[0 for i in range(cols)] for i in range(rows)] | ||
| t = [[0 for _ in range(cols)] for _ in range(rows)] |
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 lcs refactored with the following changes:
- Replace unused for index with underscore [×2] (
for-index-underscore)
| rows = len(s2) + 1 | ||
|
|
||
| t = [[0 for i in range(cols)] for i in range(rows)] | ||
| t = [[0 for _ in range(cols)] for _ in range(rows)] |
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 lcs refactored with the following changes:
- Replace unused for index with underscore [×2] (
for-index-underscore)
| result.append(i) | ||
|
|
||
| return result | ||
| return [i for i in range(1, n+1) if (n%i == 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 divisors.findAllDivisors refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace
list()with[](list-literal) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| result = list() | ||
|
|
||
| for i in range(1, n+1): | ||
| if (n%i == 0 and i%2==1): | ||
| result.append(i) | ||
|
|
||
| return result | ||
| return [i for i in range(1, n+1) if (n%i == 0 and i%2==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 divisors.oddFactors refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace
list()with[](list-literal) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| result = list() | ||
|
|
||
| for i in range(1, n+1): | ||
| if (n%i == 0 and i%2==0): | ||
| result.append(i) | ||
|
|
||
| return result | ||
| return [i for i in range(1, n+1) if (n%i == 0 and i%2==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 divisors.evenFactors refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace
list()with[](list-literal) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| result = list() | ||
| result = [] |
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 divisors.primeFactors refactored with the following changes:
- Replace
list()with[](list-literal)
| else: | ||
| for num in range(1, number+1): | ||
| answer = answer * num | ||
| for num in range(1, number+1): | ||
| answer = answer * num |
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 factorial refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| answer = number * factorial(number - 1) | ||
|
|
||
| return answer | ||
| return 1 if number in [0, 1] else number * factorial(number - 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 factorial refactored with the following changes:
- Split conditional into multiple branches [×3] (
split-or-ifs) - Simplify conditional into switch-like form [×2] (
switch) - Replace multiple comparisons of same variable with
inoperator [×2] (merge-comparisons) - Inline variable that is immediately returned (
inline-immediately-returned-variable) - Lift code into else after jump in control flow (
reintroduce-else) - Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if) - Replace if statement with if expression (
assign-if-exp)
| return x | ||
|
|
||
| return gcd(y, x % y) | ||
| return x if y == 0 else gcd(y, x % y) |
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 gcd 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)
| return bases[x] | ||
| else: | ||
| return default | ||
| return bases[x] if x in bases else default |
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 verify_base refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| if(negative): | ||
| decimal_number = '-' + decimal_number | ||
| if negative: | ||
| decimal_number = f'-{decimal_number}' |
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 convert refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
| while i * i < num: | ||
| while i**2 < num: | ||
| i += 1 | ||
| if i * i == num: | ||
| return True | ||
| else: | ||
| return False | ||
| return i**2 == num |
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 is_perfect_square refactored with the following changes:
- Replace x * x with x ** 2 [×2] (
square-identity) - 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)
| else: | ||
| return "Not found" | ||
|
|
||
| return "Found" if arr[0] == val else "Not found" |
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 search refactored with the following changes:
- Replace if statement with if expression [×2] (
assign-if-exp) - Replace call to format with f-string (
use-fstring-for-formatting)
| else: | ||
| if arr[i] < arr[i-1]: | ||
| arr[i-1], arr[i] = arr[i], arr[i-1] | ||
| elif arr[i] < arr[i-1]: | ||
| arr[i-1], arr[i] = arr[i], arr[i-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 rearrange refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
| start = 0 | ||
| n = len(arr) - 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 find_sum refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| checker = 0 | ||
| pos = 0 | ||
| checker = 0 | ||
| for i in s: | ||
| val = ord(i) - ord('a') | ||
| if (checker & (1 << val) > 0): | ||
| return i | ||
| checker = checker | (1 << val) | ||
| pos += 1 | ||
| else: | ||
| checker = checker | (1 << 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 first_recurrence refactored with the following changes:
- Replace manual loop counter with call to enumerate (
convert-to-enumerate) - Remove unnecessary calls to
enumeratewhen the index is not used (remove-unused-enumerate) - Lift code into else after jump in control flow (
reintroduce-else)
| return min_pos | ||
|
|
||
| if count_negative & 1 == 0 and count_negative != 0: | ||
| if count_negative & 1 == 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 find refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| # First find out how many elements are there which are less than or | ||
| # equal to k | ||
| count = 0 | ||
| for i in arr: | ||
| if i <= k: | ||
| count += 1 | ||
|
|
||
| # This count defines a window - inside this window all our elements should | ||
| # be placed | ||
| # Find the count of bad elements - elements which are more than k and that will be | ||
| # our starting answer as we will have to swap them out | ||
| bad = 0 | ||
| for i in range(0, count): | ||
| if arr[i] > k: | ||
| bad += 1 | ||
|
|
||
| count = sum(1 for i in arr if i <= k) | ||
| bad = sum(1 for i in range(0, count) if arr[i] > k) |
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 min_swaps refactored with the following changes:
- Convert for loop into call to sum() [×2] (
sum-comprehension)
This removes the following comments ( why? ):
# This count defines a window - inside this window all our elements should
# First find out how many elements are there which are less than or
# our starting answer as we will have to swap them out
# equal to k
# Find the count of bad elements - elements which are more than k and that will be
# be placed
| count = 0 | ||
| for a in arr: | ||
| if not a == 0: | ||
| if a != 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 move refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan)
| def partition(arr): | ||
| s = sum(arr) | ||
| if not s % 3 == 0: | ||
| if s % 3 != 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 partition refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan)
| rem_lst = lst[:i] + lst[i+1:] | ||
| for p in permutation(rem_lst): | ||
| l.append([m] + p) | ||
| l.extend([m] + p for p in permutation(rem_lst)) |
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 permutation refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend)
| else: | ||
| pivot = array[0] | ||
| less = [i for i in array[1:] if i <= pivot] | ||
| greater = [i for i in array[1:] if i > pivot] | ||
| return quicksort(less) + [pivot] + quicksort(greater) | ||
| pivot = array[0] | ||
| less = [i for i in array[1:] if i <= pivot] | ||
| greater = [i for i in array[1:] if i > pivot] | ||
| return quicksort(less) + [pivot] + quicksort(greater) |
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 quicksort refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| if root.left == None and root.right == None and root.val == arr[index] and index == n -1: | ||
| if ( | ||
| root.left is None | ||
| and root.right is None | ||
| and root.val == arr[index] | ||
| and index == n - 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 check_path refactored with the following changes:
- Use x is None rather than x == None [×2] (
none-compare)
| if not root or (not root.left and not root.right): | ||
| if not root or not root.left and not root.right: | ||
| return True | ||
|
|
||
| else: | ||
| if root.left: | ||
| l = root.left.val | ||
| if root.left: | ||
| l = root.left.val | ||
|
|
||
| if root.right: | ||
| r = root.right.val | ||
| if root.right: | ||
| r = root.right.val | ||
|
|
||
| if (root.val == l + r) and sum_prop(root.left) and sum_prop(root.right): | ||
| return True | ||
| else: | ||
| return False | ||
| return bool( | ||
| (root.val == l + r) and sum_prop(root.left) and sum_prop(root.right) | ||
| ) |
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 sum_prop refactored with the following changes:
- Swap if/else branches [×2] (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else) - Replace if statement with if expression (
assign-if-exp) - Simplify boolean if expression (
boolean-if-exp-identity)
|
|
||
| if root.left == None and root.right == None: | ||
| return True | ||
|
|
||
| if root.left == None: | ||
| if root.left is None: | ||
| if root.right is None: | ||
| return True | ||
|
|
||
| return (abs(root.val - root.right.val) == 1) and continuous(root.right) | ||
|
|
||
| if root.right == None: | ||
| if root.right is None: |
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 continuous refactored with the following changes:
- Hoist a repeated condition into a parent condition (
hoist-repeated-if-condition) - Use x is None rather than x == None [×3] (
none-compare)
| for i in diagonal_map: | ||
| for j in diagonal_map[i]: | ||
|
|
||
| for value in diagonal_map.values(): | ||
| for j in value: |
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 diagonal_print refactored with the following changes:
- Use items() to directly unpack dictionary values (
use-dict-items) - Replace calls to
dict.itemswithdict.valueswhen the keys are not used (replace-dict-items-with-values)
| else: | ||
| lheight = height(root.left) | ||
| rheight = height(root.right) | ||
| lheight = height(root.left) | ||
| rheight = height(root.right) | ||
|
|
||
| return 1 + max(lheight, rheight) | ||
| return 1 + max(lheight, rheight) |
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 height refactored with the following changes:
- Swap if/else branches [×2] (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
Branch
masterrefactored 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
masterbranch, then run:Help us improve this pull request!