diff --git a/Cat2Text.py b/Cat2Text.py index c5bb738..e1f5e23 100644 --- a/Cat2Text.py +++ b/Cat2Text.py @@ -1,16 +1,50 @@ -def translate(data): +from base4 import encode + + +def cat2text(data): data = data.lower() - data = data.replace("meow","0").replace("mrrp", "1").replace("mreow", "2").replace("mrow", "3").replace(" ", "") + data = data.replace("meow", "0").replace("mrrp", "1").replace( + "mreow", "2").replace("mrow", "3").replace(" ", "") seperatewords = data.split(";") finalwordlist = [] for i in seperatewords: letters = [i[e:e+3] for e in range(0, len(i), 3)] lettersinword = [] for x in letters: - letternum = int(x,4) + letternum = int(x, 4) letter = chr(ord('`') + letternum) lettersinword.append(letter) finalwordlist.append(''.join(lettersinword)) return ' '.join(finalwordlist) -catspeak = input("Please input the cat's words: ") -print(translate(catspeak)) + + +def text2cat(data): + data = data.lower() + words = [word for word in data.split(" ")] + output = '' + for word in words: + new_word = '' + for letter in word: + num = ord(letter) - 96 + # add leading zeros + encoded = format(f'{int(encode(num)):03d}') + encoded = encoded.replace("0", "meow ",).replace("1", "mrrp ").replace( + "2", "mreow ").replace("3", "mrow ") + new_word += encoded + + new_word = new_word.strip() + new_word += '; ' + output += new_word + + output = output[:-2] + + return output + + +print("Pick your translation:") +print("1) cat to text") +print("2) text to cat") + +selection = cat2text if int(input()) == 1 else text2cat +catspeak = input("Please input the words: ") +print(selection(catspeak)) diff --git a/base4.py b/base4.py new file mode 100644 index 0000000..57cf54e --- /dev/null +++ b/base4.py @@ -0,0 +1,41 @@ +# stolen from this stack overflow answer and modified: https://stackoverflow.com/a/1119769 + +alphabet = "0123" + +def encode(num): + """Encode a positive number into Base X and return the string. + + Arguments: + - `num`: The number to encode + - `alphabet`: The alphabet to use for encoding + """ + if num == 0: + return alphabet[0] + arr = [] + arr_append = arr.append # Extract bound-method for faster access. + _divmod = divmod # Access to locals is faster. + base = len(alphabet) + while num: + num, rem = _divmod(num, base) + arr_append(alphabet[rem]) + arr.reverse() + return ''.join(arr) + +def decode(string): + """Decode a Base 4 encoded string into the number + + Arguments: + - `string`: The encoded string + - `alphabet`: The alphabet to use for decoding + """ + base = len(alphabet) + strlen = len(string) + num = 0 + + idx = 0 + for char in string: + power = (strlen - (idx + 1)) + num += alphabet.index(char) * (base ** power) + idx += 1 + + return num \ No newline at end of file