SHARE
TWEET

Untitled

a guest May 19th, 2017 40 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def y_combin(f, *args):
  2.     return f(f, *args)
  3.  
  4. def make_line(recur, cur, rest, width):
  5.     return (
  6.         (cur, rest)
  7.         if (not rest) or sum(map(len, cur)) + len(cur) + len(rest[0]) > width else
  8.         recur(recur, cur + (rest[0],), rest[1:], width)
  9.     )
  10.  
  11. import math
  12.  
  13. def spacing(words, width):
  14.     return int(math.ceil((width - sum(map(len, words))) / float(len(words) - 1)))
  15.  
  16. def justify(recur, words, width):
  17.     return (
  18.         ''
  19.         if (not words) or width == 0 else (
  20.             words[0]
  21.             if len(words) == 1 else
  22.             words[0] + ' ' * spacing(words, width) + recur(recur, words[1:], width - len(words[0]) - spacing(words, width))
  23.         )
  24.     )
  25.  
  26. def make_doc(recur, words, width):
  27.     return (
  28.         ()
  29.         if not words else
  30.         (y_combin(justify, y_combin(make_line, (), words, width)[0], width),) + recur(recur, y_combin(make_line, (), words, width)[1], width)
  31.     )
  32.  
  33. def fix_doc(recur, lines):
  34.     return (
  35.         (lines[0],) + recur(recur, lines[1:])
  36.         if len(lines) > 1 else
  37.         (' '.join(lines[0].split()),)
  38.     )
  39.  
  40. #ln, rest = (y_combin(make_line, (), __import__('sys').stdin.read().split(), 30))
  41. #print(ln)
  42. #print(spacing(ln, 30))
  43. #
  44. #print(y_combin(justify, ln, 30))
  45. #print('0123456789'*3)
  46. #print(len(rest), 'more')
  47.  
  48. #doc = y_combin(fix_doc, (y_combin(make_doc, __import__('sys').stdin.read().split(), 20)))
  49. #print(doc)
  50. #print(map(len, doc))
  51. def pr(x):
  52.     return __import__('sys').stdout.write(x + '\n')
  53. map(pr, y_combin(fix_doc, y_combin(make_doc, __import__('sys').stdin.read().split(), 20)))
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top