You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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...
341
340
342
341
__copyable__
342
+
def diagonal_winner(board):
343
+
...
344
+
343
345
assert_equal(
344
346
diagonal_winner(
345
347
[
@@ -362,6 +364,7 @@ def diagonal_winner(board):
362
364
False
363
365
)
364
366
367
+
In the first example, `X` won in the diagonal going from the bottom left to the top right.
365
368
"""
366
369
367
370
hints="""
@@ -375,10 +378,11 @@ def diagonal_winner(board):
375
378
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.
376
379
Do you remember negative indexing? It might be helpful here.
377
380
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.
380
382
"""
381
383
384
+
parsons_solution=True
385
+
382
386
defsolution(self):
383
387
defdiagonal_winner(board: List[List[str]]):
384
388
all_equal1=True
@@ -442,7 +446,10 @@ class winner(ExerciseStep):
442
446
Now we can put the three functions together! Write a function `winner` that takes an argument `board` as before,
443
447
and returns `True` if `board` contains either a winning row, column or diagonal, `False` otherwise.
444
448
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`.
446
453
Click the Copy button, and fill in the blanks for your `winner` function.
447
454
448
455
__copyable__
@@ -524,6 +531,14 @@ def diagonal_winner(board):
524
531
How can you use the three functions and a boolean operator together to get the result you need?
0 commit comments