hackerrank:Text Wrap

jlqwer 发表于 代码 分类,标签: 热度:168°

Textwrap

The textwrap module provides two convenient functions: wrap() and fill().

textwrap.wrap() 
The wrap() function wraps a single paragraph in text (a string) so that every line is width characters long at most. 
It returns a list of output lines.

>>> import textwrap
>>> string = "This is a very very very very very long string."
>>> print textwrap.wrap(string,8)
['This is', 'a very', 'very', 'very', 'very', 'very', 'long', 'string.']

textwrap.fill() 
The fill() function wraps a single paragraph in text and returns a single string containing the wrapped paragraph.

>>> import textwrap
>>> string = "This is a very very very very very long string."
>>> print textwrap.fill(string,8)
This is
a very
very
very
very
very
long
string.

Task

You are given a string  and width 
Your task is to wrap the string into a paragraph of width .

Input Format

The first line contains a string, 
The second line contains the width, .

Constraints

 

Output Format

Print the text wrapped paragraph.

Sample Input

ABCDEFGHIJKLIMNOQRSTUVWXYZ
4

Sample Output

ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ

答案有点有点投机取巧

code:

def wrap(string, max_width):
    temp=""
    m=len(string)
    for i in range(m):
        if(i%max_width==(max_width-1)):
            temp=temp+string[i]
            print (temp)
            temp=""
        else:
            temp=temp+string[i]
    return temp


lll

0 篇评论

发表我的评论