Below is a segment of code I wrote for a class for a coding problem. I confused about why my use of the sort function doesn't work, in that it doesn't sort the list self.swimmers
at all (See the code below, the commented out part actually works) This is 30% of the full class. I didn't want to make this too long, so I didn't copy the rest here. It would be great to know the possible reasons why my line of sorting code didn't work.
def finish_time(self, swimmer):
found = False
for s in self.swimmers:
if swimmer == s.name:
swimmer = s
found = True
break
if not found:
return "No such swimmer"
swim_distance = self.laps * self.distance
swim_time = swim_distance / swimmer.speed + \
(self.laps - 1) * swimmer.turning
return swim_time
def winner(self, *k):
#for s in self.swimmers:
# s.finish_time = self.finish_time(s.name)
#self.swimmers.sort(key = lambda s: s.finish_time) # the above 3 lines work
self.swimmers.sort(key = lambda s: self.finish_time(s.name)) # this doesn't work
print(list(map(lambda s: self.finish_time(s.name), self.swimmers)))
if len(k) == 0:
return self.swimmers[0].name
else:
output = list(map(lambda s: s.name, self.swimmers[:k[0]]))
return output