Answered step by step
Verified Expert Solution
Question
1 Approved Answer
my code is shown below however it has some errors the following is the description of the exercise and the test cases where I have
my code is shown below however it has some errors
the following is the description of the exercise and the test cases where I have some issues in
based on this can you please rectify my code and can you give me a code which implements this
return self, size def s ste ( self) : oreating the geid padding : 1 \# Cceating a yariablo with a space assigned so that it acts as a padding to the poss that have a single digtt heading =1,,, ' + join(letters[:self,size]) \# Alphabetical heading 1 s created Lines = [heading] \# adding the alphabetical heading into a 11 ist named lines to which the pows will be added later for r,row in enumerate(self,grid): if len(self,grid) ="+f{ self.size r}+, join(self,points [x] for in row) Lines.append(line) else: \# for the grids that are larger than 9 if r>9: \# for rows 1 to 9 the single digits are aligned acconding to the first digit from the pioht of the tro digit rous else: Line =f{ self.size r+, join(self.points [x] for x in row) Line = padding + line \# adding the space using the variable padding to the row created Lines.append(line) \# adding the row to the list of rows else: \# for the rows 10 onwards - as there is no requirement to add a padding it is not added here if (self,from_strings): else: from string import aseit-upperease as uetters class Board: \#Dictionary created for the colours and the renspected symbols points={E:,B:,W:0} \#Constructor === Outside the class, define a function Load_board(filename) that takes as an argument the name of a text file, reads a Go position from that file and returns a Board object representing the position. The file must contain one line for each row of the board, using the characters ".", "@", and "0" as above to represent the colours. Define a function save board (filename, board) that takes as arguments a filename and a Board object and saves the position to a text file in the same format as that accepted by load_board(). should print and create a file l19b.txt which is identical to l19.txt. In class Board, define two new methods: - to_integer(self) returns an integer encoding of the position - set_from_integer(self, integer_encoding) sets the colours of all points of the board according to the given integer_encoding. This does not change the size of the board! The integer encoding of a board position is based on the ternary number system. The ternary system uses the digits 0,1 , and 2 to represent numbers as follows. If d0,d1,,dn{0,1,2} are ternary digits, then dn3n+dn13n1++d030 is the integer represented by the sequence (dndn1d0)3 of digits. For example, (21)3=23+1=7 and (210)3=29+1. 3+0=21 If we replace the colours of the points of the Go board by digits (empty =0, white =1, black =2 ) and concatenate all rows of the board to a single row of digits, we get a ternary number that can then be converted to an integer. The method to integer (self) is supposed to do exactly that. For example, the 33 board used in the example for Task 3 looked like this: ABC30.01e.0. Reading this off as ternary digits gives us (101020210)3, which equals 7473 in decimal. Reading this off as ternary digits gives us (101020210)3, which equals 7473 in decimal. Hint: To convert an integer n to ternary representation, as required by set_from_integer (), note that you get the last digit by computing nmod3. You get the next digit by computing n/3mod3 and so on. For example, here's how to get the last two digits of the ternary representation of 7473: 7473mod3=0. 7473/3=2491.2491mod3=1. Expand Example Executing the following code should print 7473 20816819938197998469947863334486277028652245388453054842563945682092741961273801537852564845 A B C 3 (a. 2 . 0. 10 . ABCDE 5 ..... 4 .... 3 .... 2 .... 1 ... 0 In the Board class, define a method fill_reaching(self, colour_name, reach_name, visited, r, c) that receives the following parameters: - colour_name : the name of the colour ("E", "B", or "W") of the chain to be marked - reach_name : the name of a colour to be tested for reachability - visited : a matrix (list of lists) of Booleans of th same size as the board - r,c : the row and column number indexed from 0, i.e. 0,0 is the top left corner The method executes the flood fill algorithm, starting from the position given by r and c, to mark all points of colour colour_name that are connected to the starting position by a path of colour colour_name moving only vertically and horizontally. The method modifies the matrix visited: marking a point is defined as setting the corresponding element in visited to True. Some elements of visited may be True to begin with - the corresponding grid points are treated as if they were of a colour different from colour_name. The method must not modify the Board object. i You have implemented the flood fill algorithm in W10.3 Post-Class. This is a variation of that algorithm that uses a different matrix to mark visited points and also returns whether reach_name was touched at any time 11 visited =[[F alse ]3 for i in range (3)] 22e = b.fill_reaching("W", "E", visited, ,) 13print(e) 14 print(visited) should print ABC3@0.20001.0. True [[False, True, False], [True, True, True], [False, True, False]] False [[True, False, False], [False, False, False], [False, False, False]] False [[False, False, False], [False, False, False], [False, False, False]] In addition to marking all visited points, the method returns True if the starting point reaches the colour specified in reach_name (see rule \#3 in the background information) and False otherwise. Expand Example Executing the following code 1 b = Board(3, ["e0.", "O00", ".O."]) 2 print(b) 3 visited = [[False] * 3 for i in range(3)] 4 e = b.fill_reaching("W", "E", visited, 1, 1) 5 print(e) 6 print(visited) 7 visited = [[False] * 3 for i in range(3)] 8 e = b.fill_reaching ("B", "E", visited, 0,0) 9 print(e) 10 print(visited) 11 visited = [[False] * 3 for i in range(3)] 12 e = b.fill_reaching("W", "E", visited, 0,0) 13 print(e) 14 print(visited) In the Board class, define a method reaching_empty_matrix (self) that returns a matrix (list of lists) of Booleans of the same size as the board. An element of this matrix is True if the corresponding point on the board is either empty or reaches empty and false otherwise. The method must not modify the board. [[True, True, True], [True, True, True], [True, True, True]] [[True, False, True], [True, True, True], [True, True, True]] A B C DEFGHIJKLMNOPQRS 19 ... @@. 00 .@. 00000 17 ( 0..@0.00000.000000 e 16 ..@@. 00 . (a.. 0 @ 0 . (a. 0. 150 .@. @. 0 @. 00 @ 0.00 @ 0 140 ... 000 @. @ (a... (a. 0 e 13 . (a) 0 (a.. (a. 000 . @. @.. 12 . @@. (@@... (a 0.0 @.. (a 11 @ 0. (a. (a @ e 0.. 000 e 0 10 @. 0.@@0 @00 @ . (a. 0 @. 9 @ 0000.. 0.. (a 0 @ @ (a. 00 8 @@0 e. 0000 . (a. 0 . (a @ . (a 7 @.0.0 @0 0.00. @ 0 @.. @ 0 6 @... 0 @@ 00 (@ @. 0.0. 4 e. 0.00. (a 0 e 0 e 00 . @a . 3 @@0 0 @. 0 . (a. 0 . 0 e 0 . 2.00 @@ 0 . @ 0.0. @ 00 @. (1,4) (10,12) return self, size def s ste ( self) : oreating the geid padding : 1 \# Cceating a yariablo with a space assigned so that it acts as a padding to the poss that have a single digtt heading =1,,, ' + join(letters[:self,size]) \# Alphabetical heading 1 s created Lines = [heading] \# adding the alphabetical heading into a 11 ist named lines to which the pows will be added later for r,row in enumerate(self,grid): if len(self,grid) ="+f{ self.size r}+, join(self,points [x] for in row) Lines.append(line) else: \# for the grids that are larger than 9 if r>9: \# for rows 1 to 9 the single digits are aligned acconding to the first digit from the pioht of the tro digit rous else: Line =f{ self.size r+, join(self.points [x] for x in row) Line = padding + line \# adding the space using the variable padding to the row created Lines.append(line) \# adding the row to the list of rows else: \# for the rows 10 onwards - as there is no requirement to add a padding it is not added here if (self,from_strings): else: from string import aseit-upperease as uetters class Board: \#Dictionary created for the colours and the renspected symbols points={E:,B:,W:0} \#Constructor === Outside the class, define a function Load_board(filename) that takes as an argument the name of a text file, reads a Go position from that file and returns a Board object representing the position. The file must contain one line for each row of the board, using the characters ".", "@", and "0" as above to represent the colours. Define a function save board (filename, board) that takes as arguments a filename and a Board object and saves the position to a text file in the same format as that accepted by load_board(). should print and create a file l19b.txt which is identical to l19.txt. In class Board, define two new methods: - to_integer(self) returns an integer encoding of the position - set_from_integer(self, integer_encoding) sets the colours of all points of the board according to the given integer_encoding. This does not change the size of the board! The integer encoding of a board position is based on the ternary number system. The ternary system uses the digits 0,1 , and 2 to represent numbers as follows. If d0,d1,,dn{0,1,2} are ternary digits, then dn3n+dn13n1++d030 is the integer represented by the sequence (dndn1d0)3 of digits. For example, (21)3=23+1=7 and (210)3=29+1. 3+0=21 If we replace the colours of the points of the Go board by digits (empty =0, white =1, black =2 ) and concatenate all rows of the board to a single row of digits, we get a ternary number that can then be converted to an integer. The method to integer (self) is supposed to do exactly that. For example, the 33 board used in the example for Task 3 looked like this: ABC30.01e.0. Reading this off as ternary digits gives us (101020210)3, which equals 7473 in decimal. Reading this off as ternary digits gives us (101020210)3, which equals 7473 in decimal. Hint: To convert an integer n to ternary representation, as required by set_from_integer (), note that you get the last digit by computing nmod3. You get the next digit by computing n/3mod3 and so on. For example, here's how to get the last two digits of the ternary representation of 7473: 7473mod3=0. 7473/3=2491.2491mod3=1. Expand Example Executing the following code should print 7473 20816819938197998469947863334486277028652245388453054842563945682092741961273801537852564845 A B C 3 (a. 2 . 0. 10 . ABCDE 5 ..... 4 .... 3 .... 2 .... 1 ... 0 In the Board class, define a method fill_reaching(self, colour_name, reach_name, visited, r, c) that receives the following parameters: - colour_name : the name of the colour ("E", "B", or "W") of the chain to be marked - reach_name : the name of a colour to be tested for reachability - visited : a matrix (list of lists) of Booleans of th same size as the board - r,c : the row and column number indexed from 0, i.e. 0,0 is the top left corner The method executes the flood fill algorithm, starting from the position given by r and c, to mark all points of colour colour_name that are connected to the starting position by a path of colour colour_name moving only vertically and horizontally. The method modifies the matrix visited: marking a point is defined as setting the corresponding element in visited to True. Some elements of visited may be True to begin with - the corresponding grid points are treated as if they were of a colour different from colour_name. The method must not modify the Board object. i You have implemented the flood fill algorithm in W10.3 Post-Class. This is a variation of that algorithm that uses a different matrix to mark visited points and also returns whether reach_name was touched at any time 11 visited =[[F alse ]3 for i in range (3)] 22e = b.fill_reaching("W", "E", visited, ,) 13print(e) 14 print(visited) should print ABC3@0.20001.0. True [[False, True, False], [True, True, True], [False, True, False]] False [[True, False, False], [False, False, False], [False, False, False]] False [[False, False, False], [False, False, False], [False, False, False]] In addition to marking all visited points, the method returns True if the starting point reaches the colour specified in reach_name (see rule \#3 in the background information) and False otherwise. Expand Example Executing the following code 1 b = Board(3, ["e0.", "O00", ".O."]) 2 print(b) 3 visited = [[False] * 3 for i in range(3)] 4 e = b.fill_reaching("W", "E", visited, 1, 1) 5 print(e) 6 print(visited) 7 visited = [[False] * 3 for i in range(3)] 8 e = b.fill_reaching ("B", "E", visited, 0,0) 9 print(e) 10 print(visited) 11 visited = [[False] * 3 for i in range(3)] 12 e = b.fill_reaching("W", "E", visited, 0,0) 13 print(e) 14 print(visited) In the Board class, define a method reaching_empty_matrix (self) that returns a matrix (list of lists) of Booleans of the same size as the board. An element of this matrix is True if the corresponding point on the board is either empty or reaches empty and false otherwise. The method must not modify the board. [[True, True, True], [True, True, True], [True, True, True]] [[True, False, True], [True, True, True], [True, True, True]] A B C DEFGHIJKLMNOPQRS 19 ... @@. 00 .@. 00000 17 ( 0..@0.00000.000000 e 16 ..@@. 00 . (a.. 0 @ 0 . (a. 0. 150 .@. @. 0 @. 00 @ 0.00 @ 0 140 ... 000 @. @ (a... (a. 0 e 13 . (a) 0 (a.. (a. 000 . @. @.. 12 . @@. (@@... (a 0.0 @.. (a 11 @ 0. (a. (a @ e 0.. 000 e 0 10 @. 0.@@0 @00 @ . (a. 0 @. 9 @ 0000.. 0.. (a 0 @ @ (a. 00 8 @@0 e. 0000 . (a. 0 . (a @ . (a 7 @.0.0 @0 0.00. @ 0 @.. @ 0 6 @... 0 @@ 00 (@ @. 0.0. 4 e. 0.00. (a 0 e 0 e 00 . @a . 3 @@0 0 @. 0 . (a. 0 . 0 e 0 . 2.00 @@ 0 . @ 0.0. @ 00 @. (1,4) (10,12)
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