- 帖子
- 27
- 精华
- 0
- 积分
- 389
- 阅读权限
- 30
- 注册时间
- 2013-9-9
- 最后登录
- 2014-5-20
|
回帖奖励 +5
本帖最后由 fl0w 于 2013-12-23 16:01 编辑
trans.py- #! /usr/bin/env python
- # coding:utf-8
- import sys
- stops = ' !,。'
- stops = stops.decode('utf-8')
- def getLength(poe):
- situation = [i for i in range(len(poe)) if poe[i] in stops]
- situation.insert(0, -1)
- #print situation
- gaps = [situation[i] - situation[i - 1] for i in range(1, len(situation))]
- #print gaps
- if gaps:
- return max(gaps)
- else:
- return None
- def transferPoetry(poe, sentLength):
- NewPoe = []
- tempSent = []
- for i in poe:
- if i not in stops:
- tempSent.append(i)
- elif len(tempSent) < sentLength:
- tempSent.append(i)
- tempSent += stops[0] * (sentLength - len(tempSent))
- NewPoe.append(tempSent)
- tempSent = []
- RealPoe = []
- for i in xrange(sentLength):
- RealPoe.append([NewPoe[x][i] for x in xrange(len(NewPoe))])
- for i in xrange(len(RealPoe)):
- RealPoe[i].reverse()
- RealPoe[i] = '|'.join(RealPoe[i])
- return RealPoe
-
- if __name__ == '__main__':
- poe = raw_input('Please input your poetry:')
- poe = poe.decode('utf-8')
- #print('the poetry is: %s' % poe)
- #print('the length is %d' % len(poe))
- sentLength = getLength(poe)
- if sentLength:
- for i in transferPoetry(poe, sentLength):
- print i
- else:
- print 'Are you sure this is a poetry???'
复制代码 |
|