Skip to content

Commit 71d0267

Browse files
Sourcery AIamjadkofahi
authored andcommitted
'Refactored by Sourcery'
1 parent 7c1e336 commit 71d0267

File tree

70 files changed

+583
-836
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+583
-836
lines changed

02_Amazing/python/amazing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def display(self) -> None:
4747
print(" ", end="")
4848
print()
4949
for col in range(self.width):
50-
if self.walls[row][col] == 0 or self.walls[row][col] == 2:
50+
if self.walls[row][col] in [0, 2]:
5151
print(":--", end="")
5252
else:
5353
print(": ", end="")
@@ -148,7 +148,7 @@ def make_opening(
148148
maze.walls[pos.row][pos.col] = maze.walls[pos.row][pos.col] + EXIT_DOWN
149149
pos.row = pos.row + 1
150150
maze.used[pos.row][pos.col] = count
151-
count = count + 1
151+
count += 1
152152
return pos, count
153153

154154

03_Animal/python/animal.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,13 @@ def parse_input(message: str, check_list: bool, root_node: Optional[Node]) -> st
104104
list_known_animals(root_node)
105105
print("\n")
106106

107-
if len(inp) > 0:
108-
token = inp[0].lower()
109-
else:
110-
token = ""
111-
107+
token = inp[0].lower() if len(inp) > 0 else ""
112108
return token
113109

114110

115111
def avoid_void_input(message: str) -> str:
116112
answer = ""
117-
while answer == "":
113+
while not answer:
118114
answer = input(message)
119115
return answer
120116

@@ -134,8 +130,12 @@ def main() -> None:
134130

135131
# Main loop of game
136132
print_intro()
137-
keep_playing = parse_input("Are you thinking of an animal? ", True, root) == "y"
138-
while keep_playing:
133+
while (
134+
keep_playing := parse_input(
135+
"Are you thinking of an animal? ", True, root
136+
)
137+
== "y"
138+
):
139139
keep_asking = True
140140
# Start traversing the tree by the root
141141
actual_node: Node = root
@@ -170,17 +170,13 @@ def main() -> None:
170170
f"for a {new_animal} the answer would be: ", False, None
171171
)
172172

173-
actual_node.update_node(
174-
new_question + "?", answer_new_question, new_animal
175-
)
173+
actual_node.update_node(f"{new_question}?", answer_new_question, new_animal)
176174

177175
else:
178176
print("Why not try another animal?")
179177

180178
keep_asking = False
181179

182-
keep_playing = parse_input("Are you thinking of an animal? ", True, root) == "y"
183-
184180

185181
########################################################
186182
# Porting Notes

04_Awari/python/awari.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,10 @@
8383

8484
def draw_pit(line: str, board, pit_index) -> str:
8585
val = board[pit_index]
86-
line = line + " "
86+
line += " "
8787
if val < 10:
88-
line = line + " "
89-
line = line + str(val) + " "
90-
return line
88+
line += " "
89+
return line + str(val) + " "
9190

9291

9392
def draw_board(board) -> None:
@@ -148,7 +147,7 @@ def play_game(board: List[int]) -> None:
148147
print(msg)
149148
break
150149
if landing_spot == home:
151-
landing_spot, is_still_going, home, msg = computer_move(msg + " , ", board)
150+
landing_spot, is_still_going, home, msg = computer_move(f"{msg} , ", board)
152151
if not is_still_going:
153152
print(msg)
154153
break
@@ -248,7 +247,7 @@ def computer_move(msg: str, board) -> Tuple[int, bool, int, str]:
248247

249248
move_str = chr(42 + selected_move)
250249
if msg:
251-
msg += ", " + move_str
250+
msg += f", {move_str}"
252251
else:
253252
msg = move_str
254253

@@ -323,10 +322,7 @@ def execute_move(move, home: int, board) -> Tuple[int, bool, int]:
323322
# losses.
324323
losing_book[game_number] = losing_book[game_number] * 6 + move_digit
325324

326-
if player_has_stones(board) and computer_has_stones(board):
327-
is_still_going = True
328-
else:
329-
is_still_going = False
325+
is_still_going = bool(player_has_stones(board) and computer_has_stones(board))
330326
return last_location, is_still_going, home
331327

332328

05_Bagels/python/bagels.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ def pick_number() -> List[str]:
4949
# as separate strings, not a single integer or string
5050
numbers = list(range(10))
5151
random.shuffle(numbers)
52-
num = numbers[0:3]
53-
num_str = [str(i) for i in num]
54-
return num_str
52+
num = numbers[:3]
53+
return [str(i) for i in num]
5554

5655

5756
def get_valid_guess(guesses: int) -> str:

06_Banner/python/banner.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ def print_banner() -> None:
7474

7575
except ValueError:
7676
print("Please enter a number greater than zero")
77-
g1 = 0
78-
if input("Centered ").lower().startswith("y"):
79-
g1 = 1
77+
g1 = 1 if input("Centered ").lower().startswith("y") else 0
8078
character = input(
8179
"Character (type 'ALL' if you want character being printed) "
8280
).upper()
@@ -87,7 +85,7 @@ def print_banner() -> None:
8785
for statement_char in statement:
8886
s = letters[statement_char].copy()
8987
x_str = character
90-
if character == "ALL":
88+
if x_str == "ALL":
9189
x_str = statement_char
9290
if x_str == " ":
9391
print("\n" * (7 * horizontal))

07_Basketball/python/basketball.py

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -122,32 +122,30 @@ def dartmouth_jump_shot(self) -> None:
122122
if random.random() > 0.782 * self.defense / 8:
123123
if random.random() > 0.843 * self.defense / 8:
124124
print("Charging foul. Dartmouth loses ball.\n")
125-
self.opponent_ball()
126125
else:
127126
# player is fouled
128127
self.foul_shots(1)
129-
self.opponent_ball()
128+
self.opponent_ball()
129+
elif random.random() > 0.5:
130+
print(
131+
"Shot is blocked. Ball controlled by "
132+
+ self.opponent
133+
+ ".\n"
134+
)
135+
self.opponent_ball()
130136
else:
131-
if random.random() > 0.5:
132-
print(
133-
"Shot is blocked. Ball controlled by "
134-
+ self.opponent
135-
+ ".\n"
136-
)
137-
self.opponent_ball()
138-
else:
139-
print("Shot is blocked. Ball controlled by Dartmouth.")
140-
self.dartmouth_ball()
137+
print("Shot is blocked. Ball controlled by Dartmouth.")
138+
self.dartmouth_ball()
141139
else:
142140
print("Shot is off target.")
143141
if self.defense / 6 * random.random() > 0.45:
144-
print("Rebound to " + self.opponent + "\n")
142+
print(f"Rebound to {self.opponent}" + "\n")
145143
self.opponent_ball()
146144
else:
147145
print("Dartmouth controls the rebound.")
148146
if random.random() > 0.4:
149147
if self.defense == 6 and random.random() > 0.6:
150-
print("Pass stolen by " + self.opponent + ", easy lay up")
148+
print(f"Pass stolen by {self.opponent}, easy lay up")
151149
self.add_points(0, 2)
152150
self.dartmouth_ball()
153151
else:
@@ -186,13 +184,11 @@ def dartmouth_non_jump_shot(self) -> None:
186184
if 7 / self.defense * random.random() > 0.875:
187185
if 7 / self.defense * random.random() > 0.925:
188186
print("Charging foul. Dartmouth loses the ball.\n")
189-
self.opponent_ball()
190187
else:
191-
print("Shot blocked. " + self.opponent + "'s ball.\n")
192-
self.opponent_ball()
188+
print(f"Shot blocked. {self.opponent}" + "'s ball.\n")
193189
else:
194190
self.foul_shots(1)
195-
self.opponent_ball()
191+
self.opponent_ball()
196192
else:
197193
print("Shot is off the rim.")
198194
if random.random() > 2 / 3:
@@ -216,35 +212,35 @@ def dartmouth_ball(self) -> None:
216212
self.shot = shot
217213

218214
if self.time < 100 or random.random() < 0.5:
219-
if self.shot == 1 or self.shot == 2:
215+
if self.shot in [1, 2]:
220216
self.dartmouth_jump_shot()
221217
else:
222218
self.dartmouth_non_jump_shot()
219+
elif self.score[0] == self.score[1]:
220+
print("\n ***** End Of Second Half *****")
221+
print("Score at end of regulation time:")
222+
print(
223+
" Dartmouth: "
224+
+ str(self.score[1])
225+
+ " "
226+
+ self.opponent
227+
+ ": "
228+
+ str(self.score[0])
229+
)
230+
print("Begin two minute overtime period")
231+
self.time = 93
232+
self.start_of_period()
233+
223234
else:
224-
if self.score[0] != self.score[1]:
225-
print("\n ***** End Of Game *****")
226-
print(
227-
"Final Score: Dartmouth: "
228-
+ str(self.score[1])
229-
+ " "
230-
+ self.opponent
231-
+ ": "
232-
+ str(self.score[0])
233-
)
234-
else:
235-
print("\n ***** End Of Second Half *****")
236-
print("Score at end of regulation time:")
237-
print(
238-
" Dartmouth: "
239-
+ str(self.score[1])
240-
+ " "
241-
+ self.opponent
242-
+ ": "
243-
+ str(self.score[0])
244-
)
245-
print("Begin two minute overtime period")
246-
self.time = 93
247-
self.start_of_period()
235+
print("\n ***** End Of Game *****")
236+
print(
237+
"Final Score: Dartmouth: "
238+
+ str(self.score[1])
239+
+ " "
240+
+ self.opponent
241+
+ ": "
242+
+ str(self.score[0])
243+
)
248244

249245
def opponent_jumpshot(self) -> None:
250246
"""Simulate the opponents jumpshot"""
@@ -253,32 +249,35 @@ def opponent_jumpshot(self) -> None:
253249
if 8 / self.defense * random.random() > 0.75:
254250
if 8 / self.defense * random.random() > 0.9:
255251
print("Offensive foul. Dartmouth's ball.\n")
256-
self.dartmouth_ball()
257252
else:
258253
self.foul_shots(0)
259-
self.dartmouth_ball()
254+
self.dartmouth_ball()
260255
else:
261256
print("Shot is off the rim.")
262257
if self.defense / 6 * random.random() > 0.5:
263-
print(self.opponent + " controls the rebound.")
264-
if self.defense == 6:
265-
if random.random() > 0.75:
266-
print("Ball stolen. Easy lay up for Dartmouth.")
267-
self.add_points(1, 2)
268-
self.opponent_ball()
269-
else:
270-
if random.random() > 0.5:
271-
print()
272-
self.opponent_non_jumpshot()
273-
else:
274-
print("Pass back to " + self.opponent + " guard.\n")
275-
self.opponent_ball()
258+
print(f"{self.opponent} controls the rebound.")
259+
if (
260+
self.defense == 6
261+
and random.random() <= 0.75
262+
and random.random() > 0.5
263+
):
264+
print()
265+
self.opponent_non_jumpshot()
266+
elif (
267+
self.defense == 6
268+
and random.random() <= 0.75
269+
and random.random() <= 0.5
270+
or self.defense != 6
271+
and random.random() <= 0.5
272+
):
273+
print(f"Pass back to {self.opponent}" + " guard.\n")
274+
self.opponent_ball()
275+
elif self.defense == 6 and random.random() > 0.75:
276+
print("Ball stolen. Easy lay up for Dartmouth.")
277+
self.add_points(1, 2)
278+
self.opponent_ball()
276279
else:
277-
if random.random() > 0.5:
278-
self.opponent_non_jumpshot()
279-
else:
280-
print("Pass back to " + self.opponent + " guard.\n")
281-
self.opponent_ball()
280+
self.opponent_non_jumpshot()
282281
else:
283282
print("Dartmouth controls the rebound.\n")
284283
self.dartmouth_ball()
@@ -296,26 +295,30 @@ def opponent_non_jumpshot(self) -> None:
296295
if 7 / self.defense * random.random() > 0.413:
297296
print("Shot is missed.")
298297
if self.defense / 6 * random.random() > 0.5:
299-
print(self.opponent + " controls the rebound.")
300-
if self.defense == 6:
301-
if random.random() > 0.75:
302-
print("Ball stolen. Easy lay up for Dartmouth.")
303-
self.add_points(1, 2)
304-
self.opponent_ball()
305-
else:
306-
if random.random() > 0.5:
307-
print()
308-
self.opponent_non_jumpshot()
309-
else:
310-
print("Pass back to " + self.opponent + " guard.\n")
311-
self.opponent_ball()
298+
print(f"{self.opponent} controls the rebound.")
299+
if (
300+
self.defense == 6
301+
and random.random() <= 0.75
302+
and random.random() > 0.5
303+
or self.defense != 6
304+
and random.random() > 0.5
305+
):
306+
print()
307+
self.opponent_non_jumpshot()
308+
elif (
309+
self.defense == 6
310+
and random.random() <= 0.75
311+
and random.random() <= 0.5
312+
):
313+
print(f"Pass back to {self.opponent}" + " guard.\n")
314+
self.opponent_ball()
315+
elif self.defense == 6 and random.random() > 0.75:
316+
print("Ball stolen. Easy lay up for Dartmouth.")
317+
self.add_points(1, 2)
318+
self.opponent_ball()
312319
else:
313-
if random.random() > 0.5:
314-
print()
315-
self.opponent_non_jumpshot()
316-
else:
317-
print("Pass back to " + self.opponent + " guard\n")
318-
self.opponent_ball()
320+
print(f"Pass back to {self.opponent}" + " guard\n")
321+
self.opponent_ball()
319322
else:
320323
print("Dartmouth controls the rebound.\n")
321324
self.dartmouth_ball()

08_Batnum/python/batnum.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,8 @@ def computer_pick(
153153
q = pile_size - 1 if win_option == WinOptions.AvoidLast else pile_size
154154
c = min_select + max_select
155155
computer_pick = q - (c * int(q / c))
156-
if computer_pick < min_select:
157-
computer_pick = min_select
158-
if computer_pick > max_select:
159-
computer_pick = max_select
160-
return computer_pick
156+
computer_pick = max(computer_pick, min_select)
157+
return min(computer_pick, max_select)
161158

162159

163160
def computer_move(
@@ -184,7 +181,7 @@ def computer_move(
184181

185182
# Otherwise, we determine how many the computer selects
186183
curr_sel = computer_pick(pile_size, min_select, max_select, win_option)
187-
pile_size = pile_size - curr_sel
184+
pile_size -= curr_sel
188185
print(f"COMPUTER TAKES {curr_sel} AND LEAVES {pile_size}")
189186
return (False, pile_size)
190187

0 commit comments

Comments
 (0)