Advertisement
Guest User

Problem 31

a guest
Nov 1st, 2014
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. #Problem 31
  2.  
  3. #findIndex(num(str), lst(list)) -> num(str)
  4. #get rest value(num) and current list(list),
  5. #returning the highest possible value which
  6. #is less than num
  7. def findIndex(num, lst):
  8.     if num == 0:
  9.         raise "findIndex num == 0"
  10.     while True:
  11.         if num in lst:
  12.             return lst[lst.index(num)]
  13.         elif num == 0:
  14.             return None
  15.         num -= 1
  16.  
  17.  
  18. lst = [200, 100, 50, 20, 10, 5, 2, 1]
  19. tmp = lst[:]
  20.  
  21. combi = 0
  22. rest = 200
  23.  
  24. isRunning = True
  25. while isRunning:
  26.  
  27.     #If all combinations found
  28.     if len(tmp) == 0:
  29.         isRunning = False
  30.    
  31.     #If found new combination
  32.     if rest == max(tmp):
  33.         combi += 1
  34.         rest = 200
  35.        
  36. #-      #If no more combinations with max value, then:
  37.             #lst = lst[1:]
  38.         tmp = lst[:]
  39.        
  40.     #If rest is more than max value
  41.     if rest > max(tmp):
  42.         rest -= max(tmp)
  43.        
  44.     #If rest is less than max value
  45.     elif rest < max(tmp):
  46.         value = findIndex(rest, tmp)
  47.         #If highest possible value can be added
  48.         #This test is not really important
  49.         if rest:
  50.             if rest - value >= 0:
  51.                 rest -= value
  52.         else:
  53.             raise "no match in findIndex"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement