#!usr/bin/env python
#encoding:utf-8
'''''
__Author__:沂水寒城
功能:zigzag_pattern
T W Q K
N D X Z S V B
G A C
convert("TNGDWXAZQSCVBK", 3) should return "TWQBNDXZSVKGAC".
'''
def convert_test(one_str,num):
'''''
'''
mod=len(one_str)%(num+1)
if mod==0:
cols=(len(one_str)/(num+1))*2
else:
cols=(len(one_str)/(num+1))*2+1
rows=num
#print 'rows, cols', rows, cols
matrix=[]
for i in range(rows):
matrix.append([0]*cols)
one_str_list=list(one_str)
for j in range(cols):
if j%2==0:
for i in range(rows):
if len(one_str_list):
matrix[i][j]=one_str_list.pop(0)
else:
matrix[1][j]=one_str_list.pop(0)
#print matrix
# for one in matrix:
# one_list=[str(onec) for onec in one]
# print ' '.join(one_list)
res=''
for one_list in matrix:
one_list=[str(one) for one in one_list]
res+=''.join(one_list)
return ''.join(res.split('0'))
if __name__ == '__main__':
print convert_test("TNGDWXAZQSCVBK", 3)
print convert_test("PAYPALISHIRING", 2)
print convert_test("PAYPALISHIRING", 4) 结果如下:
TWQBNDXZSVKGAC
PPIINAYALSHRIG
PLRAAIIIYSNPHG
[Finished in 0.3s]
相关推荐:
Python实现求解括号匹配问题的方法
用Python实现web端用户登录和注册功能
以上就是Python实现读取字符串按列分配后按行 输出的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于Python实现读取字符串按列分配后按行输出的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did84138