So basically I have a really typical issue, I found some solutions from stackoverflow before but this didn't really help me.
I have a school task which requires me to analyze bobsleigh team. I have names, their rating and weight in three separate lists and i need to pick four competitors with best ratings and their weight combined must not be over 325 kg. What I've already done is I've created a for
cycle which picks out four competitors with highest rating, but the issue is that they may have high ratings but their weight combined is over 325 kg.
For example I have three lists:
["Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8"] ## names
[1,2,3,4,5,6,7,8] ##ratings
[50,60,70,80,90,100,110,120] ##weights
what I get is [5,6,7,8]
(and every single one of them has a according weight list and name list, for example for rating 5 goes competitor with weight of 90 kg and name is Name 5) but their weight combined is higher than 325kg (90+100+110+120 > 325)
I've done coding so far that the minimal element (5) is removed and replaced by another, lower element, but the issue occurs (by issue I mean I don't know how to do it) when switching the last element is not enough and I need to try to replace element 6 and keep the element 5 in place.
Right now the code for switching is:
while (kaalude_summa > 325): #if weight is over 325 kg
positsioon = reiting.index(max(reiting)) #position for max rating
if kaalud[positsioon] < kaalude_tulemused[3]: #two lists - one is for elements not in the resulting list (kaalud) and one is for the elements in resulting list (kaalude_tulemused)
kaalude_tulemused[3] = kaalud[positsioon] #sets new weight in result list
tulemuslist.remove(min(tulemuslist)) #removes number 5 from example list [5,6,7,8] (check above)
tulemuslist.append(max(reiting)) #adds number 4 from example list
kaalude_summa = kaalude_summa - vahekaal + kaalude_tulemused[3] #new calculated weight
reiting.remove(max(reiting)) #removes element from list [1,2,3,4]
kaalud.remove(kaalud[positsioon])
This works as long as the competitors with rating 1,2,3 or 4 weight + other three weights (6,7,8) isn't bigger than 325.
I understand this could be done somehow with two for loops but I tried three times and failed miserably.
It is compulsory to write code in Estonian in our school so I apologize for not writing in English.
I really hope that what I wrote is readable and you are able to help me. Thank you in advance.