Godtone
Joined 17 December 2020
→My Python 3 code: hopefully fixed padding |
|||
| Line 632: | Line 632: | ||
return str(n)+'/'+str(d) | return str(n)+'/'+str(d) | ||
return '[not interval]' | return '[not interval]' | ||
def | def left_pad(what,width,space=' '): | ||
return (width-len(what))*space + what | |||
def right_pad(what,width,space=' '): | |||
return what + (width-len(what))*space | |||
return | def pad(what,width,centre='/',space=' '): | ||
if len(what) >= width: | |||
return what | |||
lspaces = (width+1)//2 | |||
rspaces = width - lspaces | |||
c = what.find(centre) | |||
if c != -1: # if centre present in what | |||
c += (len(centre)+1)//2 | |||
else: | |||
c = (len(what)+1)//2 | |||
lspaces -= c | |||
rspaces -= len(what)-c | |||
if lspaces >= 0 and rspaces >= 0: | |||
return lspaces*space + what + rspaces*space | |||
if rspaces < 0 and lspaces >= 0: | |||
return (lspaces+rspaces)*space + what | |||
if lspaces < 0 and rspaces >= 0: | |||
return what + (lspaces+rspaces)*space | |||
return what | |||
# Converts a prime factorisation into a prettier and potentially | # Converts a prime factorisation into a prettier and potentially | ||
# more readable (simple) string representation. (Not for subgroup-factorisations.) | # more readable (simple) string representation. (Not for subgroup-factorisations.) | ||