设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【每日一坑 5】 文字竖排

[复制链接]

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

6#
发表于 2014-5-19 17:44:46 |只看该作者
fptxyy 发表于 2014-5-19 12:07
采用fl0w的代码,运行后是这样,
>>>
Traceback (most recent call last):

都是编码设置的问题

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

使用道具 举报

0

主题

0

好友

25

积分

新手上路

Rank: 1

5#
发表于 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

好友

11万

积分

管理员

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

好友

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
复制代码
回复

使用道具 举报

0

主题

0

好友

389

积分

中级会员

Rank: 3Rank: 3

沙发
发表于 2013-12-23 09:23:17 |只看该作者
fl0w 发表于 2013-12-23 09:08
trans.py

等长与不等长诗的效果

Screenshot-1.png (21.59 KB, 下载次数: 628)

等长

等长

Screenshot-2.png (31.99 KB, 下载次数: 609)

不等长

不等长

回复

使用道具 举报

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???'
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-5-19 01:29 , Processed in 0.020287 second(s), 25 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部