Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/python3
- import math
- import os
- import random
- import re
- import sys
- #
- # Complete the 'budgetShopping' function below.
- #
- # The function is expected to return an INTEGER.
- # The function accepts following parameters:
- # 1. INTEGER n
- # 2. INTEGER_ARRAY bundleQuantities
- # 3. INTEGER_ARRAY bundleCosts
- #
- def budgetShopping(n, bundleQuantities, bundleCosts):
- # Write your code here
- bundleValue = []
- notebooks = 0
- while len(bundleQuantities) > 0 and n > 0:
- bestValue = 0
- print("Len of bundleQuantities is {}, len of bundleCosts is {}".format(len(bundleQuantities), len(bundleCosts)))
- for quantity in range(len(bundleQuantities)):
- #bundleValue.append(bundleQuantities[quantity]/bundleCosts[quantity])
- bundleValue.append(bundleCosts[quantity]/bundleQuantities[quantity])
- print("bundleQuantities: {}, bundleCosts: {}, bundleValue: {} \n".format(bundleQuantities[quantity],bundleCosts[quantity], bundleValue[quantity]))
- #bundleValue equals the number of notebooks that a person can purchase with each dollar
- if(bundleValue[bestValue] < bundleValue[quantity]):
- bestValue = quantity
- # Now buy as many of the best value notebooks as possible by doing n//bundleCost[min] (integer division)
- print("n = {}, bundleCosts[bestValue] = {}".format(n, bundleCosts[bestValue]))
- if n >= bundleCosts[bestValue]:
- supplies = n//bundleCosts[bestValue]
- notebooks += bundleQuantities[bestValue]*supplies
- n -= bundleCosts[bestValue]*supplies
- bundleQuantities.pop(bestValue)
- bundleCosts.pop(bestValue)
- bundleValue.pop(bestValue)
- print("New n is {}, notebooks purchased at moment is {}".format(n, notebooks))
- elif n > 0 and n < bundleCosts[bestValue]:
- bundleQuantities.pop(bestValue)
- bundleCosts.pop(bestValue)
- bundleValue.pop(bestValue)
- else:
- break
- return notebooks
- if __name__ == '__main__':
- fptr = open(os.environ['OUTPUT_PATH'], 'w')
- n = int(input().strip())
- bundleQuantities_count = int(input().strip())
- bundleQuantities = []
- for _ in range(bundleQuantities_count):
- bundleQuantities_item = int(input().strip())
- bundleQuantities.append(bundleQuantities_item)
- bundleCosts_count = int(input().strip())
- bundleCosts = []
- for _ in range(bundleCosts_count):
- bundleCosts_item = int(input().strip())
- bundleCosts.append(bundleCosts_item)
- result = budgetShopping(n, bundleQuantities, bundleCosts)
- fptr.write(str(result) + '\n')
- fptr.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement