Conversation
| if wt[n-1]<=W: | ||
| return max(val[n-1]+knapsack_recursive(wt,val,W-wt[n-1],n-1),knapsack_recursive(wt,val,W,n-1)) | ||
| else: | ||
| if wt[n-1]<=W: | ||
| return max(val[n-1]+knapsack_recursive(wt,val,W-wt[n-1],n-1),knapsack_recursive(wt,val,W,n-1)) | ||
| elif wt[n-1]>W: | ||
| return knapsack_recursive(wt,val,W,n-1) | ||
| return knapsack_recursive(wt,val,W,n-1) |
There was a problem hiding this comment.
Function knapsack_recursive refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else) - Remove redundant conditional (
remove-redundant-if)
| for i in range(4): | ||
| mat.append([0] * 4) | ||
|
|
||
| mat = [[0] * 4 for _ in range(4)] |
There was a problem hiding this comment.
Function start_game refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace unused for index with underscore (
for-index-underscore)
This removes the following comments ( why? ):
# elements as 0.
# appending 4 list each with four
# declaring an empty list then
| if(mat[i][j]== mat[i + 1][j] or mat[i][j]== mat[i][j + 1]): | ||
| if mat[i][j] in [mat[i + 1][j], mat[i][j + 1]]: | ||
| return 'GAME NOT OVER' | ||
|
|
||
| for j in range(3): | ||
| if(mat[3][j]== mat[3][j + 1]): | ||
| return 'GAME NOT OVER' | ||
|
|
||
| for i in range(3): | ||
| if(mat[i][3]== mat[i + 1][3]): | ||
| return 'GAME NOT OVER' | ||
|
|
||
| # else we have lost the game | ||
| return 'LOST' | ||
| return next( | ||
| ('GAME NOT OVER' for i in range(3) if (mat[i][3] == mat[i + 1][3])), | ||
| 'LOST', | ||
| ) |
There was a problem hiding this comment.
Function get_current_state refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons) - Use the built-in function
nextinstead of a for-loop (use-next)
This removes the following comments ( why? ):
# else we have lost the game
| # empty grid | ||
| new_mat = [] | ||
|
|
||
| # with all cells empty | ||
| for i in range(4): | ||
| new_mat.append([0] * 4) | ||
|
|
||
| new_mat = [[0] * 4 for _ in range(4)] |
There was a problem hiding this comment.
Function compress refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace unused for index with underscore (
for-index-underscore)
This removes the following comments ( why? ):
# empty grid
# with all cells empty
| print("Please Press 3 To Pay in.") | ||
| print("Please Press 4 To Return Card.") | ||
|
|
There was a problem hiding this comment.
Lines 19-78 refactored with the following changes:
- Simplify conditional into switch-like form [×2] (
switch) - Use set when checking membership of a collection of literals (
collection-into-set) - Remove redundant conditional (
remove-redundant-if) - Replace assignment with augmented assignment (
aug-assign)
| fileHandle = open(fileName, "r") | ||
| with open(fileName, "r") as fileHandle: | ||
| # Declare a variable to store the number of vowels. Initally it is zero. | ||
| count = 0 | ||
|
|
||
| # Declare a variable to store the number of vowels. Initally it is zero. | ||
| count = 0 | ||
| # create an array of all the vowels (upper and lower case) that can be used to compare and determine if a character is a vowel | ||
| vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] | ||
|
|
||
| # create an array of all the vowels (upper and lower case) that can be used to compare and determine if a character is a vowel | ||
| vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] | ||
|
|
||
| # Read each character and compare it to the characters in the array. If found in the vowels array, then increase count. | ||
| for char in fileHandle.read(): | ||
| if char in vowels: | ||
| count = count+1 | ||
|
|
||
| # Close the file | ||
| fileHandle.close() | ||
| # Read each character and compare it to the characters in the array. If found in the vowels array, then increase count. | ||
| for char in fileHandle.read(): | ||
| if char in vowels: | ||
| count = count+1 |
There was a problem hiding this comment.
Lines 8-22 refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed)
This removes the following comments ( why? ):
# Close the file
| if min_node is None: | ||
| if min_node is None or visited[node] < visited[min_node]: | ||
| min_node = node | ||
| elif visited[node] < visited[min_node]: | ||
| min_node = node | ||
|
|
There was a problem hiding this comment.
Function dijsktra refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if)
|
|
||
| list1=[] | ||
| list2=[] |
There was a problem hiding this comment.
Lines 4-32 refactored with the following changes:
- Move assignment closer to its usage within a block [×2] (
move-assign-in-block) - Convert for loop into list comprehension [×2] (
list-comprehension)
This removes the following comments ( why? ):
# If Decimal Numbers occured in the Sentence
| for j in range(3): | ||
|
|
||
| globals()[i+str(j+1)].configure(text=f' ') | ||
| globals()[i+str(j+1)].configure(text=' ') |
There was a problem hiding this comment.
Function Retry refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring)
| if Xcount>Ocount: | ||
| string='Player 1(X) won' | ||
| else: | ||
| string='Player 2(O) won' | ||
| string = 'Player 1(X) won' if Xcount>Ocount else 'Player 2(O) won' | ||
| finish(string) | ||
|
|
There was a problem hiding this comment.
Function win_check refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Swap positions of nested conditionals [×2] (
swap-nested-ifs)
| vars()[i+str(j+1)]=tk.Button(vars()[i], text=f' ',bd='1',command=partial(clicked,i+' '+str(j+1))) | ||
| vars()[i + str(j + 1)] = tk.Button( | ||
| vars()[i], | ||
| text=' ', | ||
| bd='1', | ||
| command=partial(clicked, f'{i} {str(j + 1)}'), | ||
| ) |
There was a problem hiding this comment.
Lines 111-111 refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
|
|
||
|
|
||
| if(level!="hard" and level!="easy"): | ||
| if level not in ["hard", "easy"]: |
There was a problem hiding this comment.
Lines 24-24 refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| for i in range(len(word)): | ||
| for _ in range(len(word)): | ||
| blank_list += "_" | ||
| end_game=False | ||
| while end_game == False: | ||
| while not end_game: |
There was a problem hiding this comment.
Lines 66-69 refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore) - Simplify comparison to boolean (
simplify-boolean-comparison)
| hour = int(seconds/3600) #Gets hours | ||
| minute = int(seconds/60) - (hour*60) #Gets remanining minutes | ||
| hour = seconds // 3600 | ||
| minute = seconds // 60 - hour*60 | ||
| second = seconds - (minute*60) - (hour*3600) #Gets remaining seconds | ||
|
|
||
| hour = str(hour) if len(str(hour)) > 1 else '0' + str(hour) #adds 0 if the hours,minutes or seconds are only one charachter long | ||
| second = str(second) if len(str(second)) > 1 else '0' + str(second) | ||
| minute = str(minute) if len(str(minute)) > 1 else '0' + str(minute) | ||
| hour = str(hour) if len(str(hour)) > 1 else f'0{str(hour)}' | ||
| second = str(second) if len(str(second)) > 1 else f'0{str(second)}' | ||
| minute = str(minute) if len(str(minute)) > 1 else f'0{str(minute)}' |
There was a problem hiding this comment.
Function make_readable refactored with the following changes:
- Simplify division expressions [×2] (
simplify-division) - Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation)
This removes the following comments ( why? ):
#adds 0 if the hours,minutes or seconds are only one charachter long
#Gets remanining minutes
#Gets hours
|
|
||
| #screen for output | ||
| screen = turtle.Screen() | ||
|
|
||
| # Defining a turtle Instance | ||
| t = turtle.Turtle() | ||
| speed(0) | ||
|
|
||
| # initially penup() | ||
| t.penup() | ||
| t.goto(-400, 250) | ||
| t.pendown() | ||
|
|
There was a problem hiding this comment.
Lines 3-87 refactored with the following changes:
- Replace unused for index with underscore [×2] (
for-index-underscore)
| for i in range(0,nr_letters): | ||
| for _ in range(0,nr_letters): | ||
| random_letter = r.choice(letters) | ||
| random_letters.append(random_letter) | ||
|
|
||
| random_symbols = [] | ||
| for i in range(0,nr_symbols): | ||
| for _ in range(0,nr_symbols): | ||
| random_symbol = r.choice(symbols) | ||
| random_symbols.append(random_symbol) | ||
|
|
||
| random_numbers = [] | ||
| for i in range(0,nr_numbers): | ||
| for _ in range(0,nr_numbers): |
There was a problem hiding this comment.
Function hard_pass refactored with the following changes:
- Replace unused for index with underscore [×3] (
for-index-underscore) - Use str.join() instead of for loop (
use-join) - Simplify generator expression (
simplify-generator)
This removes the following comments ( why? ):
#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P
#Hard Level - Order of characters randomised:
| val = None | ||
| while not valid_square: | ||
| square = input(self.player + ' turn. Please introduce a move (1-9): ') | ||
| square = input(f'{self.player} turn. Please introduce a move (1-9): ') |
There was a problem hiding this comment.
Function Human.get_move refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| def get_move(self, game): | ||
| square = random.choice(game.remaining_moves()) | ||
| return square | ||
| return random.choice(game.remaining_moves()) |
There was a problem hiding this comment.
Function RandomComputer.get_move refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if len(game.remaining_moves()) == 9: | ||
| square = random.choice(game.remaining_moves()) | ||
| else: | ||
| square = self.minimax(game, self.player)['position'] | ||
| return square | ||
| return ( | ||
| random.choice(game.remaining_moves()) | ||
| if len(game.remaining_moves()) == 9 | ||
| else self.minimax(game, self.player)['position'] | ||
| ) |
There was a problem hiding this comment.
Function SmartComputer.get_move refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if player == max_player: | ||
| if sim_score['score'] > best['score']: | ||
| best = sim_score | ||
| else: | ||
| if sim_score['score'] < best['score']: | ||
| best = sim_score | ||
| if ( | ||
| player == max_player | ||
| and sim_score['score'] > best['score'] | ||
| or player != max_player | ||
| and sim_score['score'] < best['score'] | ||
| ): | ||
| best = sim_score |
There was a problem hiding this comment.
Function SmartComputer.minimax refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks)
|
|
||
| while True: | ||
| window.update() | ||
|
|
||
| #moving the ball | ||
| ball.setx(ball.xcor()+ballxdirection) | ||
| ball.sety(ball.ycor()+ballxdirection) | ||
|
|
There was a problem hiding this comment.
Lines 77-117 refactored with the following changes:
- Replace call to format with f-string [×2] (
use-fstring-for-formatting)
This removes the following comments ( why? ):
# Handling the collisions with paddles.
| if len(self.arr) > 0: | ||
| ditem = self.arr[0] | ||
| del self.arr[0] | ||
| return ditem | ||
| else: | ||
| if len(self.arr) <= 0: | ||
| return #queue is empty | ||
| ditem = self.arr[0] | ||
| del self.arr[0] | ||
| return ditem |
There was a problem hiding this comment.
Function Queue.dequeue refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| t1 = [] | ||
| t2 = [] | ||
| while (len(y) != 0): | ||
| while y: |
There was a problem hiding this comment.
Lines 5-5 refactored with the following changes:
- Simplify sequence length comparison (
simplify-len-comparison)
| randomInteger=random.randint(0,2) | ||
| userInput=int(input("What do you choose ? Type 0 for Rock,1 for Paper or 2 for Scissors\n")) | ||
| if(userInput!=0 and userInput!=1 and userInput!=2): | ||
| if userInput not in [0, 1, 2]: |
There was a problem hiding this comment.
Lines 5-5 refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
|
|
||
| #hiding segments of snake | ||
| for segment in segments: | ||
| segment.goto(1000,1000) | ||
| #clearing the segments | ||
| segments.clear() | ||
|
|
||
| #reset score | ||
| score = 0 | ||
|
|
||
| #reset delay | ||
| delay = 0.1 | ||
|
|
||
| pen.clear() | ||
| pen.write("Score : {} High Score : {} ".format( | ||
| score, high_score), align="center", font=("Times New Roman", 24, "bold")) | ||
|
|
||
| pen.write( | ||
| f"Score : {score} High Score : {high_score} ", | ||
| align="center", | ||
| font=("Times New Roman", 24, "bold"), | ||
| ) | ||
|
|
There was a problem hiding this comment.
Lines 100-175 refactored with the following changes:
- Replace call to format with f-string [×3] (
use-fstring-for-formatting) - Simplify sequence length comparison (
simplify-len-comparison)
| print(f"Current Balance : ${self.balance}") | ||
| print() | ||
| else: | ||
| print("Not Enough Balance!!!") | ||
| print(f"Current Balance : ${self.balance}") | ||
| print() | ||
| print("Not Enough Balance!!!") | ||
|
|
||
| print(f"Current Balance : ${self.balance}") | ||
| print() |
There was a problem hiding this comment.
Function Bank.withdraw refactored with the following changes:
- Hoist repeated code outside conditional statement [×2] (
hoist-statement-from-if)
| n = int(input("Enter number of elements: ")) | ||
| print("Enter the elements") | ||
| for i in range(0,n): | ||
| for _ in range(0,n): |
There was a problem hiding this comment.
Function userInput refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| for char in name: | ||
| if char.isupper(): | ||
| print("_" + char.lower(), end = "") | ||
| print(f"_{char.lower()}", end = "") |
There was a problem hiding this comment.
Lines 7-7 refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| print(f'Initial number is: {initial_number}') | ||
| print(f'Initial number is: {num}') | ||
| while num != 1: | ||
| print(num) | ||
| if num % 2 == 0: | ||
| num = int(num / 2) | ||
| else: | ||
| num = int(3 * num + 1) | ||
| num = int(num / 2) if num % 2 == 0 else int(3 * num + 1) |
There was a problem hiding this comment.
Function collatz refactored with the following changes:
- Use previously assigned local variable (
use-assigned-variable) - Replace if statement with if expression (
assign-if-exp)
| maxNum = 1000; #maximum number in array | ||
| lengthOfItems= 500; #this will generate 500 random numbers. | ||
|
|
||
| file = open("newFile.csv", "a") # creates a new file named newFile.csv - feel free to edit file name | ||
|
|
||
| for i in range(lengthOfItems): | ||
| file.write(str(str(rd.randint(minNum,maxNum)) + ",")) | ||
|
|
||
| file.close() | ||
| maxNum = 1000 | ||
| lengthOfItems= 500 | ||
| with open("newFile.csv", "a") as file: | ||
| for _ in range(lengthOfItems): | ||
| file.write(str(f"{str(rd.randint(minNum, maxNum))},")) |
There was a problem hiding this comment.
Lines 7-15 refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed) - Replace unused for index with underscore (
for-index-underscore) - Use f-string instead of string concatenation (
use-fstring-for-concatenation)
This removes the following comments ( why? ):
# creates a new file named newFile.csv - feel free to edit file name
#this will generate 500 random numbers.
#maximum number in array
Branch
mainrefactored 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
mainbranch, then run:Help us improve this pull request!