Python 3,181字节
def f(s):
for k in [1,0]*4:
b=list(zip(*[([' ']*(len(s)-1-n)*k+list(i)+[' ']*n*k)[::-1] for n,i in enumerate(s)]))
print([' '.join(i).replace(' ','') for i in b])
if k==0:s=b
说明
def f(s):
for k in [0]*4: # loop 4 times, we don't need the index so [0]*4 is shorter than range(4)
l=len(s)-1 # number of line
# rotation of 45°
a=[(['.']*(l-n)+list(i)+['.']*n)[::-1] for n,i in enumerate(s)]
# tranform matrice :
# ABC ..ABC CBA..
# DEF --> .DEF. --> .FED.
# GHI GHI.. ..IHG
b=list(zip(*a)) # transpose
# CBA.. C..
# .FED. --> BF.
# ..IHG AEI
# .DH
# ..G
print(' '.join(''.join(i).replace('.','') for i in b))
# rotation of 90°
a=[(list(i))[::-1] for n,i in enumerate(s)]
# tranform matrice :
# ABC CBA
# DEF --> FED
# GHI IHG
b=list(zip(*a)) # transpose
# CBA CFI
# FED --> BEH
# IHG ADG
print(' '.join(''.join(i) for i in b))
s=b
结果
>>> f(['ABCDE','FGHIJ','KLMNO','PQRST'])
['E', 'DJ', 'CIO', 'BHNT', 'AGMS', 'FLR', 'KQ', 'P']
['EJOT', 'DINS', 'CHMR', 'BGLQ', 'AFKP']
['T', 'OS', 'JNR', 'EIMQ', 'DHLP', 'CGK', 'BF', 'A']
['TSRQP', 'ONMLK', 'JIHGF', 'EDCBA']
['P', 'QK', 'RLF', 'SMGA', 'TNHB', 'OIC', 'JD', 'E']
['PKFA', 'QLGB', 'RMHC', 'SNID', 'TOJE']
['A', 'FB', 'KGC', 'PLHD', 'QMIE', 'RNJ', 'SO', 'T']
['ABCDE', 'FGHIJ', 'KLMNO', 'PQRST']
>>> f(['ABCDEF','GHIJKL','MNOPQR','STUVWX'])
['F', 'EL', 'DKR', 'CJQX', 'BIPW', 'AHOV', 'GNU', 'MT', 'S']
['FLRX', 'EKQW', 'DJPV', 'CIOU', 'BHNT', 'AGMS']
['X', 'RW', 'LQV', 'FKPU', 'EJOT', 'DINS', 'CHM', 'BG', 'A']
['XWVUTS', 'RQPONM', 'LKJIHG', 'FEDCBA']
['S', 'TM', 'UNG', 'VOHA', 'WPIB', 'XQJC', 'RKD', 'LE', 'F']
['SMGA', 'TNHB', 'UOIC', 'VPJD', 'WQKE', 'XRLF']
['A', 'GB', 'MHC', 'SNID', 'TOJE', 'UPKF', 'VQL', 'WR', 'X']
['ABCDEF', 'GHIJKL', 'MNOPQR', 'STUVWX']
输出更清晰(189字节)
j=' '.join
def f(s):
for k in [1,0]*4:
b=list(zip(*[([' ']*(len(s)-1-n)*k+list(i)+[' ']*n*k)[::-1] for n,i in enumerate(s)]))
print(j(j(i).replace(' ','') for i in b))
if k==0:s=b
。
>>> f(['ABCDE','FGHIJ','KLMNO','PQRST'])
E DJ CIO BHNT AGMS FLR KQ P
EJOT DINS CHMR BGLQ AFKP
T OS JNR EIMQ DHLP CGK BF A
TSRQP ONMLK JIHGF EDCBA
P QK RLF SMGA TNHB OIC JD E
PKFA QLGB RMHC SNID TOJE
A FB KGC PLHD QMIE RNJ SO T
ABCDE FGHIJ KLMNO PQRST