Answered step by step
Verified Expert Solution
Question
1 Approved Answer
from _ _ future _ _ import annotations import python _ ta from python _ ta . contracts import check _ contracts ############################################################################### # TODO
from future import annotations
import pythonta
from pythontacontracts import checkcontracts
###############################################################################
# TODO Task :
# Add at least two doctest examples to each helper below, then
# implement each helper function according to its docstring.
###############################################################################
@checkcontracts
def withingridcoord: tupleint int n: int bool:
Return whether is within an nbyn grid
Preconditions:
n
TODO Task : add at least two doctests here and implement this function
# TODO: Implement this function
@checkcontracts
def allwithingridcoords: listtupleint int n: int bool:
Return whether every coordinate in is within an nbyn grid.
Preconditions:
n
TODO Task : add at least two doctests here and implement this function
# TODO: Implement this function
@checkcontracts
def reflectverticallycoord: tupleint int n: int tupleint int:
Return the coordinate that is but reflected across the middle
horizontal of an nbyn grid. See the handout and supplemental materials
for a diagram showing an example.
Preconditions:
n
withingridcoord n
TODO Task : add at least two doctests here and implement this function
# TODO: Implement this function
@checkcontracts
def reflectpointsline: listtupleint int
n: int listtupleint int:
Return the given reflected vertically across the middle horizontal
of an nbyn grid.
Preconditions:
n
allwithingridline n
TODO Task : add at least two doctests here and implement this function
# TODO: Implement this function
@checkcontracts
class Square:
A class representing a single square in a FourinaRow 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 XO
coord and coord
symbol: None str
coord: tupleint int
def initself coord: tupleint 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
sqsymbol is None
True
sq SquareX
sqsymbol
X
sqcoord
self.symbol s
self.coord coord
def strself 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
printSquare
printSquareX
X
if self.symbol is not None:
return self.symbol
else:
return
###############################################################################
# TODO Task :
# Line Class and related helpers
# For each of the three public helper functions below,
# write at least two pytests in testapy then implement isdiagonal.
# Once these tests are passing, see the Line class for the rest of Task
###############################################################################
@checkcontracts
def isrowsquares: listSquare 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 from the previous square.
Preconditions:
lensquares
l Square Square Square Square
isrowl
True
notl Square Square Square Square
isrownotl
False
currow, curcol squarescoord
for square in squares::
if square.coord currow or square.coord curcol :
return False
curcol square.coord
return True
@checkcontracts
def iscolumnsquares: listSquare 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started