Onsite

Write a function solution that, given an integer N, returns the maximum possible value obtained by inserting one '5' digit inside the decimal representation of integer N.

Examples:

  1. Given N = 268, the function should return 5268.
  2. Given N = 670, the function should return 6750.
  3. Given N = 0, the function should return 50.
  4. Given N = -999, the function should return -5999.
def solution(N):
    j = 0

    if N < 0:
        N = abs(N)
        for i in str(N):
            if int(str(N)[0]) > 5:
                return int('-' +'5' + str(N))
            if int(i) < 5:
                j += 1
        
        result = int('-' + str(N)[:j] +'5' + str(N)[j:int(len(str(N)))])
    
    if N == 0:
        return 50

    for i in str(N):
        if int(str(N)[0]) < 5:
            return int('5' + str(N))
        if int(i) > 5:
            j += 1
        
        result = int(str(N)[:j] +'5' + str(N)[j:int(len(str(N)))])
    return result