import itertools as it
def verticalize(*strings):
return (''.join(col)
for col in it.izip_longest(
*sorted(strings, key=len, reverse=True),
fillvalue=''))
def verticalize2(*strings):
return (''.join(col)
for col in it.izip_longest(
*strings, fillvalue=' '))
The first one sorts the string by their length.
As it if often the case when using itertools stuff, that code has a very functional intention.
Here an example:
print '-' * 80
verticalized = verticalize('casa', 'python', 'mela')
for row in verticalized:
print row
print '-' * 4
verticalized = verticalize2('casa', 'python', 'mela')
for row in verticalized:
print row
-------------------------------------------------------------------------------- phh yoo tum hse oe n ---- hph oyo mtu ehs oe n
No comments:
Post a Comment