Last active
September 19, 2016 21:25
-
-
Save alexandre/6ab804059fcb822caef9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import pprint | |
| import random | |
| import string | |
| def generate_matrix(i_length, j_length, default_value=None): | |
| return [[default_value] * j_length for _ in range(i_length)] | |
| def get_matrix_index(matrix): | |
| # considering a simple 2D matrix. | |
| return [(i, j) for i in range(len(matrix)) for j in range(len(matrix[0]))] | |
| def get_empty_position(matrix, get_all=False): | |
| all_empty_positions = [ | |
| pos for pos in get_matrix_index(matrix) if not matrix[pos[0]][pos[1]] | |
| ] | |
| return random.choice(all_empty_positions) if not get_all else all_empty_positions # noqa | |
| def get_a_brand_new_character(text): | |
| return random.choice(list( | |
| set(string.ascii_lowercase + string.digits) | set(text) | |
| )) | |
| def insert_text_randomly(matrix, text): | |
| for letter in text: | |
| position = get_empty_position(matrix) | |
| matrix[position[0]][position[1]] = letter | |
| for pos in get_empty_position(matrix, get_all=True): | |
| if not matrix[pos[0]][pos[1]]: | |
| matrix[pos[0]][pos[1]] = get_a_brand_new_character(text) | |
| return matrix | |
| if __name__ == '__main__': | |
| matrix = generate_matrix(8, 8) | |
| text = list(set(input('Text: '))) | |
| pprint.pprint(insert_text_randomly(matrix, text)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment