Saturday, April 6, 2019

Text Wrap - Hacker Rank Solution

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 0
ABCDEFGHIJKLIMNOQRSTUVWXYZ
4
Sample Output 0
ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ
Text Wrap - Hacker Rank Solution
import textwrap
S = raw_input()
w = input()
print textwrap.fill(S,w)

2 comments:

  1. How to do this for sentence ? Width =18.fit(sentence,width)

    ReplyDelete
  2. def wrap(string, max_width):
    ll=list()
    j=0
    for i in range(max_width,len(string),max_width):
    if i<=len(string):
    ll.append(string[j:i])
    j=i
    ll.append(string[j:])
    ll="\n".join(ll)
    return ll

    if __name__ == '__main__':
    string, max_width = input(), int(input())
    result = wrap(string, max_width)
    print(result)
    try this without any import required

    ReplyDelete

Powered by Blogger.