Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. def space(gaps, spaces):
  2. if gaps == 0:
  3. return []
  4. if float(spaces)/gaps == spaces/gaps:
  5. start = spaces/gaps
  6. else:
  7. start = spaces/gaps + 1
  8. l = []
  9. for z in range(gaps):
  10. l.append(start)
  11. while sum(l) != spaces:
  12. for x in range(gaps-1, -1, -1):
  13. l[x] -= 1
  14. if sum(l) == spaces:
  15. break
  16. return l
  17.  
  18.  
  19. def justify(text, width):
  20. a = text.split(" ")
  21. x = 0
  22. s = ""
  23. newx = 0
  24. while x < len(a) - 1:
  25. total = 0
  26. for y in range(x, len(a)):
  27. if total + len(a[y]) <= width:
  28. newx = y
  29. total += len(a[y]) + 1
  30. else:
  31. total -= 1
  32. break
  33. if newx == len(a) - 1:
  34. total -= 1
  35. spaces = space(newx - x, (newx - x) + width - total)
  36. if newx - x > 0:
  37. s += a[x]
  38. for y in range(x, newx):
  39. s += " " * spaces[y - x] + a[y+1]
  40. else:
  41. s += a[x]
  42. x = newx + 1
  43. if x == len(a) - 1:
  44. s += a[x]
  45. else:
  46. s += "\n"
  47. return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement