Advertisement
vaboro

ps2b.py

Apr 18th, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. #########################################
  2. #WARNING! Exponential order of compexity#
  3. #########################################
  4.  
  5. def isSolution(coefficients, number_of_nuggets):
  6.     '''returns True if there is a solution to a*x + b*y + c*z = n and None if there is no solution'''
  7.     for k in range(0,number_of_nuggets+1):
  8.         for m in range(0,number_of_nuggets+1):
  9.             for j in range(0,number_of_nuggets+1):
  10.                 current_number_of_nuggets = coefficients[0]*j+coefficients[1]*m+coefficients[2]*k
  11.                 if current_number_of_nuggets == number_of_nuggets:
  12.                     isSolution = True
  13.                     #print str(coefficients[0])+"*"+str(j)+"+"+str(coefficients[1])+"*"+str(m)+"+"+str(coefficients[2])+"*"+str(k)+"="+str(current_number_of_nuggets) # for debugging
  14.                     return isSolution
  15.  
  16. while True:
  17.     n = input ("Input integer number of McNuggets between 1 and 200:")
  18.     if 0 < n < 201:
  19.         break
  20.  
  21. a = input ("Input the first coefficient 'a':")
  22. b = input ("Input the second coefficient 'b':")
  23. c = input ("Input the third coefficient 'c':")
  24. packages = (a,b,c)
  25. packages = sorted(packages)
  26.  
  27. if a > n and b > n and c > n:
  28.     print str(n)+" nuggets can't be bought with packages of "+str(a)+", "+str(b)+" "+"and "+str(c)+" nuggets."
  29. else:
  30.  
  31.     the_largest_number = 0
  32.     for j in range(1,n+1):
  33.         if isSolution(packages,j) == None:
  34.             the_largest_number = j
  35.             # print  "isSolution == None "+str(j)+" nuggets cannot be bought in exact quantity" # for debugging
  36.     print "Given package sizes of "+str(packages[0])+", "+str(packages[1])+", and "+str(packages[2])+", the largest number of McNuggets that\ncannot be bought in exact quantity is: "+str(the_largest_number)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement