Last active
April 5, 2025 19:41
-
-
Save saintsGrad15/59adefd17c17fa106394b1eeb38dbf82 to your computer and use it in GitHub Desktop.
Return the digit in `number` at the zero-indexed, left-to-right place `ltr_place` without coercing to a string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_digit(number: int, ltr_place: int) -> int: | |
if ltr_place < 0: | |
raise ValueError("ltr_place must be >= 0") | |
length = math.floor(math.log10(number)) + 1 | |
if ltr_place > length - 1: | |
raise ValueError(f"ltr_place is zero-indexed. {ltr_place} exceeds the length of {number}") | |
return number % pow(10, length - ltr_place) // pow(10, length - ltr_place - 1) | |
## Examples ## | |
get_digit(32546, -1) >> ValueError | |
get_digit(32546, 0) == 3 | |
get_digit(32546, 2) == 5 | |
get_digit(32546, 4) == 6 | |
get_digit(32546, 5) >> ValueError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment