Sha256: 7307991f5447a33236f0c747ad1dd235ec902cc49275937306158231ed35bc64

Contents?: true

Size: 499 Bytes

Versions: 269

Compression:

Stored size: 499 Bytes

Contents

from itertools import cycle, chain


def fence_pattern(rails, size):
    zig_zag = cycle(chain(range(rails), range(rails - 2, 0, -1)))
    return zip(zig_zag, range(size))


def encode(msg, rails):
    fence = fence_pattern(rails, len(msg))
    return ''.join(msg[i] for _, i in sorted(fence))


def decode(msg, rails):
    fence = fence_pattern(rails, len(msg))
    fence_msg = zip(msg, sorted(fence))
    return ''.join(
        char for char, _ in sorted(fence_msg, key=lambda item: item[1][1]))

Version data entries

269 entries across 269 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.179 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.178 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.177 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.176 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.175 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.174 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.173 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.172 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.171 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.170 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.169 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.167 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.166 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.165 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.164 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.163 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.162 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.161 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.2.1.160 tracks/python/exercises/rail-fence-cipher/example.py