Sha256: f8ad456ec5dbaa81e0482c44b7cfbca98654496f30d11167be905c0c7345a7bb

Contents?: true

Size: 653 Bytes

Versions: 274

Compression:

Stored size: 653 Bytes

Contents

from math import ceil, sqrt
import sys

if sys.version_info[0] == 2:
    from itertools import izip_longest as zip_longest
else:
    from itertools import zip_longest


def encode(msg):
    msg = _cleanse(msg)
    square_size = int(ceil(sqrt(len(msg))))
    square = _chunks_of(msg, square_size)
    return ' '.join([''.join(col)
                     for col in zip_longest(*square, fillvalue='')])


def _cleanse(s):
    """Lowercase a string and remove punctuation and whitespace
    """
    return ''.join([c for c in s if c.isalnum()]).lower()


def _chunks_of(s, n):
    if len(s) <= n:
        return [s]
    return [s[:n]] + _chunks_of(s[n:], n)

Version data entries

274 entries across 274 versions & 1 rubygems

Version Path
trackler-2.2.1.56 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.55 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.54 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.53 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.52 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.51 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.50 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.49 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.48 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.47 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.46 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.45 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.44 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.43 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.42 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.41 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.40 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.39 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.38 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.37 tracks/python/exercises/crypto-square/example.py