mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-06 01:14:56 -06:00
simple cipher exercise
This commit is contained in:
@@ -31,11 +31,11 @@ locales_currency = {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
def create_entry(date, description, change):
|
||||
return LedgerEntry(datetime.strptime(date, '%Y-%m-%d'), description, change)
|
||||
create_entry = lambda date, description, change: LedgerEntry(datetime.strptime(date, '%Y-%m-%d'), description, change)
|
||||
|
||||
def format_entries(currency, locale, entries):
|
||||
table = []
|
||||
|
||||
@@ -1,26 +1,32 @@
|
||||
from string import ascii_lowercase as low
|
||||
from string import ascii_lowercase as alphabet
|
||||
from pprint import PrettyPrinter
|
||||
import random
|
||||
print = PrettyPrinter().pprint
|
||||
|
||||
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)
|
||||
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 text.translate(self.encode_)
|
||||
return ''.join(self.key_alphabets[index][alphabet.find(char)]
|
||||
for index, char in enumerate(text.lower()))
|
||||
|
||||
def decode(self, text):
|
||||
return text.translate(self.decode_)
|
||||
return ''.join(alphabet[self.key_alphabets[index].find(char)]
|
||||
for index, char in enumerate(text.lower()))
|
||||
|
||||
x = list(zip('iamapandabear', 'ldpdsdqgdehdu'))
|
||||
x = sorted(dict.fromkeys(x))
|
||||
x =
|
||||
from pprint import PrettyPrinter
|
||||
print = PrettyPrinter().pprint
|
||||
# Gets the shifting value of the key at the index specified
|
||||
def keyvalue(self, index):
|
||||
return alphabet.find(self.key[index % len(self.key)])
|
||||
|
||||
# c = Cipher('d' * 18)
|
||||
# print(c.encode('iamapandabear'))
|
||||
random.seed(0)
|
||||
c = Cipher()
|
||||
print(c.decode(c.encode('Hello World')))
|
||||
Reference in New Issue
Block a user