Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def space(gaps, spaces):
- if gaps == 0:
- return []
- if float(spaces)/gaps == spaces/gaps:
- start = spaces/gaps
- else:
- start = spaces/gaps + 1
- l = []
- for z in range(gaps):
- l.append(start)
- while sum(l) != spaces:
- for x in range(gaps-1, -1, -1):
- l[x] -= 1
- if sum(l) == spaces:
- break
- return l
- def justify(text, width):
- a = text.split(" ")
- x = 0
- s = ""
- newx = 0
- while x < len(a) - 1:
- total = 0
- for y in range(x, len(a)):
- if total + len(a[y]) <= width:
- newx = y
- total += len(a[y]) + 1
- else:
- total -= 1
- break
- if newx == len(a) - 1:
- total -= 1
- spaces = space(newx - x, (newx - x) + width - total)
- if newx - x > 0:
- s += a[x]
- for y in range(x, newx):
- s += " " * spaces[y - x] + a[y+1]
- else:
- s += a[x]
- x = newx + 1
- if x == len(a) - 1:
- s += a[x]
- else:
- s += "\n"
- return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement