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.139 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.138 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.137 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.136 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.135 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.134 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.133 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.132 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.131 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.130 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.129 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.128 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.127 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.126 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.125 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.124 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.123 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.122 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.121 tracks/python/exercises/crypto-square/example.py
trackler-2.2.1.120 tracks/python/exercises/crypto-square/example.py