xonecas.io
387
Pe

python-exercices / files

python-exercices / level_two / ex_12.py

1.2 KB
1#!/usr/bin/env python3
2
3# Question 12
4# Level 2
5
6# Question:
7# Write a program, which will find all such numbers between 1000 and 3000 (both included) such
8# that each digit of the number is an even number.
9# The numbers obtained should be printed in a comma-separated sequence on a single line.
10
11# Hints: In case of input data being supplied to the question, it should be assumed to be a
12# console input.
13
14
15# Naive solution from the prompt:
16values = []
17for i in range(1000, 3001):
18    s = str(i)
19    if int(s[0]) % 2 == 0 and int(s[1]) % 2 == 0 and int(s[2]) % 2 == 0 and int(s[3]) % 2 == 0:
20        values.append(s)
21
22print(",".join(values))
23
24
25# A way to support numbers with more or less than 4 digits:
26def hasAllEvenDigits(number):
27    number = str(number)
28    number_of_even_digits = 0
29    expected_len = len(number)
30
31    for char in number:
32        if int(char) % 2 == 0:
33            number_of_even_digits += 1
34        else:
35            break
36
37    return (number_of_even_digits == expected_len)
38
39
40if __name__ == '__main__':
41    all_even_digits_number = []
42
43    for n in range(1000, 3001):
44        if hasAllEvenDigits(n):
45            all_even_digits_number.append(str(n))
46
47    print(", ".join(all_even_digits_number))