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
|
586 Discussions
|
Please Login in order to post a comment
here is my solution icomplie in python 3
!/bin/python3
import re
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.ljust(m))
string = ''.join(matrix[row][col] for col in range(m) for row in range(n))
output = re.sub(r'(?<=\w)[!@#$&% ]+(?=\w)', ' ', string)
print(output)
import sys import re p = re.compile(r'(?<=\w)([\$#\%\s]+)(?=\w)') dem = sys.stdin.readline().split(); r = int(dem[0]) c = int(dem[1]) rows = [l for l in sys.stdin] text = ""; for i in range(c): for j in range(r): text = text+rows[j][i] print(p.sub(' ',text))
import sys import re p = re.compile(r'(?<=\w)([\$#\%\s]+)(?=\w)') dem = sys.stdin.readline().split(); r = int(dem[0]) c = int(dem[1]) rows = [l for l in sys.stdin] text = ""; for i in range(c): for j in range(r): text = text+rows[j][i] print(p.sub(' ',text))
Explanation of Changes and Why This Works:
1) Integration with Provided Skeleton: The key here was to take the already provided HackerRank code skeleton (which handles the input) and add the decoding logic after it. The provided skeleton reads the n, m, and the matrix elements.
2) Decoding Logic (Unchanged and Correct): The core decoding logic (building the decoded_script string and the regular expression) remains exactly the same as in my previous fully working response. This is the most important part, and it's already been thoroughly vetted and explained. We're simply inserting it into the HackerRank structure.
3) No Changes to Input Reading: The first_multiple_input, n, m, matrix variable assignments, and the for loop to read the matrix are all left untouched. This is exactly what the HackerRank environment expects.
Output: The print() statement is placed after the provided input-reading code and after my added decoding logic. This is the correct place to generate the output.