Extension of Practice Problem 12 from Dictionaries Mission Practice Problems

Hey community, I just wanted to share with you an extension to practice problem 12 that I created. I ended up writing a program that takes any budget and provides the user with a dictionary of all the possible combinations for a pair of laptops. And for the sake of the question, the program also assigns laptops that are equal to the budget to the variables laptop1 and laptop2. I tried my best to explain my code, but feel free to let me know if anything is confusing, I would be more than happy to explain.

Below is my program:


from csv import reader
opened_file = open('laptops.csv')
read_file = reader(opened_file)
laptops_data = list(read_file)

#You can put whatever budget you want
budget = 5000

price_to_name = {}

budget_list = []

#This segment of code itterates through our laptops_data file(excluding the header row) and assigns a list with the laptop names to the price of the laptops.

#Ex: {2049: ['GS73VR 7RF'], 1026: ['ZenBook UX310UQ-GL026T'], 2051: ['Alienware 15'], 1028: ['GV62 7RD-1686NL', 'Lenovo Book']}
for row in laptops_data[1:]:  
    price = int(row[2])
    name = row[1]
    if price in price_to_name:
        price_to_name[price].append(name)
    else:
        price_to_name[price] = [name] 

#This segment of code uses nested for  loops to itterate the inside elements(price2) once for an outside element(price1). So for each price1, the code will itterate through each price2. What happens here is that if both prices equals the budget, they will be added to budget_list. At this point we are just appending the names of laptops that correspond to price1 and price2. price1 or price2 can have more than 1 laptop, but that is an issue we will solve later in this program to get laptop pairs. Another important thing to notice is that we are appending the laptops linked to price1 and price2, which means that we already know that for every two lists in budget_list, they will equal the budget. Here is an example of what I mean. in our exmaple, the budget is $4549, at this point in our code budget_list would equal:

# budget_list = [['GS73VR 7RF'], ['Laptop MSI', 'GS60 Ghost'], ['ThinkPad P40'], ['GT73EVR 7RE', 'ThinkPad X1', 'GT72VR Dominator'], ... ]

#The output above means that ['GS73VR 7RF'] + ['Laptop MSI', 'GS60 Ghost'] = $4549, and DOES NOT mean ['GS73VR 7RF'] + ['Laptop MSI] + ['GS60 Ghost'] <-- this would equal 7049
for price1 in price_to_name:
    for price2 in price_to_name:
        if price1 + price2 == budget:
            budget_list.append(price_to_name[price1])
            budget_list.append(price_to_name[price2])


# As we stated before, we know that the combinations that equal to the budget are added for every two lists, and so the point of the code in this segment is that we would like to make a list of lists of lists, so that for each 2 consecutive lists(which are the combinations that equal the budget) we group them in thier own list. Doing this makes it easier to get combinations of only 2 laptops that equal the budget. which we will do in our next segment of code. This what I mean:

# We will go from this:

#[['GS73VR 7RF'], ['Laptop MSI', 'GS60 Ghost'], ['ThinkPad P40'], ['GT73EVR 7RE', 'ThinkPad X1', 'GT72VR Dominator'], ... ]

#To this:

# [[['GS73VR 7RF'], ['Laptop MSI', 'GS60 Ghost']], [['ThinkPad P40'], ['GT73EVR 7RE', 'ThinkPad X1', 'GT72VR Dominator']], ... ]]]
combo_list = []

it = iter(budget_list)
for x in it:
    combination = [x, next(it)]
    combo_list.append(combination)

#In this segment of code, we take each list in combo_list ([['GS73VR 7RF'], ['Laptop MSI', 'GS60 Ghost']]) <-- this the first list in our combo_list for this exmaple. And then we assgin the first sublist[0] to the varible left side and the second sublist[1] to the variable right_side. So we end up with:

# left_side = ['GS73VR 7RF']
# right_side = ['Laptop MSI', 'GS60 Ghost']

# now we iterrate the elements of the left_side once for each element of the right_side. and we make sure that we don't get pairs of laptops with the same name by putting in the line of code (if t != m:). We then append the two laptop names to the list we made called final_list. But we also need to amke sure that these pairs are put in their own list within final_list, by putting t, m in square brackets.
#So we end up having:

#final_list = [['GS73VR 7RF', 'Laptop MSI'], ['GS73VR 7RF', 'GS60 Ghost'], ['ThinkPad P40', 'GT73EVR 7RE'], ['ThinkPad P40'], ['ThinkPad P40', 'ThinkPad X1']. ['ThinkPad P40', 'GT72VR Dominator'], ...]

    

final_list = []
for x in combo_list:
    left_side = x[0]
    right_side = x[1]
    for t in left_side:
        for m in right_side:
            if t != m:
                final_list.append([t, m])

#The point of this code segment is to alphabetically order the lists of lists in final_list so that we can get rid of duplicates, duplicates arise when half the budget is equal to the price of two or more laptops, such as with a budget of $5000, we have laptops: Laptop MSI and GS60 Ghost that equal 2500 and so we would end up with:

#final_list = [['Laptop MSI', 'GS60 Ghost'], ['GS60 Ghost', 'Laptop MSI']]

#after executing the below segment of code, we would end up with:

#final_list = [['GS60 Ghost', 'Laptop MSI'], ['GS60 Ghost', 'Laptop MSI']] <-- now our list is ready to get rid of duplicates.

for x in final_list:
    x.sort()

#now we make a list that doesn't include any duplicates called fin_al_final. for every list in final_list, if it is not in fin_al_final then we append it to fin_al_final. SO we only appned lists that are not present in fin_al_final and this way we make sure that we don't have any duplicates.  

fin_al_final = []
for x in final_list:
    if x not in fin_al_final:
        fin_al_final.append(x)
        
#lastly, we make an empty dictionary where the key is our budget and we assign it an empty list, so that we can append our cominations pairs from fin_al_final.  
combo_dict = {budget : []}
for x in fin_al_final:
    combo_dict[budget].append(x)
    
#for the sake of answering the question, we can just take the first element of our first list that is assigned to our key(budget) by indexing and using [0][0] and assinging it to the variable laptop1 and then using [0][1] to assign the second element of the first list to laptop2. 
laptop1 = combo_dict[budget][0][0]
laptop2 = combo_dict[budget][0][1]

print(combo_dict)
print(laptop1)
print(laptop2)

6 Likes