Added input library

master
E. Almqvist 5 years ago
parent 832f49c37d
commit c51b4c30ed
  1. 18
      caesar.py
  2. BIN
      lib/__pycache__/input.cpython-38.pyc
  3. BIN
      lib/__pycache__/vars.cpython-38.pyc
  4. 10
      lib/input.py
  5. 7
      lib/vars.py

@ -1,12 +1,13 @@
import sys from lib.input import *
from lib.vars import alphabet from lib.vars import alphabet
from lib.vars import listToString
if( len(sys.argv) >= 4 ): if( inputHasKeys(["-k", "-i", "-a"]) ):
in_key = int(sys.argv[1]) in_key = int(getValueOfKey("-k"))
in_txt = sys.argv[2] in_txt = getValueOfKey("-i")
in_alphabet = sys.argv[3] in_alphabet = getValueOfKey("-a")
else: else:
print("file.py {int KEY} {string TXT} {string ALPHABET_TYPE}") print("file.py -k {int KEY} -i {string TXT} -a {string ALPHABET_TYPE}")
exit() exit()
alen = len(alphabet[in_alphabet]) alen = len(alphabet[in_alphabet])
@ -16,7 +17,8 @@ decryp_list = [""] * len(in_txt)
for char in txt_list: for char in txt_list:
index = alphabet[in_alphabet].index(char) index = alphabet[in_alphabet].index(char)
print("Decrypting char-index: " + str(txt_list.index(char)) + " (" + char + ")")
index = index + in_key # shift the alphabet index = index + in_key # shift the alphabet
while( index >= alen ): #cycle through the alphabet while( index >= alen ): #cycle through the alphabet
diff = (index + in_key) - (alen - 1) diff = (index + in_key) - (alen - 1)
@ -24,4 +26,4 @@ for char in txt_list:
decryp_list[txt_list.index(char)] = alphabet[in_alphabet][index] decryp_list[txt_list.index(char)] = alphabet[in_alphabet][index]
print(decryp_list) print( "Output: " + listToString(decryp_list) )

@ -0,0 +1,10 @@
import sys
def getValueOfKey(key): # get the value of an input key, example: -c {VALUE}
for i in sys.argv:
if( i == key ):
return sys.argv[sys.argv.index(key) + 1]
return -1 # if no value was found return -1
def inputHasKeys(keyList):
return all(key in sys.argv for key in keyList)

@ -10,3 +10,10 @@ alphabet["ENG"] = eng_alphabet
alphabet["SWE"] = swe_alphabet alphabet["SWE"] = swe_alphabet
# Functions # Functions
def listToString( l ):
returnStr = ""
for char in l:
returnStr += char
return returnStr

Loading…
Cancel
Save