Generalized Dice refactoring - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Generalized Dice refactoring (/thread-15878.html) |
Generalized Dice refactoring - Beginner_coder123 - Feb-04-2019 Hello everyone, I need some help with refactoring my code for an assignment. The assignment is as following: a. Solve problem 205 as a probabilistic experiment. This will not get you to 7 digits precision, but 2 to 3 digits should be possible. b. Adapt the code for part a such that it can handle two sets of 'generalized dice'. Each set consists of a number of equal objects that generate numbers from a finite set with an arbitrary probability distribution. You only need to hand in the code for part b (not for a). #Assignment import time import random def roll(die): number = random.randint(0,len(die)-1) b = die[number] return b Die1 = [1,2,3,4] Die2 = [1,2,3,4,5,6] #num lists def inptchacc(string): ending_conditions = ['stop','Stop','quit','Quit'] end = False inpu = input(string) while end == False: if not(inpu in ending_conditions): try: retr = int(inpu) return retr except: string = 'invalid input please try again' inpu = input('invalid input please try again ') else: stop = 'stop' return stop def quantitymatrix(IDie): stringDie = 'how often would you like this Die to occur?' list = [] Adding = True while Adding: print('the Die in question is '+ str(IDie)) toadd = inptchacc(string) if toadd != 'stop': list.append(toadd) else: Adding = False return list def deeper(IDie): stringDie = 'what number would you like to add to the new face of the Die? (to end the die please type "stop or Stop or quit or Quit to finish the DIE" )' list = [] Adding = True while Adding: print('The Die ' + IDie + ' is currently ' + str(list) ) toadd = inptchacc(stringDie) if toadd != 'stop': list.append(toadd) else: Adding = False return list def chance_overlap(dielist,Dielistcount): highnumber = 100000 counter = (len(dielist))*[0] chance = (len(dielist))*[0] for n in range(highnumber): dieres = len(Dielistcount)*[0] for dienumber in range(len(dielist)): for diecount in range(Dielistcount[dienumber]): dieres[dienumber] += roll(dielist[dienumber]) for dienumber2 in range(len(dielist)): if max(dieres) == dieres[dienumber2] and dieres.count(max(dieres)) == 1: counter[dienumber2] += 1 for chanceper in range(len(counter)): chance[chanceper] = counter[chanceper]/highnumber chance[chanceper] = str(chance[chanceper]) + '% for die' + str(chanceper+1) return chance def suckmypp(counterq): string1 = 'adding the amount of the die '+ str(counterq+1) firstq = True while firstq: suckmypp2 = inptchacc(string1) if suckmypp2 != 'stop': firstq = False return suckmypp2 Dielist1 = [Die1,Die2] diecount = [9,6] chance = chance_overlap(Dielist1,diecount) print(chance) Doing = True counter = 0 while Doing: Dielist2 = [] adding = True while adding: counter += 1 addQ = input('to stop type S and enter otherwise any characters and enter will add another die') if addQ != 'S': notdone = True while notdone: dietoadd = deeper('Die' + str(counter)) if len(dietoadd) >= 1: Dielist2.append(dietoadd) notdone = False else: print('die is empty not added') else: adding = False quantity = True counterq = 0 Qlist = [] print(Qlist) print(len(Dielist2)) while quantity: Qlist.append(suckmypp(counterq)) counterq += 1 if counterq == (len(Dielist2)): quantity = False print(Dielist2) print(Qlist) chance2 = chance_overlap(Dielist2,Qlist) Doing = False print(chance2)How can I refactor this in a more "efficient" way? I tried everything, but I keep breaking my code. Please don't mind the variable names, certain callback names etc. They are satire RE: Generalized Dice refactoring - nilamo - Feb-04-2019 (Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: number = random.randint(0,len(die)-1)This is the same, and clearer: number = random.randint(len(die)) But this is even clearer: number = random.choice(die) (Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: ending_conditions = ['stop','Stop','quit','Quit']If you call .lower() on the input, you only need ending_conditions = ['stop', 'quit'] . As an added bonus, you also properly handle weird things like "sToP", or "QUIT".(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: while end == False:That's kind of gross, and would look nicer as while not end: .(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: if not(inpu in ending_conditions):I'd prefer to see the "not" next to the "in", like such: if inpu not in ending_conditions: The functions quantitymatrix and deeper are the same. One of them can just be deleted, and you can call the other instead.Aside from that, what is it you're trying to refactor? RE: Generalized Dice refactoring - Beginner_coder123 - Feb-05-2019 (Feb-04-2019, 10:03 PM)nilamo Wrote:(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: number = random.randint(0,len(die)-1)This is the same, and clearer: Hello, ah appreciated the help. I would like to refactor my code into something more efficient. With the use of memoization, for example. Some higher order functions if possible. But also to make the code look a little bit clearer. What's the best way for me to do this? |