Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem test
#1
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 solution
an 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 Rolleyes

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......
Reply
#2
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 
Reply
#3
Thanks for your response....

I understand now where is the problem ....
I change the value of x.... Wall

Thank you again for your help .....
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,063 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,062 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,511 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020