Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  1. Get our chunks chunks.

  2. Round robin through them.

  3. 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))
    
  1. Get our chunks.

  2. Round robin through them.

  3. 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))
    
  1. Get our chunks.

  2. Round robin through them.

  3. 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))
    
deleted 1 character in body
Source Link
Barry
  • 18.5k
  • 1
  • 40
  • 92
  1. Get our chunks.

  2. Round robin through them.

  3. 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))
    
  1. Get our chunks.

  2. Round robin through them.

  3. 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))
    
  1. Get our chunks.

  2. Round robin through them.

  3. 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))
    
added 1189 characters in body
Source Link
Barry
  • 18.5k
  • 1
  • 40
  • 92

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))
  1. Get our chunks.

  2. ZipRound robin through them.

  3. 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!

  1. Get our chunks.

  2. Zip through them.

  3. 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))
  1. Get our chunks.

  2. Round robin through them.

  3. 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))
    
added 1189 characters in body
Source Link
Barry
  • 18.5k
  • 1
  • 40
  • 92
Loading
Source Link
Barry
  • 18.5k
  • 1
  • 40
  • 92
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /