Skip to content

adding the remove digit algorithm #6708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 10, 2023
42 changes: 42 additions & 0 deletions maths/remove_digit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def remove_digit(num: int) -> int:
"""

returns the biggest possible result
that can be achieved by removing
one digit from the given number

>>> remove_digit(152)
52
>>> remove_digit(6385)
685
>>> remove_digit(-11)
1
>>> remove_digit(2222222)
222222
>>> remove_digit("2222222")
Traceback (most recent call last):
TypeError: only integers accepted as input
>>> remove_digit("string input")
Traceback (most recent call last):
TypeError: only integers accepted as input
"""

if type(num) == int:
num_str = str(abs(num))
num_transpositions = [
[char for char in num_str] for char in range(len(num_str))
]
for index in range(len(num_str)):
num_transpositions[index].pop(index)
return sorted(
[
int("".join([char for char in transposition]))
for transposition in num_transpositions
]
)[-1]
else:
raise TypeError("only integers accepted as input")


if __name__ == "__main__":
__import__("doctest").testmod()