add base4 functions and text-to-cat translation
This commit is contained in:
parent
2b54128444
commit
7770254825
2 changed files with 80 additions and 5 deletions
44
Cat2Text.py
44
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))
|
||||
|
|
41
base4.py
Normal file
41
base4.py
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue