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
|
597 Discussions
|
Please Login in order to post a comment
print(re.sub(r'(\w)\W+(\w)', r'\1 \2', ''.join(map(lambda x: ''.join(x), zip(*matrix))))
import re
Step 1: Input dimensions
n, m = map(int, input().split())
Step 2: Read the matrix
matrix = [input() for _ in range(n)]
Step 3: Transpose the matrix and build the decoded string
decoded = ''.join([matrix[i][j] for j in range(m) for i in range(n)])
Step 4: Use regex to replace symbols/spaces between alphanumerics with space
print(re.sub(r'(?<=[A-Za-z0-9])[^A-Za-z0-9]+(?=[A-Za-z0-9])', ' ', decoded))
Can someone explain to me why `#!/bin/python3 import re
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[1])
matrix = [0] * (n * m)
for x in range(n): matrix_item = input() for y in range(m): matrix[y * n + x] = matrix_item[y]
decoded_matrix = re.sub(r'(?<=[A-Za-z\s])!@#%&', ' ', ''.join(matrix)) decoded_matrix = re.sub(r'(?<=[\w])\s{2,}(?=[\w])', ' ', decoded_matrix) print(decoded_matrix)
Is only 81% correct? It runs, passed all test cases, and locally only takes ~.03 seconds on the test cases.
Here is HackerRank Matrix Script in Python solution - https://programmingoneonone.com/hackerrank-matrix-script-problem-solution-in-python.html