Friday, December 13, 2019

Standardize Mobile Number Using Decorators - Hacker Rank Solution

Let's dive into decorators! You are given  mobile numbers. Sort them in ascending order then print them in the standard format shown below:

Standardize Mobile Number Using Decorators - Hacker Rank Solution

To solve the above question, make a list of the mobile numbers and pass it to a function that sorts the array in ascending order. Make a decorator that standardizes the mobile numbers and apply it to the function.

number = list()
N = int(raw_input())
for i in range(N):
    number.append(str(raw_input()))

def mobile(function):
    def input(number):
            return sorted([function(i) for i in number])
    return input

@mobile
def standardize(number):
 return "+91" + " " + number[-10:-5] + " " + number[-5:]

print '\n'.join(standardize(number))

No comments:

Post a Comment

Powered by Blogger.