CarlosLabs
Hackerrank Arrays - Encryption
{CM: this is a verbose, very ugly unpythonic hack; but
it worked the first time around
}
#!/bin/python3 import math import os import random import re import sys # Complete the encryption function below. def encryption(s): sPrime = s.replace(" ", "") mH = math.floor(math.sqrt(len(sPrime))) mW = math.ceil(math.sqrt(len(sPrime))) while (mW * mH) < len(sPrime): mH += 1 sPrime_list = [char for char in sPrime] enc = [["0" for x in range(mW)] for y in range(mH)] # load matrix thisCharIndex = 0 for x in range(mH): for y in range(mW): if (thisCharIndex >= len(sPrime)): #thisChar = "" break else: thisChar = sPrime_list[thisCharIndex] enc[x][y]=thisChar thisCharIndex += 1 # unload matrix retStr = "" thisCharIndex = 0 for y in range(mW): for x in range(mH): retStr += enc[x][y] # thisCharIndex += 1 retStr += " " return (retStr.replace("0", "")) if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') s = input() result = encryption(s) fptr.write(result + '\n') fptr.close()