Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

from _ _ future _ _ import annotations import python _ ta from python _ ta . contracts import check _ contracts ############################################################################### # TODO

from __future__ import annotations
import python_ta
from python_ta.contracts import check_contracts
###############################################################################
# TODO Task 1:
# Add at least two doctest examples to each helper below, then
# implement each helper function according to its docstring.
###############################################################################
@check_contracts
def within_grid(coord: tuple[int, int], n: int)-> bool:
"""
Return whether is within an n-by-n grid
Preconditions:
- n >0
TODO Task 1: add at least two doctests here and implement this function
"""
# TODO: Implement this function
@check_contracts
def all_within_grid(coords: list[tuple[int, int]], n: int)-> bool:
"""
Return whether every coordinate in is within an n-by-n grid.
Preconditions:
- n >0
TODO Task 1: add at least two doctests here and implement this function
"""
# TODO: Implement this function
@check_contracts
def reflect_vertically(coord: tuple[int, int], n: int)-> tuple[int, int]:
"""
Return the coordinate that is , but reflected across the middle
horizontal of an n-by-n grid. See the handout and supplemental materials
for a diagram showing an example.
Preconditions:
- n >0
- within_grid(coord, n)
TODO Task 1: add at least two doctests here and implement this function
"""
# TODO: Implement this function
@check_contracts
def reflect_points(line: list[tuple[int, int]],
n: int)-> list[tuple[int, int]]:
"""
Return the given reflected vertically across the middle horizontal
of an n-by-n grid.
Preconditions:
- n >0
- all_within_grid(line, n)
TODO Task 1: add at least two doctests here and implement this function
"""
# TODO: Implement this function
@check_contracts
class Square:
"""
A class representing a single square in a Four-in-a-Row game.
Attributes:
- symbol: the symbol indicating which player, if any, has played here. Note,
the strings 'X' and 'O' are used as the symbols of the players.
- coord: the (row, column) coordinate indicating this square's location in
the grid.
Representation Invariants:
- self.symbol is None or self.symbol in ('X','O')
- coord[0]>=0 and coord[1]>=0
"""
symbol: None | str
coord: tuple[int, int]
def __init__(self, coord: tuple[int, int], s: None | str = None)-> None:
"""
Initialize this Square with symbol and coordinate .
Note: parameter has a defualt parameter value of None specified for
this method. This means that if we only provide , then
will automatically have a value of None (see example below).
>>> sq = Square((0,0))
>>> sq.symbol is None
True
>>> sq = Square((0,1),'X')
>>> sq.symbol
'X'
>>> sq.coord
(0,1)
"""
self.symbol = s
self.coord = coord
def __str__(self)-> str:
"""
Return a suitable string representation of this Square.
This method will determine how our Square class is represented as a
string, when we use either str or print (see below for an example).
>>> print(Square((0,0)))
-
>>> print(Square((0,1),'X'))
X
"""
if self.symbol is not None:
return self.symbol
else:
return '-'
###############################################################################
# TODO Task 2:
# Line Class and related helpers
# For each of the three public helper functions below,
# write at least two pytests in test_a0.py, then implement _is_diagonal.
# Once these tests are passing, see the Line class for the rest of Task 2.
###############################################################################
@check_contracts
def is_row(squares: list[Square])-> bool:
"""
Return whether is a valid row or not.
A line is a valid row if all of its row coordinates are the same, and
the column coordinates all increase by exactly 1 from the previous square.
Preconditions:
- len(squares)>3
>>> l =[Square((0,1)), Square((0,2)), Square((0,3)), Square((0,4))]
>>> is_row(l)
True
>>> not_l =[Square((0,1)), Square((0,2)), Square((0,4)), Square((0,3))]
>>> is_row(not_l)
False
"""
cur_row, cur_col = squares[0].coord
for square in squares[1:]:
if square.coord[0]!= cur_row or square.coord[1]- cur_col !=1:
return False
cur_col = square.coord[1]
return True
@check_contracts
def is_column(squares: list[Square])-> bool:
"""
Return whether is a valid column or not.
A line is a valid column if all of its column coordinates are the same, an

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain in detail how the Mughal Empire was established in India

Answered: 1 week ago

Question

Problem: Evaluate the integral: I - -[ze dx

Answered: 1 week ago

Question

Problem: Evaluate the integral: I = 1- 1 dx 9

Answered: 1 week ago