From ce80fe857d466a2d7eace409bb92837480f39c8d Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 1 Oct 2020 20:21:38 +0200 Subject: [PATCH] fix ribes + add avocado --- banana/bananalib.py | 16 ++++++++++++---- tests/test_banana.py | 29 ++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/banana/bananalib.py b/banana/bananalib.py index 00c1686..13297e4 100644 --- a/banana/bananalib.py +++ b/banana/bananalib.py @@ -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) diff --git a/tests/test_banana.py b/tests/test_banana.py index d3edd23..2314791 100644 --- a/tests/test_banana.py +++ b/tests/test_banana.py @@ -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