Skip to content

Instantly share code, notes, and snippets.

@xgao32
Last active April 23, 2016 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgao32/a1a23fca0aa3439f6edfa22eb6ab6c41 to your computer and use it in GitHub Desktop.
Save xgao32/a1a23fca0aa3439f6edfa22eb6ab6c41 to your computer and use it in GitHub Desktop.
# ACM 比赛 https://open.kattis.com/problems/secretmessage
import sys
import math
strings = [s.strip() for s in sys.stdin.readlines()]
for i in range(1,int(strings[0]) + 1):
# find input size for matrix and number of characters for each word in sentence
s = strings[i]
l = len(s)
m = int(math.pow(math.ceil(math.sqrt(l)),2))
k = int(math.ceil(math.sqrt(l)))
lett = []
# pad string
for j in range(m):
if j < len(s):
lett.append(s[j])
else:
lett.append('*')
gl = []
# append append nth character from last to first column
# big list
bl = []
# first to last row
for row in range((k-1),-1,-1):
temp = []
# first column to last column
for col in range(k):
temp.append(lett[((k)*row)+col])
bl.append(temp)
# first to last
for j in range(k):
for ii in range(k):
if bl[ii][j] == '*':
# print bl[k][j]
continue
else:
gl.append(bl[ii][j])
word = ''
word = word.join(gl)
print word
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment