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.0.8 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.0.7 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.0.6 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.0.5 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.0.4 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.0.3 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.0.2 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.0.1 tracks/python/exercises/simple-cipher/example.py
trackler-2.0.0.0 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.4.1 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.4.0 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.3.0 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.2.1 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.2.0 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.1.2 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.1.1 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.1.0 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.0.1 tracks/python/exercises/simple-cipher/example.py
trackler-1.0.0 tracks/python/exercises/simple-cipher/example.py