Question
This is a follow-up of a previous set of questions. The solution to the previous set has been given below. This question has to be
This is a follow-up of a previous set of questions. The solution to the previous set has been given below. This question has to be answer basing on the answers from the previous set. Please solve them in python language for me. Thank you.
Please note: The whole thing is just one question and the rest of the base code has been noted with the next question at https://www.chegg.com/homework-help/questions-and-answers/question-follows-https-wwwcheggcom-homework-help-questions-answers-follow-previous-set-que-q43828154?trackid=F8BYKJiv
***The code at the end is not the solution. Those are the base codes basing on which the codes for the following questions are to be answered.***
**Please take your time***
# Part A: Representation and display (15 marks)
# Part A - Task 1 - Initial setup (5 marks) def readBoard(fileName): M = 0 N = 0 positivesColumn = [] negativesColumn = [] positivesRow = [] negativesRow = [] orientations = [] workingBoard = []
file = open(fileName) fileContent = []
for line in file: fileContent.append(line.strip())
M = int(fileContent[0]) # line 1 in file N = int(fileContent[1]) # line 2 in file
positivesColumn = convertStringListToIntList( fileContent[2].split()) # line 3 in file negativesColumn = convertStringListToIntList( fileContent[3].split()) # line 4 in file positivesRow = convertStringListToIntList( fileContent[4].split()) # line 5 in file negativesRow = convertStringListToIntList( fileContent[5].split()) # line 6 in file
# next M lines after line 6 for counter in range(M): orientations.append(list(str(fileContent[counter+6])))
# remaining M lines in file for counter in range(M): workingBoard.append(list(str(fileContent[counter+M+6]))) file.close() return (positivesColumn, negativesColumn, positivesRow, negativesRow, orientations, workingBoard)
def convertStringListToIntList(lst): for item in range(len(lst)): lst[item] = int(lst[item]) return lst
# Part A - Task 2 - Display (10 marks) def printBoard(positivesColumn, negativesColumn, positivesRow, negativesRow, orientations, workingBoard): M = len(orientations) N = len(orientations[0])
# print the positivesColumn -> the top positive print(" + |",end="") for rowCounter in range(N): if positivesColumn[rowCounter]>=0: print(" " + str(positivesColumn[rowCounter])+ " ",end="") elif positivesColumn[rowCounter]==-1: # -1 to empty square print(" ", end="") print("|",end="") print("")
# print the following line print("--- "*(N+2))
# print the workingBoard one row at a time for rowCounter in range(M):
#print the positivesRow first if positivesRow[rowCounter]>=0: print(" " + str(positivesRow[rowCounter]) + " ", end="") elif positivesRow[rowCounter]==-1: print(" ", end="") print("|",end="")
#print the workingBoard Row for colCounter in range(N): #if E print blank square if (workingBoard[rowCounter][colCounter]=='E'): print(" ", end="") else: print (" " + workingBoard[rowCounter][colCounter] + " ", end="") #Check orientations board if current magnet is horizontal or vertical #if horizontal if (orientations[rowCounter][colCounter]=='L'): print(" ",end="") else: #if vertical print("|",end="")
#print the negatives row if negativesRow[rowCounter]>=0: print(" " + str(negativesRow[rowCounter]) + " ", end="") elif negativesRow[rowCounter]==-1: print(" ", end="")
print("") print("--- ",end="") # print line below row check orientation for colCounter in range(N): if (orientations[rowCounter][colCounter]!='T'): print("--- ",end="") else: #if vertical print(" ",end="") print("--- ",end="") print("") # print the negativeColumn at the bottom print(" |", end="") for rowCounter in range(len(negativesColumn)): if negativesColumn[rowCounter] >= 0: print(" " + str(negativesColumn[rowCounter]) + " ", end="") else: print(" ", end="") print("|",end="") print(" - ") print("") return
Part A: Helper functions (10 marks) This task involves writing some new helper functions, that are useful to use with brute force and backtracking algorithms. Task 1: Converting a set into a board (5 Marks) Write a function setToBoard(set, orientations) that takes a given set list and converts it into a board based on existing orientations. This function will return the resulting board as the output. set is a list of values that can only be 0, 1, or 2. The length of set is equivalent to the number of blocks that will be converted. Orepresents: O For an LR block: A positive pole ('+') in the left (L') square and therefore negative pole (-) in the right ('R') square. o For a TB block: A positive pole ('+') in the top ('T') square and therefore negative pole (-4) in the bottom ('B') square. 1 represents: o For an LR block: A negative pole (-) in the left (L') square and therefore positive pole ('+') in the right ('R') square. o For a TB block: A negative pole (-) in the top ('T') square and therefore positive pole ('+') in the bottom ('B') square. 2 represents: O Blank block ('X') squares for both squares of any block orientation. Important Notes: The function will start from the top left square find the required number of blocks that it will need to change based on the length of set. The len (set) the top positive print(" + 1", end="") for rowCounter in range (N): if positivesColumn[rowCounter] >=0: print(" " + str (positivesColumn[rowCounter]) + " ", end="") elif positivesColumn [rowCounter)==-1: # -1 to empty square print(" ", end="") print("/", end="") print("") # print the following line print("--- "* (N+2)) # print the workingBoard one row at a time for rowCounter in range (M): #print the positivesRow first if positivesRow [rowCounter] >=0: print(" " + str (positivesRow [rowCounter]) + " ", end="") elif positives Row [rowCounter)==-1: print(" ", end="") print("/", end="") #print the workingBoard Row for colCounter in range (N): #if E print blank square if (workingBoard[rowCounter] [colCounter]=='E'): print(" ", end="") else: print (" " + workingBoard (rowCounter] [colCounter] + " ", end="") #Check orientations board if current magnet is horizontal or vertical #if horizontal if (orientations[rowCounter] [colCounter]=='L'): print(" ", end="") else: #if vertical print("/", end="") #print the negatives row if negativesRow [rowCounter] >=0: print(" " + str (negativesRow [rowCounter]) + " ", end="") elif negativesRow [rowCounter)==-1: print(" ", end="") print("") print("--- ", end="") # print line below row check orientation for colCounter in range (N): if (orientations (rowCounter] [colCounter] !='T'): print("--- ", end="") else: #if vertical print(" ", end="") print("--- ", end="") print("") # print the negativeColumn at the bottom print(" l", end="") for rowCounter in range (len (negativesColumn)) : if negativesColumn [rowCounter] >= 0: print(" " + str(negativesColumn [rowCounter]) + " ", end="") else: print(" ", end="") print("l", end="") print(" - ") print("") return #Part B: Helper functions (25 marks) #Part B - Task 1 - Safe placing (10 marks) def canPlace Pole (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 0 and workingBoard[row][col - 1] == pole) or (col 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+l #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]) or (index > N-1 and rowOrCol == 'C')): if ((index >M-1 and rowOrCol == 'r') return None poleCounter=0 if str(rowOrCol).lower() =='r': for iterator in range (N): if workingBoard[index][iterator]== pole: poleCounter+=l 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 (5 marks) 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) 0 and orientations (randomRow] [randomCol] == orientations (randomRow-1] [randomCol] and getBlockOrientation (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] and getBlockOrientation (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 3: Generating random new board (10 marks) def randomNewBoard (M,N): # 1. Generate the orientations board of size M x N randomly. (1 mark) # Note: Use orientations Generator function for this. orientations = orientations Generator (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 with a # blank block is based on a random chance for each block and repeated # until 30% of board is replaced. Other end of a checked block will be ignored. blockCount = M * N / 2 requiredReplacements = math.ceil(0.3 * blockCount) replacement Chance=0.2 replacement Count=0 while (replacement CountStep 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