add ribes
This commit is contained in:
parent
96aa4d8153
commit
b4ba921ed3
2 changed files with 58 additions and 8 deletions
|
@ -1,7 +1,7 @@
|
|||
"""Main module."""
|
||||
|
||||
|
||||
def dec2banana(num, dictstart=0, shiftend=0, minlength=0, dictionary=None):
|
||||
def encoder(num, dictstart=0, shiftend=0, minlength=0, dictionary=None):
|
||||
if dictionary is None:
|
||||
dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
|
||||
|
||||
|
@ -11,7 +11,9 @@ def dec2banana(num, dictstart=0, shiftend=0, minlength=0, dictionary=None):
|
|||
length = 0
|
||||
|
||||
idx = (numdict - 1 + dictstart + shiftend) % numdict
|
||||
while not (v == 0 and idx == (numdict - 1 + dictstart) % numdict and length >= minlength):
|
||||
while not (
|
||||
v == 0 and idx == (numdict - 1 + dictstart) % numdict and length >= minlength
|
||||
):
|
||||
r = v % len(dictionary[idx])
|
||||
v = int(v / len(dictionary[idx]))
|
||||
st = dictionary[idx][r] + st
|
||||
|
@ -21,7 +23,7 @@ def dec2banana(num, dictstart=0, shiftend=0, minlength=0, dictionary=None):
|
|||
return st
|
||||
|
||||
|
||||
def banana2dec(banana, dictstart=0, shiftend=0, dictionary=None):
|
||||
def decoder(banana, dictstart=0, shiftend=0, dictionary=None):
|
||||
# defaults
|
||||
if dictionary is None:
|
||||
dictionary = [list("bcdfglmnprstvz"), list("aeiou")] # , list("123456")
|
||||
|
@ -40,6 +42,22 @@ def banana2dec(banana, dictstart=0, shiftend=0, dictionary=None):
|
|||
return v
|
||||
|
||||
|
||||
def banana2dec(word):
|
||||
return decoder(word)
|
||||
|
||||
|
||||
def dec2banana(word):
|
||||
return encoder(word)
|
||||
|
||||
|
||||
def ribes2dec(word):
|
||||
return decoder(word, 1, 1)
|
||||
|
||||
|
||||
def dec2ribes(word):
|
||||
return encoder(word, 1, 1)
|
||||
|
||||
|
||||
def bananarandom(dictstart=0, shiftend=0, minlength=6, dictionary=None):
|
||||
import random
|
||||
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
"""Tests for `banana` package."""
|
||||
|
||||
from banana import banana2dec, dec2banana
|
||||
from banana import banana2dec, dec2banana, dec2ribes, ribes2dec
|
||||
|
||||
known_conversions = {
|
||||
banana_conversions = {
|
||||
"ba": 0,
|
||||
"baba": 0,
|
||||
"be": 1,
|
||||
|
@ -15,27 +15,59 @@ known_conversions = {
|
|||
"banana": 2485,
|
||||
}
|
||||
|
||||
ribes_conversions = {
|
||||
"a": 0,
|
||||
"aca": 5,
|
||||
"ada": 10,
|
||||
"afa": 15,
|
||||
"aga": 20,
|
||||
"ala": 25,
|
||||
"ama": 30,
|
||||
"ana": 35,
|
||||
"apa": 40,
|
||||
"ara": 45,
|
||||
"asa": 50,
|
||||
"ata": 55,
|
||||
"ava": 60,
|
||||
"aza": 65,
|
||||
"eba": 70,
|
||||
"eca": 75,
|
||||
"eda": 80,
|
||||
"efa": 85,
|
||||
"ega": 90,
|
||||
}
|
||||
|
||||
|
||||
def test_banana_to_dec_known():
|
||||
for word, value in known_conversions.items():
|
||||
for word, value in banana_conversions.items():
|
||||
assert banana2dec(word) == value
|
||||
|
||||
|
||||
def test_banana2dec_prefix_ba():
|
||||
"""un ba all'inizio non cambia nulla!"""
|
||||
for word in known_conversions:
|
||||
for word in banana_conversions:
|
||||
value = banana2dec(word)
|
||||
for prefix in ("ba", "baba", "bababa"):
|
||||
assert banana2dec(prefix + word) == value
|
||||
|
||||
|
||||
def test_dec_to_banana_known():
|
||||
for word, value in known_conversions.items():
|
||||
for word, value in banana_conversions.items():
|
||||
if word.startswith("ba"):
|
||||
continue
|
||||
assert dec2banana(value) == word
|
||||
|
||||
|
||||
def test_ribes_to_dec_known():
|
||||
for word, value in ribes_conversions.items():
|
||||
assert ribes2dec(word) == value
|
||||
|
||||
|
||||
def test_dec_to_ribes_known():
|
||||
for word, value in ribes_conversions.items():
|
||||
assert dec2ribes(value) == word
|
||||
|
||||
|
||||
def test_answer_to_life_the_universe_and_everything():
|
||||
banana = banana2dec("banana")
|
||||
assert banana != 42
|
||||
|
|
Loading…
Reference in a new issue