Advertisement
Khizarce

Untitled

Jul 17th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #!/bin/python3
  2.  
  3. import math
  4. import os
  5. import random
  6. import re
  7. import sys
  8.  
  9.  
  10.  
  11. #
  12. # Complete the 'budgetShopping' function below.
  13. #
  14. # The function is expected to return an INTEGER.
  15. # The function accepts following parameters:
  16. # 1. INTEGER n
  17. # 2. INTEGER_ARRAY bundleQuantities
  18. # 3. INTEGER_ARRAY bundleCosts
  19. #
  20.  
  21. def budgetShopping(n, bundleQuantities, bundleCosts):
  22. # Write your code here
  23. bundleValue = []
  24. notebooks = 0
  25.  
  26. while len(bundleQuantities) > 0 and n > 0:
  27. bestValue = 0
  28. print("Len of bundleQuantities is {}, len of bundleCosts is {}".format(len(bundleQuantities), len(bundleCosts)))
  29. for quantity in range(len(bundleQuantities)):
  30. #bundleValue.append(bundleQuantities[quantity]/bundleCosts[quantity])
  31. bundleValue.append(bundleCosts[quantity]/bundleQuantities[quantity])
  32. print("bundleQuantities: {}, bundleCosts: {}, bundleValue: {} \n".format(bundleQuantities[quantity],bundleCosts[quantity], bundleValue[quantity]))
  33. #bundleValue equals the number of notebooks that a person can purchase with each dollar
  34. if(bundleValue[bestValue] < bundleValue[quantity]):
  35. bestValue = quantity
  36. # Now buy as many of the best value notebooks as possible by doing n//bundleCost[min] (integer division)
  37. print("n = {}, bundleCosts[bestValue] = {}".format(n, bundleCosts[bestValue]))
  38. if n >= bundleCosts[bestValue]:
  39. supplies = n//bundleCosts[bestValue]
  40. notebooks += bundleQuantities[bestValue]*supplies
  41. n -= bundleCosts[bestValue]*supplies
  42. bundleQuantities.pop(bestValue)
  43. bundleCosts.pop(bestValue)
  44. bundleValue.pop(bestValue)
  45. print("New n is {}, notebooks purchased at moment is {}".format(n, notebooks))
  46. elif n > 0 and n < bundleCosts[bestValue]:
  47. bundleQuantities.pop(bestValue)
  48. bundleCosts.pop(bestValue)
  49. bundleValue.pop(bestValue)
  50. else:
  51. break
  52. return notebooks
  53.  
  54. if __name__ == '__main__':
  55. fptr = open(os.environ['OUTPUT_PATH'], 'w')
  56.  
  57. n = int(input().strip())
  58.  
  59. bundleQuantities_count = int(input().strip())
  60.  
  61. bundleQuantities = []
  62.  
  63. for _ in range(bundleQuantities_count):
  64. bundleQuantities_item = int(input().strip())
  65. bundleQuantities.append(bundleQuantities_item)
  66.  
  67. bundleCosts_count = int(input().strip())
  68.  
  69. bundleCosts = []
  70.  
  71. for _ in range(bundleCosts_count):
  72. bundleCosts_item = int(input().strip())
  73. bundleCosts.append(bundleCosts_item)
  74.  
  75. result = budgetShopping(n, bundleQuantities, bundleCosts)
  76.  
  77. fptr.write(str(result) + '\n')
  78.  
  79. fptr.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement