I am trying to write a function that takes in a list of employee object and returns the object that corresponds to the employee with the youngest age. I am not sure why it is breaking. I know there are many ways to do this but I am particularly interested in solving it through the reduce tool
from functools import reduce as r
class Employee:
bonus = 0
def __init__(self,firstname,lastname,age,salary):
self.fullname = firstname + " "+ lastnamestname
self.email = "{}{}@outlook.com".format(firstname,lastname)
self.age = age
self.compensation = salary + self.bonus
e1 = Employee("Adam","George",33,100)
e2 = Employee("Samuel","Steans",35,133)
e3 = Employee("Laura","Nobel",25,200)
e4 = Employee("David","Chan",21,100)
e5 = Employee("Ben","Smith",80,90)
e6 = Employee("Santa","Ergory",19,120)
e7 = Employee("Tim","Smith",18,150)
e8 = Employee("Paul","Goodfellow",50,180)
employees = [e1,e2,e3,e4,e5,e6,e7,e8]
def getyoungest(emps):
return r(lambda x,y:x.fullname if x.age < y.age else y.fullname,emps )
youngest = getyoungest(employees)
print(youngest)
reduce, see artima.com/weblogs/viewpost.jsp?thread=98196filter/mapoperations, notreduce. What Guido mentions in that link is that he personally prefers to just see reduce replaced by a loop with the accumulation explicitly