diff --git a/python/ledger/ledger.py b/python/ledger/ledger.py index ae31bdb..fdbb2e7 100644 --- a/python/ledger/ledger.py +++ b/python/ledger/ledger.py @@ -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 = [] diff --git a/python/simple-cipher/simple_cipher.py b/python/simple-cipher/simple_cipher.py index 15bdf2e..e18956c 100644 --- a/python/simple-cipher/simple_cipher.py +++ b/python/simple-cipher/simple_cipher.py @@ -1,26 +1,32 @@ -from string import ascii_lowercase as low - -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 string import ascii_lowercase as alphabet from pprint import PrettyPrinter +import random print = PrettyPrinter().pprint -# c = Cipher('d' * 18) -# print(c.encode('iamapandabear')) \ No newline at end of file +class Cipher(object): + 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'))) \ No newline at end of file