Get our
chunks
.Round robin through them.
Join the result
def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in range(0, len(l), n): yield l[i:i+n] def encrypt(s): n = int(math.sqrt(len(s)) chunked = chunks(s, n) return ''.join(roundrobin(*chunked))
Get our
chunks
.Round robin through them.
Join the result
def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in range(0, len(l), n): yield l[i:i+n] def encrypt(s): n = int(math.sqrt(len(s)) chunked = chunks(s, n) return ''.join(roundrobin(*chunked))
Get our
chunks
.Round robin through them.
Join the result
def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in xrangerange(0, len(l), n): yield l[i:i+n] def encrypt(s): n = int(math.sqrt(len(s)) chunked = chunks(s, n) return ''.join(roundrobin(*chunked))
Get our
chunks
.Round robin through them.
Join the result
def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in xrange(0, len(l), n): yield l[i:i+n] def encrypt(s): n = int(math.sqrt(len(s)) chunked = chunks(s, n) return ''.join(roundrobin(*chunked))
Get our
chunks
.Round robin through them.
Join the result
def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in range(0, len(l), n): yield l[i:i+n] def encrypt(s): n = int(math.sqrt(len(s)) chunked = chunks(s, n) return ''.join(roundrobin(*chunked))
That's precisely what zip_longest
That is, we're round robining through each row. There's a recipe for! that:
def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).__next__ for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
Get our
chunks
.ZipRound robin through them.
Join the result
def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in xrange(0, len(l), n): yield l[i:i+n] def encrypt(s): n = int(math.sqrt(len(s)) chunked = chunks(s, n) zipped = itertools.zip_longest(*chunked, fillvalue='') return ''.join(''.joinroundrobin(v*chunked) for v in zipped)
That's precisely what zip_longest
is for!
Get our
chunks
.Zip through them.
Join the result
def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in xrange(0, len(l), n): yield l[i:i+n] def encrypt(s): n = int(math.sqrt(len(s)) chunked = chunks(s, n) zipped = itertools.zip_longest(*chunked, fillvalue='') return ''.join(''.join(v) for v in zipped)
That is, we're round robining through each row. There's a recipe for that:
def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).__next__ for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
Get our
chunks
.Round robin through them.
Join the result
def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in xrange(0, len(l), n): yield l[i:i+n] def encrypt(s): n = int(math.sqrt(len(s)) chunked = chunks(s, n) return ''.join(roundrobin(*chunked))