jamestha3d

hackerrank encryption

May 1st, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. #!/bin/python3
  2.  
  3. import math
  4. import os
  5. import random
  6. import re
  7. import sys
  8.  
  9. # Complete the encryption function below.
  10. def encryption(s):
  11. #word = input("please enter a word\n")
  12.     word = s
  13.     word = ''.join(word.split())
  14.     length = len(word)
  15.     root = math.sqrt(length)
  16.     floor = math.floor(root)
  17.     ceil = math.ceil(root)
  18.  
  19.     if floor * ceil < length :
  20.         floor = ceil
  21.  
  22.     result = ""
  23.     for j in range(ceil):
  24.         #print(f"j:{j}")
  25.         for i in range(floor):
  26.             #print(f"i:{i}")
  27.             if (i*ceil + j) >= length:
  28.                 continue
  29.             else:
  30.                 #print(word[i*ceil + j], end = "")
  31.  
  32.                 result = result + word[i*ceil + j]
  33.         #print(" ", end = "")
  34.         if j != ceil:
  35.             result = result + " "
  36.     #print()
  37.  
  38.     return result
  39.  
  40. if __name__ == '__main__':
  41.     fptr = open(os.environ['OUTPUT_PATH'], 'w')
  42.  
  43.     s = input()
  44.  
  45.     result = encryption(s)
  46.  
  47.     fptr.write(result + '\n')
  48.  
  49.     fptr.close()
Add Comment
Please, Sign In to add comment