Sha256: f2eaf22a42757253f942472d059c28461ad49f443773c17eaedea81f3c8eb378

Contents?: true

Size: 935 Bytes

Versions: 262

Compression:

Stored size: 935 Bytes

Contents

from string import ascii_lowercase
from time import time
import random


class Cipher(object):

    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

262 entries across 262 versions & 1 rubygems

Version Path
trackler-2.2.1.164 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.163 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.162 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.161 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.160 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.159 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.158 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.157 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.156 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.155 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.154 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.153 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.152 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.151 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.150 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.149 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.148 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.147 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.146 tracks/python/exercises/simple-cipher/example.py
trackler-2.2.1.145 tracks/python/exercises/simple-cipher/example.py