Question Follows Https Wwwcheggcom Homework Help Questions Answers Follow Previous Set Que Q43828154
This question followshttps://www.chegg.com/homework-help/questions-and-answers/follow-previous-set-questions-solution-previous-set-given–question-answer-basing-answers–q43822416?trackid=iQs8HVEM
Thank you
Could you please answer the first question first ifnot answered yet. Thank you.
#Part B: Helper functions (25 marks)
#Part B – Task 1 – Safe placing (10 marks)
def canPlacePole(row, col, pole, workingBoard):
M= len (workingBoard)
N= len (workingBoard[0])
if (row>M-1 or col>N-1):
return False
# check surrounding squares (diagonals are not checked )
if ((row > 0 and workingBoard[row – 1][col] == pole) or (row< M – 1 and workingBoard[row + 1][col] == pole) or
(col > 0 and workingBoard[row][col – 1] == pole)or(col < N -1 and workingBoard[row][col + 1] == pole)):
return False
return True
#Part B – Task 2 – Block orientation (5 marks)
def getBlockOrientation(row, col, orientations):
M= len (orientations)
N= len (orientations[0])
if (row>M-1 or col>N-1):
return None
#if TB (Top Bottom/Vertical) orientation
#if Top return Bottom coordinates
if (orientations[row][col] == ‘T’):
return “TB”,row+1,col
#else if Bottom return Top coordinates
elif (orientations[row][col] == ‘B’):
return “TB”,row-1,col
#else if Left return Right coordinates
elif (orientations[row][col] == ‘L’):
return “LR”,row,col+1
#else if Right return Left coordinates
elif (orientations[row][col] == ‘R’):
return “LR”,row,col-1
else:
return None
#Part B – Task 3 – Pole Count (5 marks)
def poleCount(rowOrCol, index, pole, workingBoard):
M= len (workingBoard)
N= len (workingBoard[0])
if ((index >M-1 and rowOrCol == ‘r’) or (index > N-1 androwOrCol == ‘c’)):
return None
poleCounter=0
if str(rowOrCol).lower() ==’r’:
for iterator in range(N):
if workingBoard[index][iterator]== pole:
poleCounter+=1
elif str(rowOrCol).lower() == ‘c’:
for iterator in range(M):
if workingBoard[iterator][index]== pole:
poleCounter+=1
return poleCounter
#Part B – Task 4 – Random Magnetic Pole Distribution (5marks)
def randomPoleFlip(aList, percentage, flipValue):
listLength = len(aList)
noOfFlips = math.floor(listLength*percentage)
#Keep repeating until desired number of flips is fulfilled
while aList.count(flipValue) < noOfFlips:
randomIndex= random.randint(0,listLength-1)
if aList[randomIndex]!=flipValue:
aList[randomIndex] = flipValue
# Part C: Board Generation Functions (30 marks)
# Part C – Task 1: Orientations generation (10 marks)
def orientationsGenerator(M,N):
# Generating a new M by N board with all orientations set asvertical blocks
# Note: be mindful of copy
orientations=[[”]*N]*M
for row in range(0,M-1,2):
orientations[row]=[‘T’]*N
orientations[row+1]=[‘B’]*N
# Repeat the process at least 1000 times
for repeat in range(1000):
# Picking random block
randomRow = random.randint(0,M-1)
randomCol = random.randint(0,N-1)
otherEndRow = getBlockOrientation(randomRow, randomCol,orientations)[1]
otherEndCol = getBlockOrientation(randomRow, randomCol,orientations)[2]
# If it is a LR block, check the block that is immediately aboveit.
# If it is also an LR block and it is perfectly aligned with thepicked
# block then change both blocks to become TB blocks.
# If it is not possible to do this with the top block, then trywith the bottom block.
if randomRow>0 and orientations[randomRow][randomCol] ==orientations[randomRow-1][randomCol] andgetBlockOrientation(randomRow, randomCol, orientations)[0] ==’LR’:
#Change bottom block to ‘B’s
orientations[randomRow][randomCol]=’B’
#other end
orientations[otherEndRow][otherEndCol]=’B’
#Change top block to ‘T’s
orientations[randomRow-1][randomCol]=’T’
orientations[otherEndRow-1][otherEndCol]=’T’
elif randomCol>0 and orientations[randomRow][randomCol] ==orientations[randomRow][randomCol-1] andgetBlockOrientation(randomRow, randomCol, orientations)[0] ==’TB’:
#Change right block to ‘R’s
orientations[randomRow][randomCol]=’R’
#other end
orientations[otherEndRow][otherEndCol]=’R’
#Change left block to ‘L’s
orientations[randomRow][randomCol-1]=’L’
orientations[otherEndRow][otherEndCol-1]=’L’
return orientations
# Part C – Task 2: Filling board with magnets (10 marks)
def fillWithMagnets(orientations):
M= len (orientations)
N= len (orientations[0])
# Create a new M by N empty workingBoard
# be mindful of copying
workingBoard=[[”] * N for i in range(M)]
for rowCounter in range(M):
for colCounter in range(N):
if (canPlacePole(rowCounter,colCounter,’+’,workingBoard)):
workingBoard[rowCounter][colCounter]=’+’
else:
workingBoard[rowCounter][colCounter]=’-‘
return workingBoard
# Part C – Task 3: Generating random new board (10 marks)
def randomNewBoard (M,N):
# 1. Generate the orientations board of size M x N randomly. (1mark)
# Note: use orientationsGenerator function for this.
orientations = orientationsGenerator(M, N)
# 2. Fill the board with magnet blocks. (1 mark)
# Note: use fillWithMagnets function for this.
workingBoard = fillWithMagnets(orientations)
# 3. Randomly replace 30% of the blocks with blank blocks (‘X’blocks) (3 marks)
# Cover the board from top to bottom and the decision to swap witha
# blank block is based on a random chance for each block andrepeated
# until 30% of board is replaced. Other end of a checked block willbe ignored.
blockCount = M * N / 2
requiredReplacements = math.ceil(0.3 * blockCount)
replacementChance=0.2
replacementCount=0
while (replacementCount for rowCounter in range(M):
for colCounter in range(N):
rand = random.random()
#blockOrientation = getBlockOrientation(rowCounter, colCounter,orientations)[0] # unused
otherEndRow = getBlockOrientation(rowCounter, colCounter,orientations)[1]
otherEndCol = getBlockOrientation(rowCounter, colCounter,orientations)[2]
if (otherEndRow > rowCounter or otherEndCol >colCounter):
if (rand<=replacementChance and replacementCountworkingBoard[rowCounter][colCounter]=’X’
workingBoard[otherEndRow][otherEndCol]=’X’
replacementCount+=1
# 4. Generate the resulting positivesColumn, negativesColumn,positivesRow, and negativesRow lists. (3 marks)
# Note: use poleCount function for this.
positivesColumn=[]
negativesColumn=[]
positivesRow=[]
negativesRow=[]
for counter in range(N):
positivesColumn.append(poleCount(‘c’,counter,’+’,workingBoard))
negativesColumn.append(poleCount(‘c’,counter,’-‘,workingBoard))
for counter in range(M):
positivesRow.append( poleCount(‘r’,counter,’+’,workingBoard))
negativesRow.append( poleCount(‘r’,counter,’-‘,workingBoard))
# 5. Replace 50% of the numbers in each of positivesColumn,negativesColumn, positivesRow, and
# negativesRow lists with -1. (2 marks)
# Note: use randomPoleFlip function for this.
randomPoleFlip(positivesColumn,0.5,-1)
randomPoleFlip(negativesColumn,0.5,-1)
randomPoleFlip(positivesRow,0.5,-1)
randomPoleFlip(negativesRow,0.5,-1)
return (positivesColumn, negativesColumn, positivesRow,negativesRow, workingBoard, orientations)
Task 2: Check solution (5 marks) Write the function is Solution(positives Column, negativesColumn, positives Row, negativesRow, orientations, workingBoard) that checks if a given board is a valid solution to the puzzle. The function will return Boolean value True for valid solutions; otherwise, it will return false. The function should check the following: The number of positive poles (+) and negative poles (-)in each row and column of the board must match positivesColumn, negativesColumn, positivesRow, and negativesRow The given board does not violate the orthogonal rule i.e. the magnetic poles are not vertically or horizontally adjacent. Example behaviour of this function is demonstrate below: positives Column = [1,1,1) negativesColumn = [1,1,1] positives Row = (-1,-1] negatives Row = (-1,-1] orientations = [[‘L’, ‘R’, ‘T’], [‘1’, ‘R’, ‘B’]] workingBoard = [[‘+’, ‘-‘, ‘-‘), [‘-‘, ‘+’, ‘+’]] printBoard (positivesColumn, negativesColumn, positivesRow, negatives Row, orientations, workingBoard) print (issolution (positivesColumn, negatives Column, positives Row, negativesRow, orientations, workingBoard)) workingBoard = [[‘+’, ‘-‘, ‘+’], [‘-‘, ‘t’, ‘-‘]] printBoard (positivesColumn, negativesColumn, positives Row, negativesRow, orientations, workingBoard) print(isSolution (positivesColumn, negativesColumn, positives Row, negativesRow, orientations, workingBoard)) + 1 1 1 1 1 —|—|—|— — | + – | – | —|—|—| |— | – + + | —|—|—|—|— | 1 | 1 | 1 | – False (because it violates the orthogonal rule, see column three) + | 1 | 1 | 1 | — — — — — + – + L —|— — — – + | – —1—1—1—1— | 1 | 1 | 1 | – True Example 2 – example use of is Solution function FisSolution function positives Column = [1,1,-1] negativesColumn = [1,1,-1] positives Row = [1,-1] negatives Row = (-1,-1] orientations = [[‘e’, ‘R’, ‘T’], [‘L’, ‘R’, ‘B’]] workingBoard = [[‘+’, ”, ‘+’], [‘-‘, ‘t’, ‘-‘]] printBoard (positivesColumn, negativesColumn, positivesRow, negativesRow, orientations, workingBoard) print (isSolution (positives Column, negativesColumn, positives Row, negativesRow, orientations, workingBoard)) workingBoard = [[‘+’, [‘-‘, ‘-‘, ‘X’], ‘+’, ‘X’]] printBoard (positives Column, negativesColumn, positives Row, negativesRow, orientations, workingBoard) print (issolution (positivesColumn, negativesColumn, positives Row, negativesRow, orientations, workingBoard)) + | 1 | 11 —|—|—|—|— i I + – + —|—— — | – + | – | —|—|——— 11|11- False (because the first row has 2 ‘+’ instead of 1) + | 1 | 1 —|—|—|—|— 1 | + – | X —|— — — | – + | X | —|—|—|— — | 1 | 11 | – True Example 3 – example use of issolution function Show transcribed image text Task 2: Check solution (5 marks) Write the function is Solution(positives Column, negativesColumn, positives Row, negativesRow, orientations, workingBoard) that checks if a given board is a valid solution to the puzzle. The function will return Boolean value True for valid solutions; otherwise, it will return false. The function should check the following:
The number of positive poles (+) and negative poles (-)in each row and column of the board must match positivesColumn, negativesColumn, positivesRow, and negativesRow The given board does not violate the orthogonal rule i.e. the magnetic poles are not vertically or horizontally adjacent. Example behaviour of this function is demonstrate below: positives Column = [1,1,1) negativesColumn = [1,1,1] positives Row = (-1,-1] negatives Row = (-1,-1] orientations = [[‘L’, ‘R’, ‘T’], [‘1’, ‘R’, ‘B’]] workingBoard = [[‘+’, ‘-‘, ‘-‘), [‘-‘, ‘+’, ‘+’]] printBoard (positivesColumn, negativesColumn, positivesRow, negatives Row, orientations, workingBoard) print (issolution (positivesColumn, negatives Column, positives Row, negativesRow, orientations, workingBoard)) workingBoard = [[‘+’, ‘-‘, ‘+’], [‘-‘, ‘t’, ‘-‘]] printBoard (positivesColumn, negativesColumn, positives Row, negativesRow, orientations, workingBoard) print(isSolution (positivesColumn, negativesColumn, positives Row, negativesRow, orientations, workingBoard)) + 1 1 1 1 1 —|—|—|— — | + – | – | —|—|—| |— | – + + | —|—|—|—|— | 1 | 1 | 1 | – False (because it violates the orthogonal rule, see column three) + | 1 | 1 | 1 | — — — — — + – + L —|— — — – + | – —1—1—1—1— | 1 | 1 | 1 | – True Example 2 – example use of is Solution function FisSolution function positives Column = [1,1,-1] negativesColumn = [1,1,-1] positives Row = [1,-1] negatives Row = (-1,-1] orientations = [[‘e’, ‘R’, ‘T’], [‘L’, ‘R’, ‘B’]]
workingBoard = [[‘+’, ”, ‘+’], [‘-‘, ‘t’, ‘-‘]] printBoard (positivesColumn, negativesColumn, positivesRow, negativesRow, orientations, workingBoard) print (isSolution (positives Column, negativesColumn, positives Row, negativesRow, orientations, workingBoard)) workingBoard = [[‘+’, [‘-‘, ‘-‘, ‘X’], ‘+’, ‘X’]] printBoard (positives Column, negativesColumn, positives Row, negativesRow, orientations, workingBoard) print (issolution (positivesColumn, negativesColumn, positives Row, negativesRow, orientations, workingBoard)) + | 1 | 11 —|—|—|—|— i I + – + —|—— — | – + | – | —|—|——— 11|11- False (because the first row has 2 ‘+’ instead of 1) + | 1 | 1 —|—|—|—|— 1 | + – | X —|— — — | – + | X | —|—|—|— — | 1 | 11 | – True Example 3 – example use of issolution function
Expert Answer
Answer to This question follows https://www.chegg.com/homework-help/questions-and-answers/follow-previous-set-questions-solution-p…
OR