bananalib.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """Main module."""
  2. def encoder(num, dictstart=0, shiftend=0, minlength=0, dictionary=None):
  3. if dictionary is None:
  4. dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
  5. numdict = len(dictionary)
  6. v = num
  7. st = ""
  8. length = 0
  9. idx = (numdict - 1 + dictstart + shiftend) % numdict
  10. while not (
  11. v == 0 and idx == (numdict - 1 + dictstart) % numdict and length >= minlength
  12. ):
  13. r = v % len(dictionary[idx])
  14. v = int(v / len(dictionary[idx]))
  15. st = dictionary[idx][r] + st
  16. idx = (idx - 1) % numdict
  17. length += 1
  18. return st
  19. def decoder(banana, dictstart=0, shiftend=0, dictionary=None):
  20. # defaults
  21. if dictionary is None:
  22. dictionary = [list("bcdfglmnprstvz"), list("aeiou")] # , list("123456")
  23. numdict = len(dictionary)
  24. if (len(banana) - shiftend) % numdict != 0:
  25. raise ValueError("Banana non valida")
  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 (ValueError, KeyError):
  32. raise ValueError("Carattere non valido in posizione %d" % i + 1)
  33. return v
  34. def banana2dec(word):
  35. return decoder(word)
  36. def dec2banana(word):
  37. return encoder(word)
  38. def ribes2dec(word):
  39. return decoder(word, 0, 1)
  40. def dec2ribes(word):
  41. return encoder(word, 0, 1)
  42. def avocado2dec(word):
  43. return decoder(word, 1, 1)
  44. def dec2avocado(word):
  45. return encoder(word, 1, 1)
  46. def ananas2dec(word):
  47. return decoder(word, 1, 0)
  48. def dec2ananas(word):
  49. return encoder(word, 1, 0)
  50. def bananarandom(dictstart=0, shiftend=0, minlength=6, dictionary=None):
  51. import random
  52. # defaults
  53. if dictionary is None:
  54. dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
  55. numdict = len(dictionary)
  56. st = ""
  57. length = 0
  58. i = (numdict - 1 + dictstart + shiftend) % numdict
  59. while not (i == (numdict - 1 + dictstart) % numdict and length >= minlength):
  60. r = random.randint(0, len(dictionary[i]) - 1)
  61. st = dictionary[i][r] + st
  62. i = (i - 1) % numdict
  63. length += 1
  64. return st
  65. def isbanana(banana, dictstart=0, shiftend=0, dictionary=None):
  66. # defaults
  67. if dictionary is None:
  68. dictionary = [list("bcdfglmnprstvz"), list("aeiou")] # , list("123456")
  69. numdict = len(dictionary)
  70. if (len(banana) - shiftend) % numdict != 0:
  71. return False
  72. for i in range(len(banana)):
  73. r = (numdict + i + dictstart) % numdict
  74. if banana[i] not in dictionary[r]:
  75. return False
  76. return True
  77. if __name__ == "__main__":
  78. print("Ciao sono la libreria banana")