Problem test - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem test (/thread-11201.html) |
Problem test - picalex06 - Jun-27-2018 Hi, Excuse me for my english ..... I have a problem with a test. I must to calculate the different possibility to make a rugby score (try (5 points), try with transformation (7 points), penalty(3 points)). The score is 88 my code : def calculSolution(n): solution=[] ligne=[] for x in range(0,100) : for y in range(0,100) : for z in range(0,100) : if 5*x+7*y+3*z==n : print ("combinaison x %d y %d z %d " %(x,y,z)) x=x+y ligne=(x,y,z) solution.append(ligne) # solution=set(solution) return solutionan example of solutions find : combinaison x 0 y 1 z 27 combinaison x 1 y 2 z 23 combinaison x 3 y 4 z 15 combinaison x 7 y 5 z 6 combinaison x 1 y 2 z 23 combinaison x 3 y 4 z 15 combinaison x 7 y 5 z 6 combinaison x 2 y 0 z 26 combinaison x 2 y 3 z 19 combinaison x 5 y 6 z 7 combinaison x 3 y 1 z 22 combinaison x 4 y 2 z 18 combinaison x 6 y 4 z 10 combinaison x 10 y 5 z 1 In my list, for example, I don't have the solution (0 4 20) but it's a valid solution 0 * 5 + 4 * 7 + 20 * 3 = 88 So why I don't have this solution in my resultat ? It's only three "For".... If somebody have an idea ... Thanks by advance for you ideas...... RE: Problem test - woooee - Jun-27-2018 You modify the value in x before it is appended to the list. So the values are found, as the print should show, the list however contains the modified number. x=x+y ligne=(x,y,z) solution.append(ligne)You can also speed up your code slightly by breaking out of z when the total is greater than 88 def calculSolution(n): solution=[] ## ligne=[] ## not necessary as it is created anew under the for for x in range(0,100) : for y in range(0,100) : for z in range(0,100) : combo=5*x+7*y+3*z if combo==n : print ("combinaison x %d y %d z %d " %(x,y,z)) ## x=x+y ligne=(x,y,z) solution.append(ligne) elif combo > 88: break # solution=set(solution) return solution RE: Problem test - picalex06 - Jun-27-2018 Thanks for your response.... I understand now where is the problem .... I change the value of x.... Thank you again for your help ..... RE: Problem test - woooee - Jun-27-2018 Also, there is no reason to continue when 5*x > 88, or 7*y >88, or 3*z > 88, so x stops at 18 when n==88 def calculSolution(n): solution=[] stop_x=int(n/5) + 1 stop_y=int(n/7) + 1 stop_z=int(n/3) + 1 ## ligne=[] ## not necessary as it is created anew under the for for x in range(0, stop_x) : for y in range(0, stop_y) : for z in range(0, stop_z) : combo=5*x+7*y+3*z if combo==n : print ("combinaison x %d y %d z %d " %(x,y,z)) ## x=x+y ligne=(x,y,z) solution.append(ligne) elif combo > 88: break # solution=set(solution) return solution |