← Back to challenges

The Alternating Numbers

PythonHardnumbersvalidation

Instructions

In this challenge, you have to establish if an integer num is Alternating. To be Alternating, num must be positive and every digit in its sequence must have a different parity than its next and its previous digit.

Given an integer num, implement a function that returns True is num is an Alternating number, or False if it's not.

Examples

is_alternating(123) ➞ True
# 1 is odd, 2 is even, 3 is odd

is_alternating(67) ➞ True
# 6 is even, 7 is odd

is_alternating(2380) ➞ False
# 2 is even, 3 is odd, 8 is even, 0 is even

is_alternating(75) ➞ False
# 7 is odd, 5 is odd

Notes

  • A single-digit number is trivially considered Alternating, given the absence of neighboring digits.
  • The first digit has to be compared only to its next neighbor, and the last digit has to be compared only to its previous neighbor.
  • Every non-positive integer must be treated as False.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.