bananalib.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 is_valid(banana, dictstart=0, shiftend=0, dictionary=None):
  35. # defaults
  36. if dictionary is None:
  37. dictionary = [list("bcdfglmnprstvz"), list("aeiou")] # , list("123456")
  38. numdict = len(dictionary)
  39. if (len(banana) - shiftend) % numdict != 0:
  40. return False
  41. for i in range(len(banana)):
  42. r = (numdict + i + dictstart) % numdict
  43. if banana[i] not in dictionary[r]:
  44. return False
  45. return True
  46. def banana2dec(word):
  47. return decoder(word)
  48. def dec2banana(word):
  49. return encoder(word)
  50. def isbanana(word):
  51. return is_valid(word)
  52. def ribes2dec(word):
  53. return decoder(word, 0, 1)
  54. def dec2ribes(word):
  55. return encoder(word, 0, 1)
  56. def isribes(word):
  57. return is_valid(word, 0, 1)
  58. def avocado2dec(word):
  59. return decoder(word, 1, 1)
  60. def dec2avocado(word):
  61. return encoder(word, 1, 1)
  62. def isavocado(word):
  63. return is_valid(word, 1, 1)
  64. def ananas2dec(word):
  65. return decoder(word, 1, 0)
  66. def dec2ananas(word):
  67. return encoder(word, 1, 0)
  68. def isananas(word):
  69. return is_valid(word, 1, 0)
  70. def bananarandom(dictstart=0, shiftend=0, minlength=6, dictionary=None):
  71. import random
  72. # defaults
  73. if dictionary is None:
  74. dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
  75. numdict = len(dictionary)
  76. st = ""
  77. length = 0
  78. i = (numdict - 1 + dictstart + shiftend) % numdict
  79. while not (i == (numdict - 1 + dictstart) % numdict and length >= minlength):
  80. r = random.randint(0, len(dictionary[i]) - 1)
  81. st = dictionary[i][r] + st
  82. i = (i - 1) % numdict
  83. length += 1
  84. return st
  85. if __name__ == "__main__":
  86. print("Ciao sono la libreria banana")