0

I am trying to solve the following question and can't seem to find what the problem with my code is:

You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n.

For each index i, names[i] and heights[i] denote the name and height of the ith person.

Return names sorted in descending order by the people's heights.

Here is my code

    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:
            res=[];
            while len(heights)!=0:
                res.append(names[heights.index(max(heights))]);
                names.remove(names[heights.index(max(heights))])
                heights.remove(max(heights));
            return res
1

1 Answer 1

1

how about this?

>>> names=['a', 'b', 'c']
>>> heights=[10, 5, 7]
>>> [name for _, name in sorted(zip(heights,names), reverse=True)]
['a', 'c', 'b']
>>> 
Sign up to request clarification or add additional context in comments.

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.