Sha256: 04424274cb1413ddbbadb80ab011a51d5da188b63803f42b0bda1b2f8d384452

Contents?: true

Size: 654 Bytes

Versions: 122

Compression:

Stored size: 654 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

122 entries across 122 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.97 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.96 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.95 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.94 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.93 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.92 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.91 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.90 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.89 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.88 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.87 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.86 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.85 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.84 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.83 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.82 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.81 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.80 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.79 tracks/python/exercises/crypto-square/example.py