refactor is_valid

This commit is contained in:
boyska 2020-10-02 10:46:51 +02:00
parent e257c5630c
commit 5d25fd334e
3 changed files with 75 additions and 18 deletions

View file

@ -42,6 +42,22 @@ def decoder(banana, dictstart=0, shiftend=0, dictionary=None):
return v
def is_valid(banana, dictstart=0, shiftend=0, dictionary=None):
# defaults
if dictionary is None:
dictionary = [list("bcdfglmnprstvz"), list("aeiou")] # , list("123456")
numdict = len(dictionary)
if (len(banana) - shiftend) % numdict != 0:
return False
for i in range(len(banana)):
r = (numdict + i + dictstart) % numdict
if banana[i] not in dictionary[r]:
return False
return True
def banana2dec(word):
return decoder(word)
@ -50,6 +66,10 @@ def dec2banana(word):
return encoder(word)
def isbanana(word):
return is_valid(word)
def ribes2dec(word):
return decoder(word, 0, 1)
@ -58,6 +78,10 @@ def dec2ribes(word):
return encoder(word, 0, 1)
def isribes(word):
return is_valid(word, 0, 1)
def avocado2dec(word):
return decoder(word, 1, 1)
@ -66,6 +90,10 @@ def dec2avocado(word):
return encoder(word, 1, 1)
def isavocado(word):
return is_valid(word, 1, 1)
def ananas2dec(word):
return decoder(word, 1, 0)
@ -74,6 +102,10 @@ def dec2ananas(word):
return encoder(word, 1, 0)
def isananas(word):
return is_valid(word, 1, 0)
def bananarandom(dictstart=0, shiftend=0, minlength=6, dictionary=None):
import random
@ -95,21 +127,5 @@ def bananarandom(dictstart=0, shiftend=0, minlength=6, dictionary=None):
return st
def isbanana(banana, dictstart=0, shiftend=0, dictionary=None):
# defaults
if dictionary is None:
dictionary = [list("bcdfglmnprstvz"), list("aeiou")] # , list("123456")
numdict = len(dictionary)
if (len(banana) - shiftend) % numdict != 0:
return False
for i in range(len(banana)):
r = (numdict + i + dictstart) % numdict
if banana[i] not in dictionary[r]:
return False
return True
if __name__ == "__main__":
print("Ciao sono la libreria banana")

View file

@ -118,7 +118,7 @@ def isbanana():
)
args = parser.parse_args()
print(banana.isbanana(args.banana, args.dictstart, args.shiftend, args.dictionary))
print(banana.isbanana(args.banana))
def main():

View file

@ -4,7 +4,8 @@
import pytest
from banana import (ananas2dec, avocado2dec, banana2dec, dec2ananas,
dec2avocado, dec2banana, dec2ribes, ribes2dec)
dec2avocado, dec2banana, dec2ribes, isananas, isavocado,
isbanana, isribes, ribes2dec)
banana_conversions = {"be": 1, "beba": 70, "zu": 69, "bezu": 139, "nana": 2485}
@ -88,6 +89,16 @@ def test_dec_to_banana_known(banana_known):
assert dec2banana(value) == word
def test_banana_is_banana(banana_known):
assert isbanana(banana_known[0])
def test_banana_is_only_banana(banana_known):
assert not isribes(banana_known[0])
assert not isananas(banana_known[0])
assert not isavocado(banana_known[0])
def test_banana2dec_prefix_ba(banana_known):
"""un ba all'inizio non cambia nulla!"""
word, value = banana_known
@ -105,6 +116,16 @@ def test_dec_to_ribes_known(ribes_known):
assert dec2ribes(value) == word
def test_ribes_is_ribes(ribes_known):
assert isribes(ribes_known[0])
def test_ribes_is_only_ribes(ribes_known):
assert not isbanana(ribes_known[0])
assert not isananas(ribes_known[0])
assert not isavocado(ribes_known[0])
def test_avocado_to_dec_known(avocado_known):
word, value = avocado_known
assert avocado2dec(word) == value
@ -115,6 +136,16 @@ def test_dec_to_avocado_known(avocado_known):
assert dec2avocado(value) == word
def test_avocado_is_avocado(avocado_known):
assert isavocado(avocado_known[0])
def test_avocado_is_only_avocado(avocado_known):
assert not isribes(avocado_known[0])
assert not isananas(avocado_known[0])
assert not isbanana(avocado_known[0])
def test_ananas_to_dec_known(ananas_known):
word, value = ananas_known
assert ananas2dec(word) == value
@ -125,6 +156,16 @@ def test_dec_to_ananas_known(ananas_known):
assert dec2ananas(value) == word
def test_ananas_is_ananas(ananas_known):
assert isananas(ananas_known[0])
def test_ananas_is_only_ananas(ananas_known):
assert not isribes(ananas_known[0])
assert not isbanana(ananas_known[0])
assert not isbanana(ananas_known[0])
def test_answer_to_life_the_universe_and_everything():
banana = banana2dec("banana")
assert banana != 42