Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerRank Decorators 2 – Name Directory solution in python

YASH PAL, 31 July 2024

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.

HackerRank Decorators 2 - Name Directory solution in python

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

coding problems solutions Python Solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes