0

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.

1
  • pastebin.com/1JPKqUbh added the whole code into pastebin if my explanation about what happens isn't clear enough. Commented Sep 27, 2014 at 16:09

2 Answers 2

1

a brute force approach would be to find all possible combinations of 4 sledders. for each one, find their combined weights and throw away those that exceed 325. of the remaining ones, select the one with the highest ratings.

Sign up to request clarification or add additional context in comments.

2 Comments

It is an option but unfortunately we're not allowed to go with this brute force and the code has to work in normal timeframe so two loops are better i guess.
I don't know how to explain this thing we're studying in English, but it's something like this: stackoverflow.com/questions/16555978/… and it has to be exponential (cˇn)
0

One option to solve this is to put more while-s after. Although IT is still breakable, but it will check for elements in every position.

while (kaalude_summa > 325): #AGA KUI JUHTUB SEE NALI, ET MAX REITINGUD LIIDETAKSE JA IKKA ON PAKSUD
    if reiting == []:
        break
    positsioon = reiting.index(max(reiting)) #max reitingu positsioon
    if kaalud[positsioon] < kaalude_tulemused[3]: 
        kaalude_tulemused[3] = kaalud[positsioon]
        tulemuslist.remove(min(tulemuslist))
        tulemuslist.append(max(reiting))
        kaalude_summa = kaalude_summa - vahekaal + kaalude_tulemused[3]
    reiting.remove(max(reiting))
    kaalud.remove(kaalud[positsioon])

if kaalude_summa > 325:
    reiting = reiting_safecopy[0:len(reiting_safecopy)]
    tulemuslist = tulemuslist_safecopy[0:len(tulemuslist_safecopy)]
    kaalud = kaalud_safecopy[0:len(kaalud_safecopy)]

while (kaalude_summa > 325): #KUI ESIMENE WHILE EI AIDANUD
    if reiting == []:
        break
    positsioon = reiting.index(max(reiting)) #max reitingu positsioon
    if kaalud[positsioon] < kaalude_tulemused[2]: 
        kaalude_tulemused[2] = kaalud[positsioon]
        tulemuslist.remove(min(tulemuslist))
        tulemuslist.append(max(reiting))
        kaalude_summa = kaalude_summa - vahekaal + kaalude_tulemused[3]
    reiting.remove(max(reiting))
    kaalud.remove(kaalud[positsioon])

if kaalude_summa > 325:
    reiting = reiting_safecopy[0:len(reiting_safecopy)]
    tulemuslist = tulemuslist_safecopy[0:len(tulemuslist_safecopy)]
    kaalud = kaalud_safecopy[0:len(kaalud_safecopy)]

while (kaalude_summa > 325): #KUI TEINE WHILE KA EI AIDANUD
    if reiting == []:
        break
    positsioon = reiting.index(max(reiting)) #max reitingu positsioon
    if kaalud[positsioon] < kaalude_tulemused[1]: 
        kaalude_tulemused[1] = kaalud[positsioon]
        tulemuslist.remove(min(tulemuslist))
        tulemuslist.append(max(reiting))
        kaalude_summa = kaalude_summa - vahekaal + kaalude_tulemused[3]

    reiting.remove(max(reiting))
    kaalud.remove(kaalud[positsioon])

if kaalude_summa > 325:
    reiting = reiting_safecopy[0:len(reiting_safecopy)]
    tulemuslist = tulemuslist_safecopy[0:len(tulemuslist_safecopy)]
    kaalud = kaalud_safecopy[0:len(kaalud_safecopy)]

while (kaalude_summa > 325): #SIIS VAHETAME VIIMAST ELEMENTI!!
    if reiting == []:
        break
    positsioon = reiting.index(max(reiting)) #max reitingu positsioon
    if kaalud[positsioon] < kaalude_tulemused[0]: 
        kaalude_tulemused[0] = kaalud[positsioon]
        tulemuslist.remove(min(tulemuslist))
        tulemuslist.append(max(reiting))
        kaalude_summa = kaalude_summa - vahekaal + kaalude_tulemused[3]

    reiting.remove(max(reiting))
    kaalud.remove(kaalud[positsioon])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.