请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 37808|回复: 36

【每日一坑 5】 文字竖排

[复制链接]

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2013-12-22 23:38:47 |显示全部楼层
前两天看到一道还蛮有意思的题目,今天就拿来挖坑吧:

把一段字符串用“右起竖排”的古文格式输出,并且拿竖线符号作为每一列的分割符。
比如这段文字:
"静夜思 李白床前明月光,疑似地上霜。举头望明月,低头思故乡。"
输出结果:
低┊举┊疑┊床┊静
头┊头┊似┊前┊夜
思┊望┊地┊明┊思
故┊明┊上┊月┊
乡┊月┊霜┊光┊李
。┊,┊。┊,┊白



#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

389

积分

中级会员

Rank: 3Rank: 3

发表于 2013-12-23 09:08:14 |显示全部楼层

回帖奖励 +5

本帖最后由 fl0w 于 2013-12-23 16:01 编辑

trans.py
  1. #! /usr/bin/env python
  2. # coding:utf-8

  3. import sys

  4. stops = ' !,。'
  5. stops = stops.decode('utf-8')

  6. def getLength(poe):
  7.     situation = [i for i in range(len(poe)) if poe[i] in stops]
  8.     situation.insert(0, -1)
  9.     #print situation
  10.     gaps = [situation[i] - situation[i - 1] for i in range(1, len(situation))]
  11.     #print gaps
  12.     if gaps:
  13.         return max(gaps)
  14.     else:
  15.         return None

  16. def transferPoetry(poe, sentLength):
  17.     NewPoe = []
  18.     tempSent = []
  19.     for i in poe:
  20.         if i not in stops:
  21.             tempSent.append(i)
  22.         elif len(tempSent) < sentLength:
  23.             tempSent.append(i)
  24.             tempSent += stops[0] * (sentLength - len(tempSent))
  25.             NewPoe.append(tempSent)
  26.             tempSent = []
  27.     RealPoe = []
  28.     for i in xrange(sentLength):
  29.         RealPoe.append([NewPoe[x][i] for x in xrange(len(NewPoe))])
  30.     for i in xrange(len(RealPoe)):
  31.         RealPoe[i].reverse()
  32.         RealPoe[i] = '|'.join(RealPoe[i])
  33.     return RealPoe
  34.    
  35. if __name__ == '__main__':
  36.     poe = raw_input('Please input your poetry:')
  37.     poe = poe.decode('utf-8')
  38.     #print('the poetry is: %s' % poe)
  39.     #print('the length is %d' % len(poe))
  40.     sentLength = getLength(poe)
  41.     if sentLength:
  42.         for i in transferPoetry(poe, sentLength):
  43.             print i
  44.     else:
  45.         print 'Are you sure this is a poetry???'
复制代码
回复

使用道具 举报

0

主题

0

好友

389

积分

中级会员

Rank: 3Rank: 3

发表于 2013-12-23 09:23:17 |显示全部楼层
fl0w 发表于 2013-12-23 09:08
trans.py

等长与不等长诗的效果

等长

等长

不等长

不等长
回复

使用道具 举报

0

主题

0

好友

134

积分

注册会员

Rank: 2

发表于 2013-12-23 17:43:16 |显示全部楼层

回帖奖励 +5

对自己有点难度啊,弄了一下午,还没考虑不能构成矩形的情况,这里有个问题就是每个中文字符是占两个字符的,而在中文输出下空格还是占一个字符的,所以要在输入时“思“和”李“之间打两个空格。
  1. # coding=gbk
  2. input=raw_input('中文:')#输入竖排中文,空格按两下
  3. rows=int(raw_input('rows:'))#输入要打印的行数
  4. t_l=len(input)
  5. cols=t_l/2/rows#默认可以除尽,得出列数
  6. lis=[]
  7. current=[]
  8. start=t_l#起始
  9. for i in range(0,rows):#遍历rows次,每次将正常顺序的打印存放进数组
  10.     for j in range(0,cols):#这里要打印5列,意思就是每次存放5个中文字符
  11.         start=start-rows*2#每个中文字符起始,因为有6行,所以每2*6普通字符的长度个得到一个中文字符,从头开始减
  12.         end=start+2#结尾
  13.         current.append(input[start:end])#加入临时数组
  14.     lis.append(current)
  15.     current=[]
  16.     start=t_l+2*(i+1)#初始往后移
  17.     end=start+2
  18. for i in lis:#按顺序打印,每个字符用“|”隔开
  19.     line='|'.join(i)
  20.     print line
复制代码
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2013-12-26 17:17:04 |显示全部楼层
scz_000 发表于 2013-12-23 17:43
对自己有点难度啊,弄了一下午,还没考虑不能构成矩形的情况,这里有个问题就是每个中文字符是占两个字符的 ...

最好别用input这种系统方法名来做变量名
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

25

积分

新手上路

Rank: 1

发表于 2014-5-19 12:07:15 |显示全部楼层

回帖奖励 +5

本帖最后由 fptxyy 于 2014-5-19 12:14 编辑

采用fl0w的代码,运行后是这样,
>>>
Traceback (most recent call last):
  File "D:\Python27\exercises\mryk05.py", line 12, in <module>
    stops = stops.decode('utf-8')
  File "D:\Python27\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa3 in position 1: invalid start byte
>>>
采用scz_00的代码,保存时提示要用coding:936,我就点了ok,然后再运行,成这样,
>>>
中文:静思语 床前明月光 疑是地上霜 举头望明月 低头思故乡
rows:5
低|偻|是|懊|静
头|吠|地|髟|思
˼|ûÃ|ÉÏ|¹|Óï
ᄍÊ|÷Ô|˪|â | ᄡ
Ïç|Â | ᄒ|ÒÉ|ᄇÇ
>>>
怎么修改呢?请求指导,谢谢~
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2014-5-19 17:44:46 |显示全部楼层
fptxyy 发表于 2014-5-19 12:07
采用fl0w的代码,运行后是这样,
>>>
Traceback (most recent call last):

都是编码设置的问题

你试试在命令行下面运行
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

1

主题

0

好友

50

积分

注册会员

Rank: 2

发表于 2014-6-23 16:03:38 |显示全部楼层

回帖奖励 +5

  1. #!/usr/bin/env python
  2. #coding:utf-8

  3. poem=unicode('静夜思 李白床前明月光,疑似地上霜。举头望明月,低头思故乡。','utf8')
  4. formatPoem=[[0 for col in range(5)] for row in range(6)]
  5. i=0
  6. j=4
  7. num=0
  8. for char in poem:
  9.     while i<6 and j>=0:
  10.         formatPoem[i][j]=char
  11.         i=i+1
  12.         num=num+1
  13.         break
  14.     if num%6==0:
  15.         j=j-1
  16.         i=0
  17. for list in formatPoem:
  18.     for char in list:
  19.         if char is not list[-1]:
  20.             print char+'|',
  21.         else:
  22.             print char,
  23.     print '\n',
  24. raw_input()
  25.    
复制代码
回复

使用道具 举报

1

主题

0

好友

207

积分

中级会员

Rank: 3Rank: 3

发表于 2016-1-16 14:11:47 |显示全部楼层
本帖最后由 manhong2112 于 2016-1-16 14:24 编辑
  1. poem = "\
  2. 静夜思 李白\n\
  3. 床前明月光,\n\
  4. 疑似地上霜。\n\
  5. 举头望明月,\n\
  6. 低头思故乡。".split('\n')

  7. poem = poem[::-1]

  8. poemLength = 0
  9. for i in poem: poemLength = len(i) if poemLength < len(i) else poemLength
  10. poemSize = len(poem) if len(poem) > poemLength else poemLength

  11. for i in range(0,poemSize):
  12.   for j in range(0,poemSize):
  13.     try:
  14.       print(poem[j][i] if poem[j][i] != " " else "  " ,end="|")
  15.     except IndexError:
  16.       print(end="  ")
  17.   print()
复制代码
弄了兩個小時,總算寫出來了
回复

使用道具 举报

1

主题

0

好友

207

积分

中级会员

Rank: 3Rank: 3

发表于 2016-1-16 14:21:40 |显示全部楼层
效果
螢幕擷取畫面 (15).png
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-3-28 21:30 , Processed in 0.027826 second(s), 25 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部