Sha256: 4e0d151206f54405eb32bf01e95c5c23cf87ed42df1ea962bf94bbd049dd326b

Contents?: true

Size: 927 Bytes

Versions: 119

Compression:

Stored size: 927 Bytes

Contents

from string import ascii_lowercase
from time import time
import random


class Cipher:

    def __init__(self, key=None):
        if not key:
            random.seed(time())
            key = ''.join(random.choice(ascii_lowercase) for i in range(100))
        elif not key.isalpha() or not key.islower():
            raise ValueError('Wrong key parameter!')
        self.key = key

    def encode(self, text):
        text = ''.join(c for c in text if c.isalpha()).lower()
        key = self.key * (len(text) // len(self.key) + 1)
        return ''.join(chr((ord(c) - 194 + ord(k)) % 26 + 97)
                       for c, k in zip(text, key))

    def decode(self, text):
        key = self.key * (len(text) // len(self.key) + 1)
        return ''.join(chr((ord(c) - ord(k) + 26) % 26 + 97)
                       for c, k in zip(text, key))


class Caesar(Cipher):

    def __init__(self):
        Cipher.__init__(self, 'd')

Version data entries

119 entries across 119 versions & 1 rubygems

Version Path
trackler-2.0.8.18 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.17 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.16 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.15 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.14 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.13 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.12 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.11 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.10 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.9 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.8 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.7 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.6 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.5 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.4 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.3 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.2 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.8.1 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.7.0 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.6.44 tracks/python/exercises/simple-cipher/example.py