mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
adding the remove digit algorithm (#6708)
This commit is contained in:
parent
997d56fb63
commit
6939538a41
37
maths/remove_digit.py
Normal file
37
maths/remove_digit.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
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 not isinstance(num, int):
|
||||||
|
raise TypeError("only integers accepted as input")
|
||||||
|
else:
|
||||||
|
num_str = str(abs(num))
|
||||||
|
num_transpositions = [list(num_str) for char in range(len(num_str))]
|
||||||
|
for index in range(len(num_str)):
|
||||||
|
num_transpositions[index].pop(index)
|
||||||
|
return max(
|
||||||
|
int("".join(list(transposition))) for transposition in num_transpositions
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
__import__("doctest").testmod()
|
Loading…
Reference in New Issue
Block a user