Skip to content

Delete Digit

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

    delete_digit(n) = 52
  • For n = 1001, the output should be

    delete_digit(n) = 101

Input/Output

  • [input] integer n

Solution

py
def delete_digit(n):
    s = str(n)
    max_n = 0

    for i in range(len(s)):
        tmp = int(s[0:i] + s[i+1:])
        if tmp > max_n:
            max_n = tmp

    return max_n


print(delete_digit(1001))
js
// read the python file from this folder and convert it to javascript

function deleteDigit(n) {
  let max = 0;
  let str = n.toString();
  for (let i = 0; i < str.length; i++) {
    let num = parseInt(str.slice(0, i) + str.slice(i + 1));
    if (num > max) {
      max = num;
    }
  }
  return max;
}

console.log(deleteDigit(152));

my thoughts are neither my employer's nor my wife's