<\/span><\/h2>\n\n\n\nLet’s use decorators to build a name directory! You are given some information about 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.<\/p>\n\n\n\n
For Henry Davids<\/em>, the output should be:<\/p>\n\n\n\nMr. Henry Davids<\/code><\/pre>\n\n\n\nFor Mary George<\/em>, the output should be:<\/p>\n\n\n\nMs. Mary George<\/code><\/pre>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains the integer N<\/em><\/strong>, the number of people.
N<\/em><\/strong> lines follow each containing the space separated values of the first name, last name, age and sex, respectively.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= N<\/em> <= 10<\/strong><\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nOutput N<\/em><\/strong> names on separate lines in the format described above in ascending order of age.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n3\nMike Thomson 20 M\nRobert Bustle 32 M\nAndria Bustle 30 F<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\nMr. Mike Thomson\nMs. Andria Bustle\nMr. Robert Bustle<\/code><\/pre>\n\n\n\nConcept<\/strong><\/p>\n\n\n\nFor sorting a nested list based on some parameter, you can use the itemgetter<\/em> library.<\/p>\n\n\n\n<\/span>Solution – Decorators 2 – Name Directory in Python<\/strong><\/span><\/h2>\n\n\n\nimport operator\ndef person_lister(func):\n def inner(people):\n return [func(p) for p in sorted(people, key = lambda x: (int(x[2])))]\n return inner\n@person_lister\ndef name_format(person):\n return (\"Mr. \" if person[3] == \"M\" else \"Ms. \") + person[0] + \" \" + person[1]\n\nif __name__ == '__main__':\n people = [input().split() for i in range(int(input()))]\n print(*name_format(people), sep='\\n')<\/pre>\n\n\n\n