Python Fundamentals - Practice Problems - 12. Laptop Purchase - All possible combinations

Python Fundamentals Practice Problems — Laptop Purchase | Dataquest

I’ve done a version in which all possible combinations of 2 laptops, with same or different prices, can be found, never repeating an equivalent combination (consideing [1,2] equal to [2,1], as are the same two laptops).

I’ve tried not to use magical numbers ( 5000) but define constants, not to add new magical numbers (2500), and make it as readable and human thinking as possible (first conditions with one finality and later conditions with another one, not mixing them).

The case with solutions with 2 laptops with different prices can be tested if changing TOTAL_PRICE for 1500 instead of 5000.

It’s not a very nice code but in case it’s useful or someone can share improvements, etc. Specially I think the use of the list used_prices so as not to repeat a combination could be more elegant. I imagine there are characteristics of python that could make this way shorter and/or clearer.

from csv import reader
file = open('laptops.csv')
file_reader = reader(file)
all_rows = list(file_reader)
file.close()

# brand name price
# price is not unique!!!
TOTAL_PRICE = 5000

# using dictionary
price_to_name = {}
data = all_rows[1:]

for laptop in data:
    price = int(laptop[2])
    name = laptop[1]
    if price not in price_to_name:
        # list with that first laptop with that price
        price_to_name[price] = [name]
    else:
        # append new laptop name to the list of that price
        price_to_name[price].append(name)
#print(price_to_name)

# initialize the two laptops in the answer to None
laptop1 = None
laptop2 = None

# as each price will only match another price, we'll use this not to repeat combinations
used_prices = []

for price1 in price_to_name:
    for price2 in price_to_name:
        # conditions to be solution
        if (price1 not in used_prices 
            and price1 + price2 == TOTAL_PRICE
           ):
            # We cannot buy the same laptop model twice.
            # Condition to treat the special same price case
            if price1 != price2:
                # note we use this prices in a solution
                used_prices.append(price1)
                used_prices.append(price2)
                laptop1_list = price_to_name[price1]
                laptop2_list = price_to_name[price2]
                # every laptop from both lists can be combined as the same laptop won't have different prices (won't appear in both lists):
                for laptop1 in laptop1_list:
                    for laptop2 in laptop2_list:
                        print('Different price: ', laptop1, ' | ', laptop2)
            # if the price is the same, we need it to have at least 2 different laptops to buy
            elif price1 == price2 and len(price_to_name[price1]) >= 2:
                used_prices.append(price1)
                laptop1_list = price_to_name[price1]
                # we create valid laptop combinations (different laptops):
                for i in range(len(laptop1_list)):
                    # loop over the rest, so as not to repeat any previous combination of items ([1,2] == [2,1])
                    for j in range(i+1, len(laptop1_list)):
                        print('indexes: ', i,j)
                        laptop1 = laptop1_list[i]
                        laptop2 = laptop1_list[j]
                        print('Same price: ', laptop1, ' | ', laptop2)
            
# show the last solution
print('laptop1: ', laptop1)
print('laptop2: ', laptop2)
1 Like