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.180 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.179 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.178 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.177 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.176 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.175 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.174 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.173 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.172 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.171 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.170 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.169 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.167 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.166 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.165 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.164 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.163 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.162 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.161 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.160 tracks/python/exercises/crypto-square/example.py