Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def y_combin(f, *args):
- return f(f, *args)
- def make_line(recur, cur, rest, width):
- return (
- (cur, rest)
- if (not rest) or sum(map(len, cur)) + len(cur) + len(rest[0]) > width else
- recur(recur, cur + (rest[0],), rest[1:], width)
- )
- import math
- def spacing(words, width):
- return int(math.ceil((width - sum(map(len, words))) / float(len(words) - 1)))
- def justify(recur, words, width):
- return (
- ''
- if (not words) or width == 0 else (
- words[0]
- if len(words) == 1 else
- words[0] + ' ' * spacing(words, width) + recur(recur, words[1:], width - len(words[0]) - spacing(words, width))
- )
- )
- def make_doc(recur, words, width):
- return (
- ()
- if not words else
- (y_combin(justify, y_combin(make_line, (), words, width)[0], width),) + recur(recur, y_combin(make_line, (), words, width)[1], width)
- )
- def fix_doc(recur, lines):
- return (
- (lines[0],) + recur(recur, lines[1:])
- if len(lines) > 1 else
- (' '.join(lines[0].split()),)
- )
- #ln, rest = (y_combin(make_line, (), __import__('sys').stdin.read().split(), 30))
- #print(ln)
- #print(spacing(ln, 30))
- #
- #print(y_combin(justify, ln, 30))
- #print('0123456789'*3)
- #print(len(rest), 'more')
- #doc = y_combin(fix_doc, (y_combin(make_doc, __import__('sys').stdin.read().split(), 20)))
- #print(doc)
- #print(map(len, doc))
- def pr(x):
- return __import__('sys').stdout.write(x + '\n')
- map(pr, y_combin(fix_doc, y_combin(make_doc, __import__('sys').stdin.read().split(), 20)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement