In python Decode a checkerboard Define a function named decoder_damier that accepts as argument a checkerboard represented as a string, and returns a tuple (m,n,k) where m and n are respectively the number of rows and columns of the checkerboard, and k is a dictionary of the tokens that are on this checkerboard. A token is defined as any character other than '.', the latter symbolizing an empty checkerboard. The returned dictionary must contain key associations: value where the key is a couple (i,j) Row and column indices of the block of the token with a value symbol. For example, the following expression: decoder_damier('..A....... n.B........ nC......... n.......... n') must return the following tuple: (4, 10, {(0, 2): 'A', (1, 1): 'B', (2, 0): 'C'}) That is, a checkerboard of 4 rows by 10 columns, with tokens A, B and C respectively in boxes (0.2) , (1.1) and (2.0). Note that your function must not make any display, only return the requested tuple. You can assume that the string received as argument contains no white space, that the lines are separated by n and that they all have exactly the same number of characters. You can also assume that m 1 and n 1 . To solve this problem, we suggest you: Using str.splitlines. Initialize an empty dictionary for tokens. Loop on the checkerboard lines (think of enumerate). Loop on the characters of each line. If a character differs from '.', then add it to the token dictionary. Return the tuple requested.
In python
Decode a checkerboard
Define a function named decoder_damier that accepts as argument a checkerboard represented as a string, and returns a tuple (m,n,k) where m and n are respectively the number of rows and columns of the checkerboard, and k is a dictionary of the tokens that are on this checkerboard. A token is defined as any character other than '.', the latter symbolizing an empty checkerboard. The returned dictionary must contain key associations: value where the key is a couple (i,j) Row and column indices of the block of the token with a value symbol.
For example, the following expression:
decoder_damier('..A....... n.B........ nC......... n.......... n')
must return the following tuple:
(4, 10, {(0, 2): 'A', (1, 1): 'B', (2, 0): 'C'})
That is, a checkerboard of 4 rows by 10 columns, with tokens A, B and C respectively in boxes (0.2) , (1.1) and (2.0).
Note that your function must not make any display, only return the requested tuple. You can assume that the string received as argument contains no white space, that the lines are separated by n and that they all have exactly the same number of characters. You can also assume that m 1 and n 1 .
To solve this problem, we suggest you:
Using str.splitlines.
Initialize an empty dictionary for tokens.
Loop on the checkerboard lines (think of enumerate).
Loop on the characters of each line.
If a character differs from '.', then add it to the token dictionary.
Return the tuple requested.
Description:
Define the function decoder_damier as follows:
- Initialize an empty tuple and dictionary.
- Use the splitlines function to split the input string str based on the new line character and store it in a list l.
- Set row and column values to the length of l and length of l[0] respectively.
- Use a for loop to iterate over each value in l:
- Set x to l[i].
- Use a for loop to iterate over each character in x:
- If the current character is not '.', add the character to the dictionary such that the key is its exact position and the value is the character.
- Add the values of the row, column, and dict to the resultant tuple.
- Return the resultant tuple.
Step by step
Solved in 3 steps with 1 images