Sourcery Starbot ⭐ refactored gkitg/edu_tmp#1
Conversation
|
|
||
| # длинный способ | ||
| if year % 4 != 0: | ||
| print("Обычный") | ||
| elif year % 100 == 0: | ||
| if year % 400 == 0: | ||
| print("Високосный") | ||
| else: | ||
| print("Обычный") | ||
| if ( | ||
| year % 4 == 0 | ||
| and year % 100 == 0 | ||
| and year % 400 == 0 | ||
| or year % 4 == 0 | ||
| and year % 100 != 0 | ||
| ): | ||
| print("Високосный") | ||
| else: | ||
| print("Високосный") No newline at end of file | ||
| print("Обычный") No newline at end of file |
There was a problem hiding this comment.
Lines 15-25 refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks)
| else: | ||
| odd += 1 | ||
| num = num // 10 | ||
| num //= 10 |
There was a problem hiding this comment.
Lines 17-17 refactored with the following changes:
- Replace assignment with augmented assignment (
aug-assign)
| while num > 0: | ||
| result = result * 10 + num % 10 | ||
| num = num // 10 | ||
| num //= 10 |
There was a problem hiding this comment.
Lines 14-14 refactored with the following changes:
- Replace assignment with augmented assignment (
aug-assign)
| return n | ||
| sum_n = n + sum_natural(n - 1) | ||
| return sum_n | ||
| return n if n == 1 else n + sum_natural(n - 1) |
There was a problem hiding this comment.
Function sum_natural refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| if number < 10: | ||
| return number | ||
| return number % 10 + sum_digits(number // 10) | ||
| return number if number < 10 else number % 10 + sum_digits(number // 10) |
There was a problem hiding this comment.
Function sum_digits 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)
| x = "".join([i for i in x]) | ||
| y = "".join([i for i in y]) | ||
| x = "".join(list(x)) | ||
| y = "".join(list(y)) |
There was a problem hiding this comment.
Function hex_sum refactored with the following changes:
- Replace identity comprehension with call to collection constructor [×2] (
identity-comprehension)
| x = "".join([i for i in x]) | ||
| y = "".join([i for i in y]) | ||
| x = "".join(list(x)) | ||
| y = "".join(list(y)) |
There was a problem hiding this comment.
Function hex_mul refactored with the following changes:
- Replace identity comprehension with call to collection constructor [×2] (
identity-comprehension)
| else: | ||
| r = deque() | ||
| while x > 15: | ||
| d = x % 16 | ||
| r.appendleft(str(d) if d < 10 else self.hex_letters._fields[d-10]) | ||
| x = x // 16 | ||
| r.appendleft(str(x) if x < 10 else self.hex_letters._fields[x-10]) | ||
| return list(r) | ||
| r = deque() | ||
| while x > 15: | ||
| d = x % 16 | ||
| r.appendleft(str(d) if d < 10 else self.hex_letters._fields[d-10]) | ||
| x = x // 16 | ||
| r.appendleft(str(x) if x < 10 else self.hex_letters._fields[x-10]) | ||
| return list(r) |
There was a problem hiding this comment.
Function HexCalc.to_hex refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| hex_number[value] = key | ||
|
|
||
| return sum([hex_number[j] * (16 ** i) for i, j in enumerate(num)]) | ||
| return sum(hex_number[j] * (16 ** i) for i, j in enumerate(num)) |
There was a problem hiding this comment.
Function hex_to_decimal refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator)
| for key in pi_func.keys(): | ||
| for key, size in pi_func.items(): | ||
| if num <= key: | ||
| size = pi_func[key] | ||
| break | ||
|
|
||
| array = [i for i in range(size)] | ||
| array = list(range(size)) |
There was a problem hiding this comment.
Function prime refactored with the following changes:
- Use items() to directly unpack dictionary values (
use-dict-items) - Remove unnecessary call to keys() (
remove-dict-keys) - Replace identity comprehension with call to collection constructor (
identity-comprehension)
| for i in range(num): | ||
| for _ in range(num): | ||
| name = input('name = ') | ||
| spam = [] | ||
| for j in range(1, 5): | ||
| spam.append(int(input(f'{j} = '))) | ||
| spam = [int(input(f'{j} = ')) for j in range(1, 5)] |
There was a problem hiding this comment.
Lines 7-11 refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore) - Convert for loop into list comprehension (
list-comprehension)
| else: | ||
| odd += 1 | ||
| num = num // 10 | ||
| num //= 10 |
There was a problem hiding this comment.
Lines 15-32 refactored with the following changes:
- Replace assignment with augmented assignment [×2] (
aug-assign)
|
|
||
|
|
||
| orig_list = [random.randint(-100, 100) for i in range(10)] | ||
| orig_list = [random.randint(-100, 100) for _ in range(10)] |
There was a problem hiding this comment.
Lines 40-40 refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| if len(alist)>1: | ||
| mid = len(alist)//2 | ||
| lefthalf = alist[:mid] | ||
| righthalf = alist[mid:] | ||
|
|
||
| merge_sort(lefthalf) | ||
| merge_sort(righthalf) | ||
|
|
||
| i=0 | ||
| j=0 | ||
| k=0 | ||
| while i<len(lefthalf) and j<len(righthalf): | ||
| if lefthalf[i]<righthalf[j]: | ||
| alist[k]=lefthalf[i] | ||
| i=i+1 | ||
| else: | ||
| alist[k]=righthalf[j] | ||
| j=j+1 | ||
| k=k+1 | ||
|
|
||
| while i<len(lefthalf): | ||
| if len(alist) <= 1: | ||
| return | ||
| mid = len(alist)//2 | ||
| lefthalf = alist[:mid] | ||
| righthalf = alist[mid:] | ||
|
|
||
| merge_sort(lefthalf) | ||
| merge_sort(righthalf) | ||
|
|
||
| i=0 | ||
| j=0 | ||
| k=0 | ||
| while i<len(lefthalf) and j<len(righthalf): | ||
| if lefthalf[i]<righthalf[j]: | ||
| alist[k]=lefthalf[i] | ||
| i=i+1 | ||
| k=k+1 | ||
|
|
||
| while j<len(righthalf): | ||
| i += 1 | ||
| else: | ||
| alist[k]=righthalf[j] | ||
| j=j+1 | ||
| k=k+1 | ||
| j += 1 | ||
| k=k+1 | ||
|
|
||
| while i<len(lefthalf): | ||
| alist[k]=lefthalf[i] | ||
| i += 1 | ||
| k=k+1 | ||
|
|
||
| while j<len(righthalf): | ||
| alist[k]=righthalf[j] | ||
| j += 1 | ||
| k=k+1 |
There was a problem hiding this comment.
Function merge_sort refactored with the following changes:
- Add guard clause (
last-if-guard) - Replace assignment with augmented assignment [×4] (
aug-assign)
| alist = [random.random()*50 for i in range(n)] | ||
| alist = [random.random()*50 for _ in range(n)] |
There was a problem hiding this comment.
Lines 43-43 refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| if any(i.isupper() for i in data) and any(i.islower() for i in data) and any(i.isdigit() for i in data): | ||
| return True | ||
| else: | ||
| return False | ||
| return ( | ||
| any(i.isupper() for i in data) | ||
| and any(i.islower() for i in data) | ||
| and any(i.isdigit() for i in data) | ||
| ) |
There was a problem hiding this comment.
Function checkio refactored with the following changes:
- 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)
| newlst=[] | ||
| for i in data: | ||
| if data.count(i)>1: | ||
| newlst.append(i) | ||
| return newlst | ||
| return [i for i in data if data.count(i)>1] |
There was a problem hiding this comment.
Function checkio refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| assert list(checkio([1, 2, 3, 4, 5])) == [], "2nd example" | ||
| assert not list(checkio([1, 2, 3, 4, 5])), "2nd example" |
There was a problem hiding this comment.
Lines 67-67 refactored with the following changes:
- Replaces an empty collection equality with a boolean operation (
simplify-empty-collection-comparison)
| """ | ||
| segment = (((x1 - x2) ** 2) + ((y1 - y2) ** 2)) ** 0.5 | ||
| return segment | ||
| return (((x1 - x2) ** 2) + ((y1 - y2) ** 2)) ** 0.5 |
There was a problem hiding this comment.
Function length_segment refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| elif (line_a[0] == line_b[0] and (line_a[0] != line_c[0] or line_b[0] != line_c[0])): | ||
| elif line_a[0] == line_b[0]: | ||
| print("a || b") | ||
| print(0) | ||
| elif (line_a[0] == line_c[0] and (line_a[0] != line_b[0] or line_c[0] != line_b[0])): | ||
| elif line_a[0] == line_c[0]: | ||
| print("a || с") | ||
| print(0) | ||
| elif (line_b[0] == line_c[0] and (line_b[0] != line_a[0] or line_c[0] != line_a[0])): | ||
| elif line_b[0] == line_c[0]: |
There was a problem hiding this comment.
Function run refactored with the following changes:
- Remove redundant conditional [×3] (
remove-redundant-if)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run: