I came across a python program question. Below is the question and its corresponding solution.
I just could not understand the flow of the program. Especially the @person_lister
, and its significance.
Also, print(*name_format(people), sep='\n')
name_format here is taking 2D array instead of 1D.
As per the definition of name_format it possibly expects a 1D array.
Can someone please explain me the flow of this program I just cannot understand it.
Appreciate your help. Thanks.
Question https://www.hackerrank.com/challenges/decorators-2-name-directory/problem
Solution:
import operator
people = [input().split() for i in range(int(input()))]
def person_lister(f):
def inner(people):
return [f(person) for person in sorted(people, key=operator.itemgetter(2))]
return inner
@person_lister
def name_format(person):
return ("Mr. " if person[3] == "M" else "Ms. ") + person[0] + " " + person[1]
print(*name_format(people), sep='\n')