1
0
Fork 0
forked from itec/banana

fix ribes + add avocado

This commit is contained in:
boyska 2020-10-01 20:21:38 +02:00
parent b4ba921ed3
commit ce80fe857d
2 changed files with 36 additions and 9 deletions

View file

@ -30,14 +30,14 @@ def decoder(banana, dictstart=0, shiftend=0, dictionary=None):
numdict = len(dictionary)
if (len(banana) - shiftend) % numdict != 0:
return "Banana non valida"
raise ValueError("Banana non valida")
v = 0
for i in range(len(banana)):
r = (numdict + i + dictstart) % numdict
try:
v = v * len(dictionary[r]) + dictionary[r].index(banana[i])
except (ValueError, KeyError) as e:
return "Carattere non valido in posizione", i + 1
except (ValueError, KeyError):
raise ValueError("Carattere non valido in posizione %d" % i + 1)
return v
@ -51,10 +51,18 @@ def dec2banana(word):
def ribes2dec(word):
return decoder(word, 1, 1)
return decoder(word, 0, 1)
def dec2ribes(word):
return encoder(word, 0, 1)
def avocado2dec(word):
return decoder(word, 1, 1)
def dec2avocado(word):
return encoder(word, 1, 1)

View file

@ -1,8 +1,10 @@
#!/usr/bin/env python
"""Tests for `banana` package."""
import pytest
from banana import banana2dec, dec2banana, dec2ribes, ribes2dec
from banana import (avocado2dec, banana2dec, dec2avocado, dec2banana,
dec2ribes, ribes2dec)
banana_conversions = {
"ba": 0,
@ -15,7 +17,7 @@ banana_conversions = {
"banana": 2485,
}
ribes_conversions = {
avocado_conversions = {
"a": 0,
"aca": 5,
"ada": 10,
@ -37,6 +39,13 @@ ribes_conversions = {
"ega": 90,
}
ribes_conversions = {"b": 0, "c": 1, "z": 13, "beb": 14, "bec": 15}
@pytest.fixture(params=ribes_conversions.items())
def ribes_known(request):
yield request.param
def test_banana_to_dec_known():
for word, value in banana_conversions.items():
@ -58,9 +67,9 @@ def test_dec_to_banana_known():
assert dec2banana(value) == word
def test_ribes_to_dec_known():
for word, value in ribes_conversions.items():
assert ribes2dec(word) == value
def test_ribes_to_dec_known(ribes_known):
word, value = ribes_known
assert ribes2dec(word) == value
def test_dec_to_ribes_known():
@ -68,6 +77,16 @@ def test_dec_to_ribes_known():
assert dec2ribes(value) == word
def test_avocado_to_dec_known():
for word, value in avocado_conversions.items():
assert avocado2dec(word) == value
def test_dec_to_avocado_known():
for word, value in avocado_conversions.items():
assert dec2avocado(value) == word
def test_answer_to_life_the_universe_and_everything():
banana = banana2dec("banana")
assert banana != 42