Modulo cycles players = Alice Bob Carol Alice Bob [OK]
Hint: Use modulo to cycle through players repeatedly [OK]
Common Mistakes:
Not using modulo causes index errors
Assuming players list is longer than turns
Confusing player order in output
4. Identify the bug in this code snippet for initializing a variable-sized board and multiple players:
def setup_game(n, players):
board = [[None]*n]*n
for p in players:
print(f"Player: {p}")
return board
setup_game(3, ['A', 'B'])
medium
A. The board rows are references to the same list, causing shared updates.
B. The players list is not printed correctly.
C. The function does not return anything.
D. The board size is fixed to 3 regardless of input.
Solution
Step 1: Analyze board initialization
Using [[None]*n]*n creates rows that reference the same list object.
Step 2: Understand impact of shared references
Changing one cell affects all rows because they share the same inner list.
Final Answer:
The board rows are references to the same list, causing shared updates. -> Option A
Quick Check:
Shared inner lists cause bugs = The board rows are references to the same list, causing shared updates. [OK]
Hint: Avoid list multiplication for nested lists [OK]
Common Mistakes:
Ignoring shared reference problem
Thinking players print is incorrect
Assuming function returns nothing
5. You are designing a turn-based game with an NxN board and support for multiple players. Which design approach best supports easy extensibility for future features like variable board sizes, more players, and custom rules?
hard
A. Write all game logic in one large function for simplicity.
B. Use fixed-size arrays and hardcoded player count with separate functions for each board size.
C. Use flexible data structures (lists/dictionaries), modular functions, and validate inputs dynamically.
D. Use global variables for board and players to avoid passing parameters.
Solution
Step 1: Identify extensibility requirements
Extensibility needs flexible data structures and modular code to adapt easily.
Step 2: Evaluate design options
Use flexible data structures (lists/dictionaries), modular functions, and validate inputs dynamically. uses lists/dictionaries and modular functions with input validation, supporting future changes well.
Final Answer:
Use flexible data structures (lists/dictionaries), modular functions, and validate inputs dynamically. -> Option C
Quick Check:
Modular + flexible data = extensible design [OK]
Hint: Modular code + flexible data = easy extensibility [OK]