Created
December 1, 2020 03:33
-
-
Save thanhnguyen2187/13900b9cab4b86f7fa4ae12e24d7df8a to your computer and use it in GitHub Desktop.
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
| """ | |
| Write a recursive function named `remove_odd_digits` that accepts an | |
| integer parameter n and returns the integer formed by removing the odd digits | |
| from n. For example, the call of `remove_odd_digits(6342118) should return | |
| 6248.` | |
| """ | |
| def remove_odd_digits(n: int) -> int: | |
| if ( | |
| n == 0 or | |
| ( | |
| n < 10 and | |
| n % 2 == 1 | |
| ) | |
| ): | |
| return 0 | |
| elif n % 2 == 0: | |
| return remove_odd_digits(n // 10) * 10 + n % 10 | |
| elif n % 2 == 1: | |
| return remove_odd_digits(n // 10) | |
| print(remove_odd_digits(12345)) | |
| print(remove_odd_digits(6342118)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment