Matrix Script! Python HackerRank Solution
Hello Friends, How are you? Today I am going to solve the HackerRank Matrix Script Python Problem with a straightforward explanation. In this article, you will get more than one approach to solve this problem. So let's start-
{tocify} $title={Table of Contents}
HackerRank Python Matrix Script Solution - Problem Statement
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 (!,@,#,$,%,&).
Python Matrix Script HackerRank |
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 0
7 3 Tsi h%x i # sM $a #t% ir! {codeBox}
Sample Output
This is Matrix# %! {codeBox}
Explanation
The decoded script is:
This$#is% Matrix# %! {codeBox}
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# %! {codeBox}
Matrix Script! HackerRank Python Solution
Python Matrix Script - Hacker Rank Solution
Approach I: Matrix Script HackerRank in Python 3
# ========================
# Information
# ========================
# Name: Matrix Script - Python HackerRank Problem
# Direct Link: https://www.hackerrank.com/challenges/matrix-script/problem
# Difficulty: Hard
# Max Score: 100
# Cutoff Score: 99.00
# Language: Python 3
# ========================
# Solution Start
# ========================
# Matrix Script - Hacker Rank Solution Start
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))
# Matrix Script - Hacker Rank Solution END
# MyEduWaves
Approach II: Matrix Script HackerRank Solution in Python 2
# ========================
# Information
# ========================
# Name: Matrix Script - Python HackerRank Problem
# Direct Link: https://www.hackerrank.com/challenges/matrix-script/problem
# Difficulty: Hard
# Max Score: 100
# Cutoff Score: 99.00
# Language: Python 2
# ========================
# Solution Start
# ========================
# Matrix Script - Hacker Rank Solution Start
import re
N, M = (int(x) for x in raw_input().split())
ain = []
tin = []
for i in range(N):
ain.append(raw_input())
for i in range(M):
for j in range(N):
tin.append(ain[j][i])
print re.compile(r"([a-zA-Z0-9])[^a-zA-Z0-9]+([a-zA-Z0-9])").sub(r"\1 \2", "".join(tin))
# Matrix Script - Hacker Rank Solution END
# MyEduWaves
Approach III: Matrix Script HackerRank Python Solution in Pypy 2
# ========================
# Information
# ========================
# Name: Matrix Script - Python HackerRank Problem
# Direct Link: https://www.hackerrank.com/challenges/matrix-script/problem
# Difficulty: Hard
# Max Score: 100
# Cutoff Score: 99.00
# Language: Pypy 2
# ========================
# Solution Start
# ========================
# Matrix Script - Hacker Rank Solution Start
import re
n, m = map(int, raw_input().split())
arr = ''.join([''.join(t) for t in zip(*[raw_input() for _ in xrange(n)])])
print re.sub(r'\b[^a-zA-Z0-9]+\b', r' ', arr)
# Matrix Script - Hacker Rank Solution END
# MyEduWaves
Approach IV: Matrix Script HackerRank Python Solution in Pypy3
# ========================
# Information
# ========================
# Name: Matrix Script - Python HackerRank Problem
# Direct Link: https://www.hackerrank.com/challenges/matrix-script/problem
# Difficulty: Hard
# Max Score: 100
# Cutoff Score: 99.00
# Language: Pypy 3
# ========================
# Solution Start
# ========================
# Matrix Script - Hacker Rank Solution Start
import re
n,m =map(int,(input().split()))
a,b = [],""
for _ in range(n):
a.append(input())
for z in zip(*a):
b +="".join(z)
print(re.sub(r"(?<=\w)([^\w]+)(?=\w)"," ",b))
# Matrix Script - Hacker Rank Solution END
# MyEduWaves
Disclaimer: The above Problem ( Python Matrix Script ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.
I hope you have understood the solution to this HackerRank Problem. All these solutions will pass all the test cases. Now visit Python Matrix Script Hackerrank Problem and try to solve it again.
All the Best!
Tags:
HackerRank Python