Can someone help me code the questions in python? Thankyou.
question here
Task 1: Initial setup (5 marks) Write the function readBoard(fileName) which takes as input the name of a file in which the board is stored and returns positivesColumn, negativesColumn, positives Row , negativesRow , orientations, and workingBoard as shown in Example 1 in the previous section. workingBoard represents a partial solution to the board as a list of lists (see Example 3 in Part A – Task 2), where each square contains one of the following characters ‘+’,’–, ‘X’, and ‘E’. ‘E’ represents the empty squares and ‘X’ represents blank (non-magnetic) blocks. We were unable to transcribe this imageTask 2: Display (10 marks) Extend your program to print a board on the screen using the function printBoard(positivesColumn, negativesColumn, positives Row , negativesRow , orientations, workingBoard). This function prints the content of the workingBoard to the screen except it will print a blank space for any square that contains ‘E’. The numbers in positives Column, negativesColumn, positivesRow and negatives Row are also printed if they are not equal to -1. An example of working Board and the expected output of printBoard is given below. Your board visualisation must follow the formatting of the example below: M = 6 # number of rows N = 5 # number of columns positives Column = [3, 1, 2, -1, -1] negativesColumn = [-1, -1, -1, -1, 2] positives Row = [-1, 1, -1, -1, 2, 2] negativesRow = [-1, 2, 1, -1, 2, 2] orientations = [[‘L’, ‘R’, ‘L’, ‘R’, ‘T’, [‘T’, ‘T’, ‘L’, ‘R’, ‘B’], [‘B’, ‘B’, ‘L’, ‘R’, ‘T’], [‘T’, ‘T’, ‘I’, ‘R’, ‘B’], [‘B’, ‘B’, ‘L’, ‘R’, ‘T’], [‘L’, ‘R’, ‘e’, ‘R’, ‘B’]] workingBoard = [[‘+’, ‘-‘, ‘X’, ‘X’, ‘E’], [‘X’, ‘X’, ‘E’, ‘E’, ‘E’], [‘X’, ‘X’, ‘E’, ‘E’, ‘+’], [‘t’, ‘E’, ‘X’, ‘X’, ‘-‘], [‘-‘, ‘E’, ‘E’, ‘E’, ‘X’], [‘+’, ‘a’, ‘+’, ‘a’, ‘X’]] printBoard (positives Column, negativesColumn, positives Row , negatives Row , orientations, workingBoard) + m 3 1 2 + X XI – X N Similar to X + | 3 | 1 | 2 | | | — — —|—|—|— — I + – | X X | | — — — —|—| |— 1 | X | xL L 12 —| D |—|—|—|— | X | X | + 11 — — — —|—| |— + L | X X | – | — —|— — — 2 | – | X 12 — — — —|—| |— 2 + – + – | X 2 — — — —|—|—|— L L L | 2 | – + N I N + T Similarly, you can use the task 1’s readBoard function to achieve the same result: positivesColumn, negativesColumn, positives Row , negativesRow , orientations, workingBoard = readBoard (“sampleFilel.txt”) printBoard (positivesColumn, negativesColumn, positiveshow , negativesRow , orientations, workingBoard) + | 3 | 1 | 2 | L | —|— —|—|—|—|— | + – | X XI I — — — — — — 1 | X | XL L 12 —| D |—|—|—|— | X | X | + 11 — —|—|—|—| |— | + L | X X | – | —| D |— —|— — 2 | – | L | X 12 — — —|—|—| |— 2 | + – | + – | X 12 —|—|—|—|—|—|— L L L | 2 | – Part B: Helper functions (25 marks) In this part, you will code several helper functions that you will need later. Task 1: Safe placing (10 marks) Write the function canPlacePole(row, col, pole, workingBoard) to check if it is safe to place one given magnetic block’s pole for a particular coordinates. This function checks the surrounding squares and it will return a Boolean value. (See Figures 3 and 4 for more details.) row and col represent the coordinates of a given pole. pole is a single character representation for the given polarity (i.e. ‘+’ or ‘–) of one magnetic block’s end. If the surrounding squares in workingBoard allow the placement of the given pole of a magnetic block then the function will return True otherwise it will return False. See the examples below: + 3 ‘+’, workingBoard)) N >>> print (canPlace Pole (1, 1, False Example 4 – Example use of canPlacePole function for an existing workingBoard, positive (+) pole, row=1 and col=1 >>> print (canPlacePole (2, 3, ‘-‘, workingBoard)) True Task 2: Block orientation (5 marks) Write the function getBlockOrientation(row, col, orientations) to get the orientation of a given slot as well as the coordinates of the other end of the slot, based on the given coordinates of a square and the orientations. row and col represent the coordinates of a given square. orientations, represents a list of lists with all pre- arranged slots on the board. The function will return “TB” for top-bottom (vertical) blocks and “LR” for left- right (horizontal) blocks, it will also return the coordinates of the opposite end for that block. It must follow this exact order: resultOrientation, oppositeRow, and oppositeCol. Note: The response for the slot’s orientation can only be “TB” or “LR”. >>> resultorientation, oppositeRow, oppositecol = getBlockOrientation (1, 1, orientations) + 2 2 Response Square >>> print (resultorientation, oppositeRow, oppositeCol) = TB’ 0 1 Original Square Task 3: Pole Count (5 marks) Write the function poleCount(rowOrCol, index, pole, workingBoard) to find the total number of positive (+) poles or negative (-) poles in a particular row or column. rowOrCol can only be ‘r’ or ‘c’ to indicate whether to check a given row or column, index indicates the index number for the row or column that function will check. Note: it must starts from zero. pole can only be ‘+’orto indicate which magnetic pole the function will be looking for; and consistent as before workingBoard represents a partial solution to the board as a list of lists. Example behaviour of this function is demonstrated below: + | 3 | 1 | 2 —— —|——— | + – | X XI I ———|—|— — 1 | x | xL L 12 —| |—|—|— |— | X | X | + |1 ———|—|— — | + | X X | – —| | |—|——— 2 | – | L | X 12 ————|— — 2 | + – | + – | X 12 ——|——|——— L L L L | 2 | – >>> workingBoard = [[‘+’, ‘-‘, ‘X’, ‘X’, ‘E’], [‘X’, ‘X’, ‘E’, ‘E’, ‘E’], [‘X’, ‘X’, ‘E’, ‘E’, ‘+’], [‘t’, ‘E’, ‘X’, ‘X’, ‘-‘], [‘-‘, ‘E’, ‘E’, ‘E’, ‘X’], [‘t’, ‘-‘, ‘+’, ‘-‘, ‘X’] ] >>> print (poleCount (‘r’,5,’+’, workingBoard)) >>> print (poleCount (‘r’,4,’-‘, workingBoard)) >>> print (poleCount (‘c’,4,’-‘, workingBoard)) Task 4: Random Magnetic Pole Distribution (5 marks) Write the function randomPoleFlip(alist, percentage, flipValue) to randomly swaps a percentage of elements from alist (which can represent one of positivesColumn, negativesColumn, positivesRow, or negativesRow) with the flipValue. The number of elements that needs to be flipped is defined by the floor of percentage times the length of the aList. alist=[1,2,3,4,5,6,7,8,9,10] randomPoleFlip (alist,0.5,-1). print (alist) [1, -1, -1, 4, 5, 6, -1, -1, 9, -1] Example 8 – In the example above, 50% of the elements are flipped to -1. alist=[1,2,3,4,5,6,7,8,9,10] random PoleFlip (alist,0.35, -1) print (alist) [-1, 2, -1, 4, 5, 6, 7, 8, 9, -1] Show transcribed image text Task 1: Initial setup (5 marks) Write the function readBoard(fileName) which takes as input the name of a file in which the board is stored and returns positivesColumn, negativesColumn, positives Row , negativesRow , orientations, and workingBoard as shown in Example 1 in the previous section. workingBoard represents a partial solution to the board as a list of lists (see Example 3 in Part A – Task 2), where each square contains one of the following characters ‘+’,’–, ‘X’, and ‘E’. ‘E’ represents the empty squares and ‘X’ represents blank (non-magnetic) blocks.
Task 2: Display (10 marks) Extend your program to print a board on the screen using the function printBoard(positivesColumn, negativesColumn, positives Row , negativesRow , orientations, workingBoard). This function prints the content of the workingBoard to the screen except it will print a blank space for any square that contains ‘E’. The numbers in positives Column, negativesColumn, positivesRow and negatives Row are also printed if they are not equal to -1. An example of working Board and the expected output of printBoard is given below. Your board visualisation must follow the formatting of the example below: M = 6 # number of rows N = 5 # number of columns positives Column = [3, 1, 2, -1, -1] negativesColumn = [-1, -1, -1, -1, 2] positives Row = [-1, 1, -1, -1, 2, 2] negativesRow = [-1, 2, 1, -1, 2, 2] orientations = [[‘L’, ‘R’, ‘L’, ‘R’, ‘T’, [‘T’, ‘T’, ‘L’, ‘R’, ‘B’], [‘B’, ‘B’, ‘L’, ‘R’, ‘T’], [‘T’, ‘T’, ‘I’, ‘R’, ‘B’], [‘B’, ‘B’, ‘L’, ‘R’, ‘T’], [‘L’, ‘R’, ‘e’, ‘R’, ‘B’]]
workingBoard = [[‘+’, ‘-‘, ‘X’, ‘X’, ‘E’], [‘X’, ‘X’, ‘E’, ‘E’, ‘E’], [‘X’, ‘X’, ‘E’, ‘E’, ‘+’], [‘t’, ‘E’, ‘X’, ‘X’, ‘-‘], [‘-‘, ‘E’, ‘E’, ‘E’, ‘X’], [‘+’, ‘a’, ‘+’, ‘a’, ‘X’]] printBoard (positives Column, negativesColumn, positives Row , negatives Row , orientations, workingBoard) + m 3 1 2 + X XI – X N Similar to X + | 3 | 1 | 2 | | | — — —|—|—|— — I + – | X X | | — — — —|—| |— 1 | X | xL L 12 —| D |—|—|—|— | X | X | + 11 — — — —|—| |— + L | X X | – | — —|— — — 2 | – | X 12 — — — —|—| |— 2 + – + – | X 2 — — — —|—|—|— L L L | 2 | – + N I N + T
Similarly, you can use the task 1’s readBoard function to achieve the same result: positivesColumn, negativesColumn, positives Row , negativesRow , orientations, workingBoard = readBoard (“sampleFilel.txt”) printBoard (positivesColumn, negativesColumn, positiveshow , negativesRow , orientations, workingBoard) + | 3 | 1 | 2 | L | —|— —|—|—|—|— | + – | X XI I — — — — — — 1 | X | XL L 12 —| D |—|—|—|— | X | X | + 11 — —|—|—|—| |— | + L | X X | – | —| D |— —|— — 2 | – | L | X 12 — — —|—|—| |— 2 | + – | + – | X 12 —|—|—|—|—|—|— L L L | 2 | –
Part B: Helper functions (25 marks) In this part, you will code several helper functions that you will need later. Task 1: Safe placing (10 marks) Write the function canPlacePole(row, col, pole, workingBoard) to check if it is safe to place one given magnetic block’s pole for a particular coordinates. This function checks the surrounding squares and it will return a Boolean value. (See Figures 3 and 4 for more details.) row and col represent the coordinates of a given pole. pole is a single character representation for the given polarity (i.e. ‘+’ or ‘–) of one magnetic block’s end. If the surrounding squares in workingBoard allow the placement of the given pole of a magnetic block then the function will return True otherwise it will return False. See the examples below: + 3 ‘+’, workingBoard)) N >>> print (canPlace Pole (1, 1, False Example 4 – Example use of canPlacePole function for an existing workingBoard, positive (+) pole, row=1 and col=1 >>> print (canPlacePole (2, 3, ‘-‘, workingBoard)) True
Task 2: Block orientation (5 marks) Write the function getBlockOrientation(row, col, orientations) to get the orientation of a given slot as well as the coordinates of the other end of the slot, based on the given coordinates of a square and the orientations. row and col represent the coordinates of a given square. orientations, represents a list of lists with all pre- arranged slots on the board. The function will return “TB” for top-bottom (vertical) blocks and “LR” for left- right (horizontal) blocks, it will also return the coordinates of the opposite end for that block. It must follow this exact order: resultOrientation, oppositeRow, and oppositeCol. Note: The response for the slot’s orientation can only be “TB” or “LR”. >>> resultorientation, oppositeRow, oppositecol = getBlockOrientation (1, 1, orientations) + 2 2 Response Square >>> print (resultorientation, oppositeRow, oppositeCol) = TB’ 0 1 Original Square
Task 3: Pole Count (5 marks) Write the function poleCount(rowOrCol, index, pole, workingBoard) to find the total number of positive (+) poles or negative (-) poles in a particular row or column. rowOrCol can only be ‘r’ or ‘c’ to indicate whether to check a given row or column, index indicates the index number for the row or column that function will check. Note: it must starts from zero. pole can only be ‘+’orto indicate which magnetic pole the function will be looking for; and consistent as before workingBoard represents a partial solution to the board as a list of lists. Example behaviour of this function is demonstrated below: + | 3 | 1 | 2 —— —|——— | + – | X XI I ———|—|— — 1 | x | xL L 12 —| |—|—|— |— | X | X | + |1 ———|—|— — | + | X X | – —| | |—|——— 2 | – | L | X 12 ————|— — 2 | + – | + – | X 12 ——|——|——— L L L L | 2 | – >>> workingBoard = [[‘+’, ‘-‘, ‘X’, ‘X’, ‘E’], [‘X’, ‘X’, ‘E’, ‘E’, ‘E’], [‘X’, ‘X’, ‘E’, ‘E’, ‘+’], [‘t’, ‘E’, ‘X’, ‘X’, ‘-‘], [‘-‘, ‘E’, ‘E’, ‘E’, ‘X’], [‘t’, ‘-‘, ‘+’, ‘-‘, ‘X’] ] >>> print (poleCount (‘r’,5,’+’, workingBoard)) >>> print (poleCount (‘r’,4,’-‘, workingBoard)) >>> print (poleCount (‘c’,4,’-‘, workingBoard))
Task 4: Random Magnetic Pole Distribution (5 marks) Write the function randomPoleFlip(alist, percentage, flipValue) to randomly swaps a percentage of elements from alist (which can represent one of positivesColumn, negativesColumn, positivesRow, or negativesRow) with the flipValue. The number of elements that needs to be flipped is defined by the floor of percentage times the length of the aList. alist=[1,2,3,4,5,6,7,8,9,10] randomPoleFlip (alist,0.5,-1). print (alist) [1, -1, -1, 4, 5, 6, -1, -1, 9, -1] Example 8 – In the example above, 50% of the elements are flipped to -1. alist=[1,2,3,4,5,6,7,8,9,10] random PoleFlip (alist,0.35, -1) print (alist) [-1, 2, -1, 4, 5, 6, 7, 8, 9, -1]
Expert Answer
Answer to Task 1: Initial setup (5 marks) Write the function readBoard(fileName) which takes as input the name of a file in which …