We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Regex and Parsing
- Matrix Script
- Discussions
Matrix Script
Matrix Script
Sort by
recency
|
603 Discussions
|
Please Login in order to post a comment
!/bin/python3
import math import os import random import re import sys
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[1])
matrix = [] pattern = r"(?<=[A-Za-z0-9])[^A-Za-z0-9]+(?=[A-Za-z0-9])" for _ in range(n): matrix_item = input() matrix.append(matrix_item) zipped = list(zip(*matrix)) final = "".join(ch for tup in zipped for ch in tup)
output = re.sub(pattern," ",final) print(output)
By far not the best way, but I've tried to do it without regex:
!/bin/python3
import math import os import random import re import sys
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[1])
matrix = []
for _ in range(n): matrix_item = input() matrix.append(matrix_item)
string = ""
for j in range(m): for i in range(n): string += matrix[i][j]
symb = ('!','@','#','$','%','&', ' ')
temp_list = []
for idx, c in enumerate(string): ((c in symb) == True and ((idx-1>=0 and string[idx-1] not in symb) == True or temp_list != [])) and temp_list.append(idx)
try: item = temp_list[-1] == len(string)-1 and temp_list[-1] or -999
except Exception: # In case the themp_list is [], so we just return the original string print(string)