# Problem #12: Laptop Purchase

My Code:

``````file = open('laptops.csv')

price_to_name = {}
for data in laptops:
key = int(data)
value = data
price_to_name[key] = value

laptop1 = None
laptop2 = None

for row in laptops:
price = int(row)
for laptop in laptops:
price2 = int(laptop)
if price + price2 == 5000 and row != laptop:
laptop1 = row
laptop2 = laptop
print(laptop1 + ", " + laptop2)```
Replace this line with your code
``````

What I expected to happen:
laptop1 = GS60 Ghost
laptop2 = Laptop MSI

What actually happened:

``````Replace this line with the output/error

<!--Enter other details below: -->
I double checked my code and it s giving me two laptops with a cost of 2500. Don’t know why my code does not pass the answer check. I also verified solution given but, i don’t understand the logic.``````

@cfsc060486

One logic that solves this problem is finding two laptops that cost 2500 each. This was how I solved it in the past.

``````from csv import reader
file = open("laptops.csv")

price_to_name = {}

for one in data[1:]:
price_to_name.setdefault(int(one[-1]), []).append(one)

abc = [a for a in list(price_to_name) if a * 2 == 5000]

abc = price_to_name.get(abc)

laptop1, laptop2 = abc
``````

Although this solves this problem, but I am thinking using itertools.combinations will be a better approach.

In this approach, you get a combination of 2 laptops and sum the prices. There might be several that cost 5000.