forked from itec/banana
Changed dictionary to alphabet + argparse list
This commit is contained in:
parent
7f668fe8de
commit
9a38bf63a1
5 changed files with 63 additions and 45 deletions
58
banana.py
58
banana.py
|
@ -1,81 +1,81 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
def dec2banana(num, dictstart = None, shiftend = None, minlength = None, dictionary = None):
|
def dec2banana(num, alphastart = None, shiftend = None, minlength = None, alphabet = None):
|
||||||
#defaults
|
#defaults
|
||||||
if dictstart is None: dictstart = 0
|
if alphastart is None: alphastart = 0
|
||||||
if shiftend is None: shiftend = 0
|
if shiftend is None: shiftend = 0
|
||||||
if minlength is None: minlength = 0
|
if minlength is None: minlength = 0
|
||||||
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
|
if alphabet is None: alphabet = [list("bcdfglmnprstvz"), list("aeiou")]
|
||||||
|
|
||||||
numdict = len(dictionary)
|
numdict = len(alphabet)
|
||||||
v = num
|
v = num
|
||||||
st = ""
|
st = ""
|
||||||
l = 0
|
l = 0
|
||||||
|
|
||||||
i = (numdict - 1 + dictstart + shiftend) % numdict
|
i = (numdict - 1 + alphastart + shiftend) % numdict
|
||||||
while not (v == 0 and i == (numdict - 1 + dictstart) % numdict and l >= minlength):
|
while not (v == 0 and i == (numdict - 1 + alphastart) % numdict and l >= minlength):
|
||||||
r = v % len(dictionary[i])
|
r = v % len(alphabet[i])
|
||||||
v = int(v / len(dictionary[i]))
|
v = int(v / len(alphabet[i]))
|
||||||
st = dictionary[i][r] + st
|
st = alphabet[i][r] + st
|
||||||
i = (i - 1) % numdict
|
i = (i - 1) % numdict
|
||||||
l += 1
|
l += 1
|
||||||
|
|
||||||
return(st)
|
return(st)
|
||||||
|
|
||||||
|
|
||||||
def banana2dec(banana, dictstart = None, shiftend = None, dictionary = None):
|
def banana2dec(banana, alphastart = None, shiftend = None, alphabet = None):
|
||||||
#defaults
|
#defaults
|
||||||
if dictstart is None: dictstart = 0
|
if alphastart is None: alphastart = 0
|
||||||
if shiftend is None: shiftend = 0
|
if shiftend is None: shiftend = 0
|
||||||
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
|
if alphabet is None: alphabet = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
|
||||||
|
|
||||||
numdict = len(dictionary)
|
numdict = len(alphabet)
|
||||||
if (len(banana) - shiftend) % numdict != 0:
|
if (len(banana) - shiftend) % numdict != 0:
|
||||||
return("Banana non valida")
|
return("Banana non valida")
|
||||||
v = 0
|
v = 0
|
||||||
for i in range(len(banana)):
|
for i in range(len(banana)):
|
||||||
r = (numdict + i + dictstart) % numdict
|
r = (numdict + i + alphastart) % numdict
|
||||||
try:
|
try:
|
||||||
v = v * len(dictionary[r]) + dictionary[r].index(banana[i])
|
v = v * len(alphabet[r]) + alphabet[r].index(banana[i])
|
||||||
except:
|
except:
|
||||||
return("Carattere non valido in posizione", i+1)
|
return("Carattere non valido in posizione", i+1)
|
||||||
|
|
||||||
return(v)
|
return(v)
|
||||||
|
|
||||||
def bananarandom(dictstart = None, shiftend = None, minlength = None, dictionary = None):
|
def bananarandom(alphastart = None, shiftend = None, minlength = None, alphabet = None):
|
||||||
import random
|
import random
|
||||||
|
|
||||||
#defaults
|
#defaults
|
||||||
if dictstart is None: dictstart = 0
|
if alphastart is None: alphastart = 0
|
||||||
if shiftend is None: shiftend = 0
|
if shiftend is None: shiftend = 0
|
||||||
if minlength is None: minlength = 6
|
if minlength is None: minlength = 6
|
||||||
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
|
if alphabet is None: alphabet = [list("bcdfglmnprstvz"), list("aeiou")]
|
||||||
|
|
||||||
numdict = len(dictionary)
|
numdict = len(alphabet)
|
||||||
st = ""
|
st = ""
|
||||||
l = 0
|
l = 0
|
||||||
|
|
||||||
i = (numdict - 1 + dictstart + shiftend) % numdict
|
i = (numdict - 1 + alphastart + shiftend) % numdict
|
||||||
while not (i == (numdict - 1 + dictstart) % numdict and l >= minlength):
|
while not (i == (numdict - 1 + alphastart) % numdict and l >= minlength):
|
||||||
r = random.randint(0, len(dictionary[i]) - 1)
|
r = random.randint(0, len(alphabet[i]) - 1)
|
||||||
st = dictionary[i][r] + st
|
st = alphabet[i][r] + st
|
||||||
i = (i - 1) % numdict
|
i = (i - 1) % numdict
|
||||||
l += 1
|
l += 1
|
||||||
|
|
||||||
return(st)
|
return(st)
|
||||||
|
|
||||||
def isbanana(banana, dictstart = None, shiftend = None, dictionary = None):
|
def isbanana(banana, alphastart = None, shiftend = None, alphabet = None):
|
||||||
#defaults
|
#defaults
|
||||||
if dictstart is None: dictstart = 0
|
if alphastart is None: alphastart = 0
|
||||||
if shiftend is None: shiftend = 0
|
if shiftend is None: shiftend = 0
|
||||||
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
|
if alphabet is None: alphabet = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
|
||||||
|
|
||||||
numdict = len(dictionary)
|
numdict = len(alphabet)
|
||||||
if (len(banana) - shiftend) % numdict != 0:
|
if (len(banana) - shiftend) % numdict != 0:
|
||||||
return(False)
|
return(False)
|
||||||
for i in range(len(banana)):
|
for i in range(len(banana)):
|
||||||
r = (numdict + i + dictstart) % numdict
|
r = (numdict + i + alphastart) % numdict
|
||||||
if banana[i] not in dictionary[r]:
|
if banana[i] not in alphabet[r]:
|
||||||
return(False)
|
return(False)
|
||||||
|
|
||||||
return(True)
|
return(True)
|
||||||
|
|
|
@ -5,9 +5,14 @@ import banana
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description="Convert banana string to dec")
|
parser = argparse.ArgumentParser(description="Convert banana string to dec")
|
||||||
parser.add_argument("banana", help="String to be converted")
|
parser.add_argument("banana", help="String to be converted")
|
||||||
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs='+')
|
parser.add_argument("--alphabet", help="Set alphabet")
|
||||||
parser.add_argument("--dictstart", help="Set starting dictionary", type=int)
|
parser.add_argument("--alphastart", help="Set starting alphabet", type=int)
|
||||||
parser.add_argument("--shiftend", help="Set shift for ending dictionary", type=int)
|
parser.add_argument("--shiftend", help="Set shift for ending alphabet", type=int)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
print(banana.banana2dec(args.banana, args.dictstart, args.shiftend, args.dictionary))
|
alphabet = None
|
||||||
|
if not args.alphabet is None:
|
||||||
|
alphabet = [list(i) for i in args.alphabet.split(",")]
|
||||||
|
|
||||||
|
print(banana.banana2dec(args.banana, args.alphastart, args.shiftend, alphabet))
|
||||||
|
|
|
@ -4,10 +4,14 @@ import banana
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description="Generate random banana")
|
parser = argparse.ArgumentParser(description="Generate random banana")
|
||||||
parser.add_argument("--dictstart", help="Set starting dictionary", type=int)
|
parser.add_argument("--alphastart", help="Set starting alphabet", type=int)
|
||||||
parser.add_argument("--shiftend", help="Set shift for ending dictionary", type=int)
|
parser.add_argument("--shiftend", help="Set shift for ending alphabet", type=int)
|
||||||
parser.add_argument("--minlength", help="Set minimum length", type=int)
|
parser.add_argument("--minlength", help="Set minimum length", type=int)
|
||||||
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs='+')
|
parser.add_argument("--alphabet", help="Set alphabet")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
print(banana.bananarandom(args.dictstart, args.shiftend, args.minlength, args.dictionary))
|
alphabet = None
|
||||||
|
if not args.alphabet is None:
|
||||||
|
alphabet = [list(i) for i in args.alphabet.split(",")]
|
||||||
|
|
||||||
|
print(banana.bananarandom(args.alphastart, args.shiftend, args.minlength, alphabet))
|
||||||
|
|
|
@ -5,10 +5,15 @@ import banana
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description="Convert dec number to banana")
|
parser = argparse.ArgumentParser(description="Convert dec number to banana")
|
||||||
parser.add_argument("num", help="Number to be converted", type=int)
|
parser.add_argument("num", help="Number to be converted", type=int)
|
||||||
parser.add_argument("--dictstart", help="Set starting dictionary", type=int)
|
parser.add_argument("--alphastart", help="Set starting alphabet", type=int)
|
||||||
parser.add_argument("--shiftend", help="Set shift for ending dictionary", type=int)
|
parser.add_argument("--shiftend", help="Set shift for ending alphabet", type=int)
|
||||||
parser.add_argument("--minlength", help="Set minimum length", type=int)
|
parser.add_argument("--minlength", help="Set minimum length", type=int)
|
||||||
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs='+')
|
parser.add_argument("--alphabet", help="Set alphabet")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
print(banana.dec2banana(args.num, args.dictstart, args.shiftend, args.minlength, args.dictionary))
|
alphabet = None
|
||||||
|
if not args.alphabet is None:
|
||||||
|
alphabet = [list(i) for i in args.alphabet.split(",")]
|
||||||
|
|
||||||
|
print(banana.dec2banana(args.num, args.alphastart, args.shiftend, args.minlength, alphabet))
|
||||||
|
|
12
isbanana.py
12
isbanana.py
|
@ -5,9 +5,13 @@ import banana
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description="Checks if string is banana")
|
parser = argparse.ArgumentParser(description="Checks if string is banana")
|
||||||
parser.add_argument("banana", help="String to be checked")
|
parser.add_argument("banana", help="String to be checked")
|
||||||
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs='+')
|
parser.add_argument("--alphabet", help="Set alphabet")
|
||||||
parser.add_argument("--dictstart", help="Set starting dictionary", type=int)
|
parser.add_argument("--alphastart", help="Set starting alphabet", type=int)
|
||||||
parser.add_argument("--shiftend", help="Set shift for ending dictionary", type=int)
|
parser.add_argument("--shiftend", help="Set shift for ending alphabet", type=int)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
print(banana.isbanana(args.banana, args.dictstart, args.shiftend, args.dictionary))
|
alphabet = None
|
||||||
|
if not args.alphabet is None:
|
||||||
|
alphabet = [list(i) for i in args.alphabet.split(",")]
|
||||||
|
|
||||||
|
print(banana.isbanana(args.banana, args.alphastart, args.shiftend, alphabet))
|
||||||
|
|
Loading…
Reference in a new issue