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.

In the case where L is not a perfect square num, you would increase col by 1

After incresement, if row x col is smaller than L, just increase row by 1 as well.

Then you should get row, col pair based on challenge's rules.

zip_longest():

fromitertoolsimportzip_longest# Use it to combine 2 iterables:wordlist=['apple','grape','rank']print(*zip_longest(*wordlist,fillvalue='_'))# Output: ('a', 'g', 'r') ('p', 'r', 'a') ('p', 'a', 'n') ('l', 'p', 'k') ('e', 'e', '_')

importmathfromitertoolsimportzip_longestdefencryption(s):# Write your code here# Remove spaces and calculate the lengthnew_s="".join(s.split(" "))l=len(new_s)row=col=int(math.sqrt(l))ifnotmath.sqrt(l).is_integer():col=row+1ifrow*col<l:row=colgrid=[new_s[i:i+col]foriinrange(0,len(new_s),col)]new_grid=zip_longest(*grid,fillvalue="")encrypted=" ".join("".join(items)foritemsinnew_grid)returnencrypted

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website

## Encryption

You are viewing a single comment's thread. Return to all comments â†’

Simple pypy3 solution using itertools lib

## Row / Column tweak logic:

Comparison: L ^ 0.5 <= row <= col <= L ^ 0.5## zip_longest():