Standardize Mobile Number Using Decorators

Sort by

recency

|

222 Discussions

|

  • + 0 comments
    def is_valid_phone(phone):
        return (len(phone) in range(10, 14) and
                phone[1:].isdigit())
    
    def wrapper(f):
        def fun(l):
            if len(l) == 0 or not isinstance(l, list):
                raise ValueError("Input must be a non-empty list")
            
            invalid_numbers = [phone for phone in l if not is_valid_phone(phone)]
            if invalid_numbers:
                raise ValueError(f"Invalid phone numbers: {', '.join(invalid_numbers)}")
            
            return f(f"+91 {phone[-10:-5]} {phone[-5:]}" for phone in l)
        return fun
    
  • + 0 comments

    Here is HackerRank Standardize Mobile Number Using Decorators in Python solution - https://programmingoneonone.com/hackerrank-standardize-mobile-number-using-decorators-solution-in-python.html

  • + 0 comments
    def wrapper(f):
        def fun(l):
            return f(sorted([f"+91 {num[-10:-5]} {num[-5:]}" for num in l]))
        return fun
    
    @wrapper
    def sort_phone(l):
        print(*sorted(l), sep='\n')
    
    if __name__ == '__main__':
        l = [input() for _ in range(int(input()))]
        sort_phone(l) 
    
  • + 0 comments

    More pythonic way and energy efficient execution

    def fun(l):
            nums = sorted([num[-10:-5] + num[-5:] for num in l if len(num) >= 10])
            print('\n'.join(f"+91 {str(num)[0:5]} {str(num)[5:]}" for num in nums))
        return fun
    
  • + 0 comments
    def fun(l):
        li = []
        for i in l:
            if len(i) == 10:
                x = i[0:5] + i[5:]
                li.append(x)
            elif len(i) > 10 and i[0] == "0":
          
                y = i[1:6] + i[6:]
                li.append(y)
            elif len(i) > 10 and i[0] == "+":
      
                w = i[3:8] + i[8:]
                li.append(w)
            else:
    
                z = i[2:7] + i[7:]
                li.append(z)
        
        li.sort()  # Sort the numbers
        
        for j in li:
            p = str(j)
            print(f"+91 {p[0:5]} {p[5:]}")