banana/banana.py

89 lines
2.6 KiB
Python
Raw Normal View History

2020-08-29 18:44:55 +02:00
#!/usr/bin/env python3
2020-08-04 09:59:27 +02:00
def dec2banana(num, alphastart = None, shiftend = None, minlength = None, alphabet = None):
2020-08-31 18:21:24 +02:00
#defaults
if alphastart is None: alphastart = 0
2020-08-31 18:21:24 +02:00
if shiftend is None: shiftend = 0
2020-10-01 17:43:33 +02:00
if minlength is None: minlength = 1
if alphabet is None: alphabet = [list("bcdfglmnprstvz"), list("aeiou")]
2020-08-31 18:21:24 +02:00
numdict = len(alphabet)
2020-08-30 14:59:07 +02:00
v = num
2020-08-04 09:59:27 +02:00
st = ""
2020-08-04 13:13:20 +02:00
l = 0
2020-08-04 09:59:27 +02:00
i = (numdict - 1 + alphastart + shiftend) % numdict
while not (v == 0 and i == (numdict - 1 + alphastart) % numdict and l >= minlength):
r = v % len(alphabet[i])
v = int(v / len(alphabet[i]))
st = alphabet[i][r] + st
2020-08-30 14:59:07 +02:00
i = (i - 1) % numdict
l += 1
return(st)
2020-08-04 09:59:27 +02:00
2020-08-29 18:44:55 +02:00
def banana2dec(banana, alphastart = None, shiftend = None, alphabet = None):
2020-08-31 18:21:24 +02:00
#defaults
if alphastart is None: alphastart = 0
2020-08-31 18:21:24 +02:00
if shiftend is None: shiftend = 0
if alphabet is None: alphabet = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
2020-08-31 18:21:24 +02:00
numdict = len(alphabet)
2020-08-31 23:17:15 +02:00
if (len(banana) - shiftend) % numdict != 0:
return("Banana non valida")
2020-08-29 18:44:55 +02:00
v = 0
for i in range(len(banana)):
r = (numdict + i + alphastart) % numdict
2020-08-29 18:44:55 +02:00
try:
v = v * len(alphabet[r]) + alphabet[r].index(banana[i])
2020-08-29 18:44:55 +02:00
except:
2020-08-31 23:17:15 +02:00
return("Carattere non valido in posizione", i+1)
2020-08-30 14:59:07 +02:00
return(v)
2020-08-04 09:59:27 +02:00
def bananarandom(alphastart = None, shiftend = None, minlength = None, alphabet = None):
2020-08-31 22:36:10 +02:00
import random
#defaults
if alphastart is None: alphastart = 0
2020-08-31 22:36:10 +02:00
if shiftend is None: shiftend = 0
if minlength is None: minlength = 6
if alphabet is None: alphabet = [list("bcdfglmnprstvz"), list("aeiou")]
2020-08-31 22:36:10 +02:00
numdict = len(alphabet)
2020-08-31 22:36:10 +02:00
st = ""
l = 0
i = (numdict - 1 + alphastart + shiftend) % numdict
while not (i == (numdict - 1 + alphastart) % numdict and l >= minlength):
r = random.randint(0, len(alphabet[i]) - 1)
st = alphabet[i][r] + st
2020-08-31 22:36:10 +02:00
i = (i - 1) % numdict
l += 1
return(st)
def isbanana(banana, alphastart = None, shiftend = None, alphabet = None):
2020-08-31 23:17:15 +02:00
#defaults
if alphastart is None: alphastart = 0
2020-08-31 23:17:15 +02:00
if shiftend is None: shiftend = 0
if alphabet is None: alphabet = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
2020-08-31 23:17:15 +02:00
numdict = len(alphabet)
2020-08-31 23:17:15 +02:00
if (len(banana) - shiftend) % numdict != 0:
return(False)
for i in range(len(banana)):
r = (numdict + i + alphastart) % numdict
if banana[i] not in alphabet[r]:
2020-08-31 23:17:15 +02:00
return(False)
2020-08-31 22:36:10 +02:00
2020-08-31 23:17:15 +02:00
return(True)
2020-08-31 22:36:10 +02:00
2020-08-04 09:59:27 +02:00
if __name__ == "__main__":
2020-08-29 18:44:55 +02:00
print("Ciao sono la libreria banana")