Question

3. Skiers go fastest with skis whose length is about their height. Your team consists ofn members, with heights hi,h2,..., hn

0 0
Add a comment Improve this question Transcribed image text
Answer #1

So to accomplish this task we can follow below algorithm :

  1. sort the skies_list
  2. get the max height from the person_list
  3. get the index of the min abs_diff of skies_list values with this max_height of person
  4. assign the value at this index as max_skie_height.
  5. create a new list with skie_heights <= max_skie_height.
  6. if there are not sufficient skies with height <= max_skie_height then we have to include the skies with height more than the max_skie_height until the length of new list is equal to the person list.
  7. then sort both the lists , person and new_skies in descending order.
  8. generate a new list with abs difference values of person and new_skies at ith index.
  9. return the sum of this list.

the code for the same is attached below :

text code :

def minimum_diff(person , skies):

    skies.sort()   #sort the skies list to easily process this later

    max_height_person = max(person)   #get the maximum height of the person list , act as a upper limit for skies selection .
    diff = 10000    #initialise it to whatever impossible maximum value u want.
    max_i = -1
    for i ,x in enumerate(skies):    #this loop will get the index of the skies list whose absolute difference with the max_height_person is minimum.
        if abs(max_height_person - x) < diff:
            diff = max_height_person - x
            max_i = i

    max_skies_height = skies[max_i]   #get the skie height as maximum height based on max_height_person.
    new_skies = []   #create and fill this new skies list with skies whose height is <= max_skies_height.
    #So that we will have a list of skies whose height will be less than or eqaul to max_height_person.
    for i in range(len(skies)):
        if skies[i] <= max_skies_height:
            new_skies.append(skies[i])
    max_i += 1

    length_skies = len(new_skies)
    diff_length = len(person) - len(new_skies)
    for i in range(diff_length):   #this loop will only execute when the new_skies list will have skies less than the no of persons
    # cause there were not enough skies whose height <=max_skies_height
        new_skies.append(skies[max_i + i])

    new_skies.sort(reverse = True)   #sort both the lists in descending order
    person.sort(reverse = True)

    abs_diff = [abs(new_skies[i] - person[i]) for i in range(len(person))]   #make a new list as abs_diff getting the difference of heights of both lists's ith index.

    return sum(abs_diff) #return the sum of ths abs_diff list.


print(minimum_diff([1,2,3,4,5] , [5,8,7,6,9,10]))   #skies selected are 5,6,7,8,9 and their abs_diff = [4,4,4,4,4]
print(minimum_diff([1,2,3,4,5] , [5,8,7,6,9,10,1,2,3,4])) #skies selected are 1,2,3,4,5 and their abs_diff = [0,0,0,0,0]

Add a comment
Know the answer?
Add Answer to:
3. Skiers go fastest with skis whose length is about their height. Your team consists ofn...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT