Skip to content

Instantly share code, notes, and snippets.

@thanhnguyen2187
Created December 1, 2020 03:33
Show Gist options
  • Save thanhnguyen2187/13900b9cab4b86f7fa4ae12e24d7df8a to your computer and use it in GitHub Desktop.
Save thanhnguyen2187/13900b9cab4b86f7fa4ae12e24d7df8a to your computer and use it in GitHub Desktop.
"""
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