Reputation: 19
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
Upvotes: 0
Views: 211
Reputation: 132
how about this?
>>> names=['a', 'b', 'c']
>>> heights=[10, 5, 7]
>>> [name for _, name in sorted(zip(heights,names), reverse=True)]
['a', 'c', 'b']
>>>
Upvotes: 1