51. deleteDigit


Task

Given some integer, find the maximal number you can obtain by deleting exactly one digit of the given number.

Example

  • For n = 152, the output should be
    deleteDigit(n) = 52;

  • For n = 1001, the output should be
    deleteDigit(n) = 101.

Input/Output

  • [execution time limit]
    4 seconds (py3)

  • [input] integer n
    Guaranteed constraints: 10 ≤ n ≤ 106.

  • [output] integer

My Solution

def deleteDigit(n):
    str_n = str(n)
    maxNumber = 0
    for i in range(len(str_n) - 1):
        if int(str_n[:i] + str_n[i + 1:]) > maxNumber:
            maxNumber = int(str_n[:i] + str_n[i + 1:])
    if int(str_n[:-1]) > maxNumber:
        maxNumber = int(str_n[:-1])
    return maxNumber