Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Generalized Dice refactoring Beginner_coder123 Programmer named Tim Posts: 10 Threads: 4 Joined: Oct 2018 Reputation: 0 Likes received: 0 #1 Feb-04-2019, 09:20 PM 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 nilamo Last Thursdayist Posts: 3,153 Threads: 79 Joined: Sep 2016 Reputation: 127 Likes received: 704 #2 Feb-04-2019, 10:03 PM (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? Beginner_coder123 Programmer named Tim Posts: 10 Threads: 4 Joined: Oct 2018 Reputation: 0 Likes received: 0 #3 Feb-05-2019, 08:45 AM (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: `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? 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? « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post dice program sunny_awesome 2 196 May-13-2019, 09:39 AM Last Post: sunny_awesome Two dice Game of Pig help elliemehl 2 381 Feb-14-2019, 01:19 AM Last Post: woooee Python Connect 4 refactoring Beginner_coder123 6 490 Oct-29-2018, 05:30 PM Last Post: Beginner_coder123 Two Dice Pig Game in Python 3.6 Help inspired22 4 2,468 Oct-10-2018, 01:17 PM Last Post: ichabod801 help with while loop on dice game sean5 2 1,086 Dec-14-2017, 07:24 PM Last Post: nilamo Dice and Random Variables isuckatcoding 3 1,227 Dec-04-2017, 05:00 AM Last Post: micseydel dice game im stuck sylerr 3 1,841 May-12-2017, 10:50 PM Last Post: ichabod801

Forum Jump:

Users browsing this thread: 1 Guest(s)