simple cipher exercise

This commit is contained in:
Xevion
2019-07-24 21:27:10 -05:00
parent 5c541fc03e
commit 52d101cf0a
2 changed files with 32 additions and 26 deletions

View File

@@ -31,11 +31,11 @@ locales_currency = {
} }
class LedgerEntry(object): class LedgerEntry(object):
def __init__(self, date, description, change): def __init__(self, date, desc
6ption, change):
self.date, self.description, self.change = date, description, change self.date, self.description, self.change = date, description, change
def create_entry(date, description, change): create_entry = lambda date, description, change: LedgerEntry(datetime.strptime(date, '%Y-%m-%d'), description, change)
return LedgerEntry(datetime.strptime(date, '%Y-%m-%d'), description, change)
def format_entries(currency, locale, entries): def format_entries(currency, locale, entries):
table = [] table = []

View File

@@ -1,26 +1,32 @@
from string import ascii_lowercase as low from string import ascii_lowercase as alphabet
class Cipher(object):
def __init__(self, key=None):
self.key = key
self.shift = 27 - (sum([low.index(char) for char in self.key]) % 25)
shifted = low[self.shift:] + low[:self.shift]
print(low)
print(shifted)
self.encode_ = str.maketrans(low, shifted)
self.decode_ = str.maketrans(shifted, low)
def encode(self, text):
return text.translate(self.encode_)
def decode(self, text):
return text.translate(self.decode_)
x = list(zip('iamapandabear', 'ldpdsdqgdehdu'))
x = sorted(dict.fromkeys(x))
x =
from pprint import PrettyPrinter from pprint import PrettyPrinter
import random
print = PrettyPrinter().pprint print = PrettyPrinter().pprint
# c = Cipher('d' * 18) class Cipher(object):
# print(c.encode('iamapandabear')) def __init__(self, key=None):
if key:
self.key = ''.join(dict.fromkeys(self.key)) # Key with repeated characters is completely useless
elif not key:
self.key = list(alphabet)
random.shuffle(self.key)
self.key = ''.join(self.key)
key_values = [self.keyvalue(i) for i in range(len(self.key))]
self.key_alphabets = [(alphabet[index:] + alphabet[: index]) for index in key_values]
def encode(self, text):
return ''.join(self.key_alphabets[index][alphabet.find(char)]
for index, char in enumerate(text.lower()))
def decode(self, text):
return ''.join(alphabet[self.key_alphabets[index].find(char)]
for index, char in enumerate(text.lower()))
# Gets the shifting value of the key at the index specified
def keyvalue(self, index):
return alphabet.find(self.key[index % len(self.key)])
random.seed(0)
c = Cipher()
print(c.decode(c.encode('Hello World')))