aboutsummaryrefslogtreecommitdiff
path: root/challenge-019/paulo-custodio/python/ch-2.py
blob: e36e65f37909e5f75a73cb65ee8ad2a9aa0e482d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python3

# Challenge 019
#
# Task #2
# Write a script that can wrap the given paragraph at a specified column using
# the greedy algorithm.

import fileinput
import sys

def read_input():
    lines = []
    for line in fileinput.input():
        lines.append(line)
    return lines

def wrap(text, column):
    output = ""
    pos = 0
    sep = ""
    for word in (text.split()):
        if pos+len(sep)+len(word) >= column:
            output += "\n"
            sep = ""
            pos = 0
        output += sep+word
        pos += len(sep)+len(word)
        sep = " "
    return output

print(wrap(" ".join(read_input()), 72))