-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKaprekars_Constant-v2.py
50 lines (39 loc) · 1.37 KB
/
Kaprekars_Constant-v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""[Challenge] Kaprekar's Constant - medium level
Number of four digits
Arrange the digits descending and ascending
Subtract and repeat this until the result stays the same.
Input: 101-9999, no repetitions for 3-digit numbers,
max 2 repetitions in 4-digit numbers.
By Dick Stada - NL, June, 2018
"""
from re import match
def find_kaprekar(n):
old = ""
while n != old:
old = n
low = ''.join(sorted(n))
high = ''.join(reversed(low))
n = str(int(high) - int(low))
print("{} - {} = {}".format(high, low, n))
return n
format = "[0-9]{3,4}" # must be 3 or 4 digits
while True:
new = input("Give number of three or four digits: ")
print(new)
# if match(format, new) and int(new) > 100: # input of 5 digits allowed! Why??
if match(format, new) and (100 < int(new) < 10000):
low = str(''.join(sorted(new)))
counter = 1
for i in range(0, len(low) - 1):
if low[i] == low[i + 1]:
counter += 1
if len(new) // 2 >= counter:
break
else:
if len(new) == 3:
print("No equal digits in a 3-digit number, please.")
else:
print("Only 2 digits may be the same in a 4-digit number.")
else:
print("Wrong input. Try again.")
print("Kaprekar's Constant: {}".format(find_kaprekar(new)))