<\/span><\/h2>\n\n\n\nLet’s dive into decorators! You are given N<\/strong><\/em> mobile numbers. Sort them in ascending order then print them in the standard format shown below:<\/p>\n\n\n\n+91 xxxxx xxxxx<\/code><\/pre>\n\n\n\nThe given mobile numbers may have +91<\/strong>, 91<\/strong> or 0<\/strong> written before the actual 10<\/strong> digit number. Alternatively, there may not be any prefix at all.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line of input contains an integer N<\/strong><\/em>, the number of mobile phone numbers.
N<\/strong><\/em> lines follow each containing a mobile number.<\/p>\n\n\n\n<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint N<\/em><\/strong> mobile numbers on separate lines in the required format.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n3\n07895462130\n919875641230\n9195969878<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n+91 78954 62130\n+91 91959 69878\n+91 98756 41230<\/code><\/pre>\n\n\n\nConcept<\/strong><\/p>\n\n\n\nLike most other programming languages, Python has the concept of closures. Extending these closures gives us decorators, which are an invaluable asset.
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.<\/p>\n\n\n\n
<\/span>Solution – Standardize Mobile Number Using Decorators in Python<\/strong><\/span><\/h2>\n\n\n\ndef wrapper(f):\n def fun(l):\n # complete the function\n f(['+91 ' + c[-10:-5] + ' ' + c[-5:] for c in l])\n return fun\n\n@wrapper\ndef sort_phone(l):\n print(*sorted(l), sep='\\n')\n\nif __name__ == '__main__':\n l = [input() for _ in range(int(input()))]\n sort_phone(l) <\/pre>\n\n\n\n