Last active
April 1, 2024 23:45
-
-
Save appositum/0ffad505c9355cde7f1386d4c43787ea to your computer and use it in GitHub Desktop.
regex.py
This file contains 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
import re | |
class Crossword: | |
def __init__(self, rows, columns, answer): | |
self.rows = rows | |
self.columns = columns | |
self.answer = answer | |
self.input_matrix = [ | |
[' ', *self.columns] | |
] | |
for r in self.rows: | |
row = [r, *('_' * 4)] | |
self.input_matrix.append(row) | |
def print_matrix(self): | |
s = [[str(e) for e in row] for row in self.input_matrix] | |
lens = [max(map(len, col)) for col in zip(*s)] | |
fmt = '\t'.join(f'{{:{x}}}' for x in lens) | |
table = [fmt.format(*row) for row in s] | |
print('\n'.join(table)) | |
def clear_screen(): | |
print("\x1B[2J\x1B[0;0H") | |
pass | |
def main(): | |
crossword = Crossword( | |
[r'[TRASH]*', r'(FA|AB)[TUP]*', r'(BA|TH|TU)*', r'.*A.*'], | |
[r'(TS|RA|QA)*', r'(AB|UT|AR)*', r'(K|T)U.*(A|R)', r'(AR|FS|ST)+'], | |
[ | |
['R', 'A', 'T', 'S'], | |
['A', 'B', 'U', 'T'], | |
['T', 'U', 'B', 'A'], | |
['S', 'T', 'A', 'R'] | |
] | |
) | |
while True: | |
clear_screen() | |
crossword.print_matrix() | |
answer = input(f"> ") | |
if re.match(r'^\d\d?\s?[A-Z]$', answer): | |
position = int(re.findall(r'\d\d?', answer)[0]) | |
letter = answer[-1] | |
coord_map = dict(zip(range(1, 17), [(x, y) for x in range(1, 5) for y in range(1, 5)])) | |
(i, j) = coord_map[position] | |
crossword.input_matrix[i][j] = letter | |
matrix_answer = [row[1:] for row in crossword.input_matrix[1:]] | |
if crossword.answer == matrix_answer: | |
clear_screen() | |
crossword.print_matrix() | |
print("Resposta correta") | |
for i in range(4): | |
print(re.fullmatch(crossword.rows[i], ''.join(matrix_answer[i]))) | |
print(re.fullmatch(crossword.columns[i], ''.join(matrix_answer[i]))) | |
break | |
elif answer == 'q' or answer == 'quit': | |
break | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment