In this HackerRank Matrix Script problem solution 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].

HackerRank Matrix Script in python problem solution

HackerRank Matrix Script in python problem solution

import sys
import re
N, M = map(int, sys.stdin.readline().split())
rows = [sys.stdin.readline()[:M] for i in xrange(N)]

cols = [''.join([rows[i][j] for i in xrange(N)]) for j in xrange(M)]
decode = ''.join(cols)

print re.sub('([0-9a-zA-Z])[^0-9a-zA-Z]+([0-9a-zA-Z])', '\g<1> \g<2>', decode)

#bdict = {True: 1, False: 0}
#fanum = -max([-i*bdict[c.isalnum()] for i, c in enumerate(decode)])
#lanum = max([i*bdict[c.isalnum()] for i, c in enumerate(decode)])+1

#decsub = decode[fanum:lanum]
#decsub = re.sub('[^0-9a-zA-Z]+', ' ', decsub)
#print decode[:fanum] + decsub + decode[lanum:]