Skip to content

Commit 4528eb1

Browse files
committed
Tweaks to diagonal_winner and winner
1 parent 2bc9954 commit 4528eb1

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

backend/main/chapters/c11_tic_tac_toe_project.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# flake8: NOQA E501
2+
import ast
23
from string import ascii_uppercase
34
from typing import List
45
from random import choice, randint
56

6-
from main.text import ExerciseStep, Page, MessageStep
7+
from main.text import ExerciseStep, Page, MessageStep, Disallowed
78

89

910
class IntroducingTicTacToe(Page):
@@ -335,11 +336,12 @@ def diagonal_winner(board):
335336
(middle == board[0][2] and middle == board[2][0])
336337
)
337338
338-
Now write a `diagonal_winner` that works for *square boards* of arbitrary size.
339-
Your function should work for any square sizes: 4-by-4, 5-by-5, and so on...
340-
Some tests are provided, add more of your own tests:
339+
Now write a `diagonal_winner` that works for square boards of any size: 4-by-4, 5-by-5, and so on...
341340
342341
__copyable__
342+
def diagonal_winner(board):
343+
...
344+
343345
assert_equal(
344346
diagonal_winner(
345347
[
@@ -362,6 +364,7 @@ def diagonal_winner(board):
362364
False
363365
)
364366
367+
In the first example, `X` won in the diagonal going from the bottom left to the top right.
365368
"""
366369

367370
hints = """
@@ -375,10 +378,11 @@ def diagonal_winner(board):
375378
Now focus on the other diagonal (from top right to bottom left). There is a pattern in the subscripts again, but it's a little bit more difficult.
376379
Do you remember negative indexing? It might be helpful here.
377380
Once you get the hang of the patterns, use the same ideas from before to check if all entries are equal.
378-
For the top-left to bottom-right diagonal, you can use the top-left board entry`board[0][0]` to compare all the entries to it. Something similar for the other diagonal...
379-
For each diagonal define a boolean, and use a loop to go through the entries in that diagonal, updating that diagonal's boolean accordingly.
381+
You can use one loop and check both diagonals at the same time. Or you can use one loop for each diagonal.
380382
"""
381383

384+
parsons_solution = True
385+
382386
def solution(self):
383387
def diagonal_winner(board: List[List[str]]):
384388
all_equal1 = True
@@ -442,7 +446,10 @@ class winner(ExerciseStep):
442446
Now we can put the three functions together! Write a function `winner` that takes an argument `board` as before,
443447
and returns `True` if `board` contains either a winning row, column or diagonal, `False` otherwise.
444448
445-
We added the codes of `row_winner`, `column_winner` and `diagonal_winner` functions, along with some tests for `winner` (feel free to add more).
449+
Your solution should work by calling the three functions. `winner` itself should not do any
450+
looping, subscripting, etc.
451+
452+
Here is some code for `row_winner`, `column_winner` and `diagonal_winner`, along with some tests for `winner`.
446453
Click the Copy button, and fill in the blanks for your `winner` function.
447454
448455
__copyable__
@@ -524,6 +531,14 @@ def diagonal_winner(board):
524531
How can you use the three functions and a boolean operator together to get the result you need?
525532
"""
526533

534+
disallowed = Disallowed((ast.For, ast.Subscript), function_only=True, message="""
535+
Your solution should work by calling the three functions. `winner` itself should not do any
536+
looping, subscripting, etc. It should be very short.
537+
538+
Copy the `row_winner` and other functions and leave them as they are. Don't copy code from them
539+
into the `winner` function, just call those functions.
540+
""")
541+
527542
def solution(self):
528543
def row_winner(board):
529544
for row in board:

0 commit comments

Comments
 (0)