Problem #12: Laptop Purchase

Screen Link:

My Code:

file = open('laptops.csv')
reader = csv.reader(file)
laptops = list(reader)[1:]

price_to_name = {}
for data in laptops:
    key = int(data[2])
    value = data[1]
    price_to_name[key] = value
    
laptop1 = None
laptop2 = None

for row in laptops:
    price = int(row[2])
    for laptop in laptops:
        price2 = int(laptop[2])
        if price + price2 == 5000 and row[1] != laptop[1]:
            laptop1 = row[1]
            laptop2 = laptop[1]
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
```Your code did not pass our answer checks

<!--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

Found that your code output the correct answer but is not passing the answer check.

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")
data = list(reader(file))

price_to_name = {}

for one in data[1:]:
    price_to_name.setdefault(int(one[-1]), []).append(one[1])
    
abc = [a for a in list(price_to_name) if a * 2 == 5000]


abc = price_to_name.get(abc[0])

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.