# HackerRank Solution: Martix Script [Python Regex]

Author: Bashir Alam

## Question: Matrix Script [Python Regex and Parsing]

Neo has a complex matrix script. The matrix script is a N X M grid of strings. It consists of alphanumeric characters, spaces and symbols (!,@,#,\$,%,&).

To decode the script, Neo needs to read each column and select only the alphanumeric characters and connect them. Neo reads the column from top to bottom and starts reading from the leftmost column.

If there are symbols or spaces between two alphanumeric characters of the decoded script, then Neo replaces them with a single space '' for better readability.

Neo feels that there is no need to use 'if' conditions for decoding.

Alphanumeric characters consist of: [A-Z, a-z, and 0-9].

Input format

The first line contains space-separated integers N (rows) and M (columns) respectively.
The next N lines contain the row elements of the matrix script.

Constraints:

• 0 < N
• Â M < 100

Note: A 0 score will be awarded for using 'if' conditions in your code

Output Format:

Print the decoded matrix script.

Sample Input:

```Tsi
h%x
i #
sM
\$a
#t%
ir!```

Sample Output:

```This is Matrix#  %!
```

Explanation

The decoded script is:

```This\$#is% Matrix#  %!
```

Neo replaces the symbols or spaces between two alphanumeric characters with a single space ' ' for better readability.
So, the final decoded script is:

```This is Matrix#  %!
```

## Possible Solutions

Here we will discuss various possible solutions and will explain each script. We have been given the following code already:

``````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)
``````

Let us now move toward solutions:

### Solution-1: Using zip

Let us first solve the problem and then explain the script.

``````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)

# start
matrix = list(zip(*matrix))

sample = str()

for words in matrix:
for char in words:
sample += char

print(re.sub(r'(?<=\w)([^\w\d]+)(?=\w)', ' ', sample))``````

This code takes in input from the user in the form of two integers separated by a space on the first line, followed by a matrix of strings with dimensions specified by the integers. It then transposes the matrix (converting the rows to columns and columns to rows) and creates a single string by concatenating all the characters in the transposed matrix. This string is then passed to a regular expression to replace any non-word and non-digit characters with spaces, and the modified string is printed.

### Solution-2: Using nested for loop

Now let us move toward a second possible solution using python nested for loop:

``````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 = []
t = []
for i in range(n):
matrix_item = [x for x in input()]
matrix.append(matrix_item)

for i in range(m):
for j in range(n):
t.append(matrix[j][i])

s = ''.join(t)

path = re.compile(r'\b[ !@#\$%&]+\b', re.M)
k = re.sub(path, ' ', s)
print(k)``````

This code takes in input from the user in the form of two integers separated by a space on the first line, followed by a matrix of strings with dimensions specified by the integers. It then creates an empty list called '`t`', and iterates through the columns and rows of the matrix to build a new list with the elements of the matrix in a transposed order. It then concatenates the elements of the new list into a single string and passes this string to a regular expression to replace any non-word and non-digit characters with spaces. The modified string is then printed.

### Solution-3: Modifying previous solution

Let us modify the previous code and solve the problem.

``````import re

# Read n and m from the input
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[1])

# Read the matrix script from the input
matrix = []
for _ in range(n):
matrix_item = input()
matrix.append(matrix_item)

# Decode the matrix script
decoded_script = ""
for j in range(m):
for i in range(n):
decoded_script += matrix[i][j]

# Replace symbols or spaces between two alphanumeric characters with a single space
decoded_script = re.sub(r'(?<=[A-Za-z0-9])[^A-Za-z0-9]+(?=[A-Za-z0-9])', ' ', decoded_script)

# Print the decoded matrix script
print(decoded_script)``````

The code you provided reads in input from the user in the form of two integers separated by a space on the first line, followed by a matrix of strings with dimensions specified by the integers.

It then decodes the matrix by iterating through the columns and rows of the matrix and adding each element to a string called '`decoded_script`'.

The code then uses a regular expression to replace any non-alphanumeric characters that occur between two alphanumeric characters with a single space.

Finally, it prints the modified '`decoded_script`' string. This code is different from the previous one in that it iterates through the columns and rows of the matrix in a different order to build the '`decoded_script`' string and it uses a different regular expression to modify the string.

The previous code iterated through the rows and then the columns, and replaced any non-word and non-digit characters with spaces.

## Summary

In this short article, we covered various ways to solve Matrix scrip questions from Hacker Rank. We solve the problem using three different methods and explained each of the scripts.

Question on hacker rank: Matrix Script [Regex and Parsing]

Views: 1,339

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com