Sha256: 80a4188bb500e20502f806712424d8b153a34c193f4c0b0b31b0933ee218e098

Contents?: true

Size: 490 Bytes

Versions: 127

Compression:

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

127 entries across 127 versions & 1 rubygems

Version Path
trackler-2.0.8.27 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.26 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.24 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.23 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.22 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.21 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.20 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.19 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.18 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.17 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.16 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.15 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.14 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.13 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.12 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.11 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.10 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.9 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.8 tracks/python/exercises/rail-fence-cipher/example.py
trackler-2.0.8.7 tracks/python/exercises/rail-fence-cipher/example.py