refactor random
This commit is contained in:
parent
be5ebcfd5a
commit
9881d09fc5
2 changed files with 24 additions and 40 deletions
|
@ -1,4 +1,5 @@
|
||||||
"""Main module."""
|
"""Main module."""
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
class Codec:
|
class Codec:
|
||||||
|
@ -60,6 +61,21 @@ class Codec:
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def random(self, minlength=6):
|
||||||
|
|
||||||
|
numdict = len(self.dictionary)
|
||||||
|
word = ""
|
||||||
|
|
||||||
|
curr_dict = (numdict - 1 + self.dictstart + self.shiftend) % numdict
|
||||||
|
final_dict = (numdict - 1 + self.dictstart) % numdict
|
||||||
|
print("final", final_dict)
|
||||||
|
while curr_dict != final_dict or len(word) < minlength:
|
||||||
|
word = random.choice(self.dictionary[curr_dict]) + word
|
||||||
|
curr_dict = (curr_dict - 1) % numdict
|
||||||
|
print("curr", curr_dict)
|
||||||
|
|
||||||
|
return word
|
||||||
|
|
||||||
|
|
||||||
class BananaCodec(Codec):
|
class BananaCodec(Codec):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -81,26 +97,5 @@ class AvocadoCodec(Codec):
|
||||||
super().__init__(1, 1)
|
super().__init__(1, 1)
|
||||||
|
|
||||||
|
|
||||||
def bananarandom(dictstart=0, shiftend=0, minlength=6, dictionary=None):
|
|
||||||
import random
|
|
||||||
|
|
||||||
# defaults
|
|
||||||
if dictionary is None:
|
|
||||||
dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
|
|
||||||
|
|
||||||
numdict = len(dictionary)
|
|
||||||
st = ""
|
|
||||||
length = 0
|
|
||||||
|
|
||||||
i = (numdict - 1 + dictstart + shiftend) % numdict
|
|
||||||
while not (i == (numdict - 1 + dictstart) % numdict and length >= minlength):
|
|
||||||
r = random.randint(0, len(dictionary[i]) - 1)
|
|
||||||
st = dictionary[i][r] + st
|
|
||||||
i = (i - 1) % numdict
|
|
||||||
length += 1
|
|
||||||
|
|
||||||
return st
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("Ciao sono la libreria banana")
|
print("Ciao sono la libreria banana")
|
||||||
|
|
|
@ -39,6 +39,10 @@ def main_check(args):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def main_random(args):
|
||||||
|
print(get_codec(args).random(minlength=args.minlength))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="Convert dec number to banana")
|
parser = argparse.ArgumentParser(description="Convert dec number to banana")
|
||||||
parser.add_argument("--ananas", action="store_true")
|
parser.add_argument("--ananas", action="store_true")
|
||||||
|
@ -66,6 +70,10 @@ def main():
|
||||||
check.add_argument("--quiet", "-q", action="store_true")
|
check.add_argument("--quiet", "-q", action="store_true")
|
||||||
check.set_defaults(func=main_check)
|
check.set_defaults(func=main_check)
|
||||||
|
|
||||||
|
rand = sub.add_parser("random", help="Generate random banana")
|
||||||
|
rand.add_argument("--minlength", help="Set minimum length", type=int, default=6)
|
||||||
|
rand.set_defaults(func=main_random)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if not hasattr(args, "func"):
|
if not hasattr(args, "func"):
|
||||||
print("You need to select one subcommand. \nUse --help", file=sys.stderr)
|
print("You need to select one subcommand. \nUse --help", file=sys.stderr)
|
||||||
|
@ -74,25 +82,6 @@ def main():
|
||||||
args.func(args)
|
args.func(args)
|
||||||
|
|
||||||
|
|
||||||
def bananarandom():
|
|
||||||
parser = argparse.ArgumentParser(description="Generate random banana")
|
|
||||||
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs="+")
|
|
||||||
parser.add_argument(
|
|
||||||
"--dictstart", help="Set starting dictionary", type=int, default=0
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--shiftend", help="Set shift for ending dictionary", type=int, default=0
|
|
||||||
)
|
|
||||||
parser.add_argument("--minlength", help="Set minimum length", type=int, default=6)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
print(
|
|
||||||
banana.bananarandom(
|
|
||||||
args.dictstart, args.shiftend, args.minlength, args.dictionary
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# pragma: no cover
|
# pragma: no cover
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue