Writing string into a matrix seems like a non-interesting and somehow trivial task. Anyway, manipulating with matrices and strings in Python is actually somehow interesting, thus I have decided to present a small algorithmic challenge. The task is pretty much like this:
Input
- The input data should be read from the console. It consists of exactly two lines
- On the first line, you’ll receive the dimensions of the stairs in format: “N M”, where N is the number of rows, and M is the number of columns. They’ll be separated by a single space
- On the second line you’ll receive the string representing a snake, starting down from the top-left corner and slithering its way down. The first cell is filled with the first symbol of the snake, the second cell is filled with the second symbol, etc. The “snake” is as long as it takes in order to fill the box completely – if you reach the end of the string representing the snake, start again at the beginning.
Output
- The output should be printed on the console. It should consist of N lines
- Each line should contain a string representing the respective row of the matrix
Constraints
- The dimensions N and M of the matrix will be integers in the range [1 … 12]
- The snake will be a string with length in the range [1 … 20] and will not contain any whitespace characters
I have decided to use a matrix for the creation of the place, where the “snake” was supposed to be. In Python, this is a beautiful 1-liner with list comprehension:
1 |
matrix = [[None for c in range(columns)] for r in range(rows)] |
Once the matrix is created, we may easily loop through its rows and columns. I am using r for row and c for column in the code. The “tricky” part in the algorithm is actually to find which letter from the “snake” to write at a given position. The “remainder” in python helps us to calculate it quite easily – matrix[r][c] = snake[current_position % snake_size]. Every second line is “reversed”, thus matrix[r].reverse() is exactly the method that we need.
Printing a matrix
At the end, we need to print the matrix, and I have decided to make a separate function for it:
1 2 |
def format_matrix(my_matrix): return '\n'.join(''.join(str(column) for column in row) for row in my_matrix) |
The whole code works quite ok, displaying the matrix filled with the given string in the given dimensions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
def main(): first_line = input().split(" ") rows = int(first_line[0]) columns = int(first_line[1]) snake = list(input()) snake_size = len(snake) matrix = [[None for c in range(columns)] for r in range(rows)] current_position = 0 is_reversed = True for r in range(rows): is_reversed = not is_reversed for c in range(columns): matrix[r][c] = snake[current_position % snake_size] current_position += 1 if is_reversed: matrix[r].reverse() print(format_matrix(matrix)) def format_matrix(my_matrix): return '\n'.join(''.join(str(column) for column in row) for row in my_matrix) if __name__ == "__main__": main() |
Enjoy your day! 🙂