from itertools import permutations
from copy import deepcopy
def eva(xx, y):
x = deepcopy(xx)
for i, v in enumerate(y):
if v != "BOGO":
if v == "$5":
x[i] = max(x[i]-5, 0)
elif v == "$10":
x[i] = max(x[i]-10, 0)
elif v == "$50":
x[i] = max(x[i]-50, 0)
elif v == "10%":
x[i] *= 0.9
elif v == "20%":
x[i] *= 0.8
elif v == "BOGO":
temp = y.index("!")
maxv = max(x[i], x[temp])
if maxv == x[i]:
x[temp] = 0
else:
x[i] = 0
if v != "TAX":
x[i] *= 1.13
for i, v in enumerate(x):
x[i] = round(v, 2)
return sum(x)
infile = open("DATA41.TXT")
nums = []
keys = []
coupons = []
testcases = []
for i in infile:
i = i.strip()
if "." in i:
nums.append(float(i))
elif "B" in i or "%" in i or "$" in i or "T" in i:
coupons.append(i)
else:
keys.append(int(i))
if len(keys) == 3:
testcases.append([nums, coupons, keys[:-1]])
nums = []
coupons = []
keys = [keys[-1]]
testcases.append([nums, coupons, keys[:-1]])
for i in range(5):
nums = testcases[i][0]
coupons = testcases[i][1]
keys = testcases[i][2]
while coupons.count("BOGO") > 2:
coupons.remove("BOGO")
if coupons.count("BOGO") == 1:
coupons.append("BOGO")
while len(coupons) < len(nums):
coupons.append("NOTHING")
lnum = 100000
for i in set(permutations(coupons, keys[0])):
if (i.count("BOGO") == 0 or i.count("BOGO") == 2):
if "BOGO" in i:
temp = i.index("BOGO")
ii = []
c = 0
for blah in i:
if blah == "BOGO" and c == 0:
c += 1
ii.append("BOGO")
elif blah == "BOGO" and c != 0:
ii.append("!")
else:
ii.append(blah)
num = eva(nums, ii)
else:
num = eva(nums, i)
if num < lnum:
lnum = num
print "The best price is $%.2f" %lnum
|