Last active
July 27, 2020 04:20
-
-
Save JosephTLyons/04cad0a7696458f87e8aca78ed231ecb to your computer and use it in GitHub Desktop.
Prints odd numbers whose individual digits are odd and are not repeated
This file contains 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
#!/usr/bin/env python3 | |
# Only prints odd numbers whose individual digits are odd and are not repeated | |
def meets_criteria(number): | |
if number % 2 == 0: | |
return False | |
digits = [] | |
while number > 0: | |
digit = number % 10 | |
if digit % 2 == 0: | |
return False | |
if digit in digits: | |
return False | |
digits.append(digit) | |
number //= 10 | |
return True | |
# Or | |
def meets_criteria(number): | |
if number % 2 == 0: | |
return False | |
digits = [int(digit) for digit in str(number)] | |
if any([digit % 2 == 0 for digit in digits]): | |
return False | |
if len(digits) != len(set(digits)): | |
return False | |
return True | |
try: | |
upper_range = int(input("Specify upper range: ")) | |
except ValueError: | |
upper_range = 50 | |
count = 1 | |
for i in range(1, upper_range + 1): | |
if meets_criteria(i): | |
print("{}) {}".format(count, i)) | |
count += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment