- 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?
Uh oh!
There was an error while loading. Please reload this page.
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.
| ifwt[n-1]<=W: | ||
| returnmax(val[n-1]+knapsack_recursive(wt,val,W-wt[n-1],n-1),knapsack_recursive(wt,val,W,n-1)) | ||
| else: | ||
| ifwt[n-1]<=W: | ||
| returnmax(val[n-1]+knapsack_recursive(wt,val,W-wt[n-1],n-1),knapsack_recursive(wt,val,W,n-1)) | ||
| elifwt[n-1]>W: | ||
| returnknapsack_recursive(wt,val,W,n-1) | ||
| returnknapsack_recursive(wt,val,W,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 knapsack_recursive refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else) - Remove redundant conditional (
remove-redundant-if)
| foriinrange(4): | ||
| mat.append([0] *4) | ||
| mat= [[0] *4for_inrange(4)] |
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 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]ormat[i][j]==mat[i][j+1]): | ||
| ifmat[i][j]in [mat[i+1][j], mat[i][j+1]]: | ||
| return'GAME NOT OVER' | ||
| forjinrange(3): | ||
| if(mat[3][j]==mat[3][j+1]): | ||
| return'GAME NOT OVER' | ||
| foriinrange(3): | ||
| if(mat[i][3]==mat[i+1][3]): | ||
| return'GAME NOT OVER' | ||
| # else we have lost the game | ||
| return'LOST' | ||
| returnnext( | ||
| ('GAME NOT OVER'foriinrange(3) if (mat[i][3] ==mat[i+1][3])), | ||
| 'LOST', | ||
| ) |
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 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 | ||
| foriinrange(4): | ||
| new_mat.append([0] *4) | ||
| new_mat= [[0] *4for_inrange(4)] |
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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") | ||
| withopen(fileName, "r") asfileHandle: | ||
| # 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. | ||
| forcharinfileHandle.read(): | ||
| ifcharinvowels: | ||
| 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. | ||
| forcharinfileHandle.read(): | ||
| ifcharinvowels: | ||
| count=count+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.
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 | ifmin_nodeisNone: | ||
| ifmin_nodeisNoneorvisited[node] <visited[min_node]: | ||
| min_node=node | ||
| elifvisited[node] <visited[min_node]: | ||
| min_node=node | ||
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | forjinrange(3): | ||
| globals()[i+str(j+1)].configure(text=f' ') | ||
| globals()[i+str(j+1)].configure(text=' ') |
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 Retry refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring)
| ifXcount>Ocount: | ||
| string='Player 1(X) won' | ||
| else: | ||
| string='Player 2(O) won' | ||
| string='Player 1(X) won'ifXcount>Ocountelse'Player 2(O) won' | ||
| finish(string) | ||
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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"andlevel!="easy"): | ||
| iflevelnotin ["hard", "easy"]: |
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 24-24 refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| foriinrange(len(word)): | ||
| for_inrange(len(word)): | ||
| blank_list+="_" | ||
| end_game=False | ||
| whileend_game==False: | ||
| whilenotend_game: |
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 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) iflen(str(hour)) >1else'0'+str(hour)#adds 0 if the hours,minutes or seconds are only one charachter long | ||
| second=str(second) iflen(str(second)) >1else'0'+str(second) | ||
| minute=str(minute) iflen(str(minute)) >1else'0'+str(minute) | ||
| hour=str(hour) iflen(str(hour)) >1elsef'0{str(hour)}' | ||
| second=str(second) iflen(str(second)) >1elsef'0{str(second)}' | ||
| minute=str(minute) iflen(str(minute)) >1elsef'0{str(minute)}' |
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 3-87 refactored with the following changes:
- Replace unused for index with underscore [×2] (
for-index-underscore)
| foriinrange(0,nr_letters): | ||
| for_inrange(0,nr_letters): | ||
| random_letter=r.choice(letters) | ||
| random_letters.append(random_letter) | ||
| random_symbols= [] | ||
| foriinrange(0,nr_symbols): | ||
| for_inrange(0,nr_symbols): | ||
| random_symbol=r.choice(symbols) | ||
| random_symbols.append(random_symbol) | ||
| random_numbers= [] | ||
| foriinrange(0,nr_numbers): | ||
| for_inrange(0,nr_numbers): |
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 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 | ||
| whilenotvalid_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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Human.get_move refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| defget_move(self, game): | ||
| square=random.choice(game.remaining_moves()) | ||
| returnsquare | ||
| returnrandom.choice(game.remaining_moves()) |
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 RandomComputer.get_move refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| iflen(game.remaining_moves()) ==9: | ||
| square=random.choice(game.remaining_moves()) | ||
| else: | ||
| square=self.minimax(game, self.player)['position'] | ||
| returnsquare | ||
| return ( | ||
| random.choice(game.remaining_moves()) | ||
| iflen(game.remaining_moves()) ==9 | ||
| elseself.minimax(game, self.player)['position'] | ||
| ) |
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 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)
| ifplayer==max_player: | ||
| ifsim_score['score'] >best['score']: | ||
| best=sim_score | ||
| else: | ||
| ifsim_score['score'] <best['score']: | ||
| best=sim_score | ||
| if ( | ||
| player==max_player | ||
| andsim_score['score'] >best['score'] | ||
| orplayer!=max_player | ||
| andsim_score['score'] <best['score'] | ||
| ): | ||
| best=sim_score |
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 SmartComputer.minimax refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks)
| whileTrue: | ||
| window.update() | ||
| #moving the ball | ||
| ball.setx(ball.xcor()+ballxdirection) | ||
| ball.sety(ball.ycor()+ballxdirection) | ||
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 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. | iflen(self.arr) >0: | ||
| ditem=self.arr[0] | ||
| delself.arr[0] | ||
| returnditem | ||
| else: | ||
| iflen(self.arr) <=0: | ||
| return#queue is empty | ||
| ditem=self.arr[0] | ||
| delself.arr[0] | ||
| returnditem |
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 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): | ||
| whiley: |
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 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!=0anduserInput!=1anduserInput!=2): | ||
| ifuserInputnotin [0, 1, 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.
Lines 5-5 refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| #hiding segments of snake | ||
| forsegmentinsegments: | ||
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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") | ||
| foriinrange(0,n): | ||
| for_inrange(0,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 userInput refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| forcharinname: | ||
| ifchar.isupper(): | ||
| print("_"+char.lower(), end="") | ||
| print(f"_{char.lower()}", 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.
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}') | ||
| whilenum!=1: | ||
| print(num) | ||
| ifnum%2==0: | ||
| num=int(num/2) | ||
| else: | ||
| num=int(3*num+1) | ||
| num=int(num/2) ifnum%2==0elseint(3*num+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 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 | ||
| foriinrange(lengthOfItems): | ||
| file.write(str(str(rd.randint(minNum,maxNum)) +",")) | ||
| file.close() | ||
| maxNum=1000 | ||
| lengthOfItems=500 | ||
| withopen("newFile.csv", "a") asfile: | ||
| for_inrange(lengthOfItems): | ||
| file.write(str(f"{str(rd.randint(minNum, maxNum))},")) |
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 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!