In this Decorators 2 Name directory problem, You are given some information about N people. Each person has a first name, last name, age, and sex. Print their names in a specific format sorted by their age in ascending order i.e. the youngest person’s name should be printed first. For two people of the same age, print them in the order of their input.
Problem solution in Python 2 programming.
def sort_age(func): def inner(persons): persons = sorted(persons, key = lambda x: x[1]) return [func(name, sex) for (name, age, sex) in persons] return inner @sort_age def list_name(name, sex): title = {"M": "Mr. ", "F": "Ms. "} return title[sex] + name if __name__ == "__main__": persons = [] for i in range(input()): data = raw_input().split() name, age, sex = data[0] + ' ' + data[1], int(data[2]), data[3] persons.append([name, age, sex]) print 'n'.join(list_name(persons))
Problem solution in Python 3 programming.
def person_lister(f): def inner(people): return map(f, sorted(people, key=lambda x: int(x[2]))) return inner
Problem solution in pypy programming.
def person_lister(f): def inner(people): # complete the function return (f(person) for person in sorted(people, key=operator.itemgetter(2))) return inner
Problem solution in pypy3 programming.
def person_lister(f): def inner(people): age=[] for i in people: age.append(i[2]) age.sort() people_new=[] for i in age: for j in people: if j[2]==i: people_new.append(j) people.remove(j) break else: continue people=people_new for person in people: print(f(person)) return inner