banana.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. def dec2banana(num, dictstart = None, shiftend = None, minlength = None, dictionary = None):
  3. #defaults
  4. if dictstart is None: dictstart = 0
  5. if shiftend is None: shiftend = 0
  6. if minlength is None: minlength = 0
  7. if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
  8. numdict = len(dictionary)
  9. v = num
  10. st = ""
  11. l = 0
  12. i = (numdict - 1 + dictstart + shiftend) % numdict
  13. while not (v == 0 and i == (numdict - 1 + dictstart) % numdict and l >= minlength):
  14. r = v % len(dictionary[i])
  15. v = int(v / len(dictionary[i]))
  16. st = dictionary[i][r] + st
  17. i = (i - 1) % numdict
  18. l += 1
  19. return(st)
  20. def banana2dec(banana, dictstart = None, shiftend = None, dictionary = None):
  21. #defaults
  22. if dictstart is None: dictstart = 0
  23. if shiftend is None: shiftend = 0
  24. if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
  25. numdict = len(dictionary)
  26. v = 0
  27. for i in range(len(banana)):
  28. r = (numdict + i + dictstart) % numdict
  29. try:
  30. v = v * len(dictionary[r]) + dictionary[r].index(banana[i])
  31. except:
  32. print("Carattere non valido in posizione", i+1)
  33. return()
  34. return(v)
  35. def bananarandom(dictstart = None, shiftend = None, minlength = None, dictionary = None):
  36. import random
  37. #defaults
  38. if dictstart is None: dictstart = 0
  39. if shiftend is None: shiftend = 0
  40. if minlength is None: minlength = 6
  41. if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
  42. numdict = len(dictionary)
  43. st = ""
  44. l = 0
  45. i = (numdict - 1 + dictstart + shiftend) % numdict
  46. while not (i == (numdict - 1 + dictstart) % numdict and l >= minlength):
  47. r = random.randint(0, len(dictionary[i]) - 1)
  48. st = dictionary[i][r] + st
  49. i = (i - 1) % numdict
  50. l += 1
  51. return(st)
  52. if __name__ == "__main__":
  53. print("Ciao sono la libreria banana")