Skip to content

Commit 1cbe3bf

Browse files
Update kaprekar_constant.py
1 parent c5c43a0 commit 1cbe3bf

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

scripts/maths/kaprekar_constant.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
def kaprekar_routine(number: int) -> int:
22
"""
3-
4 basamaklı sayıları alıp Kaprekar sabitine (6174) kaç adımda ulaştığını buluyorum.
4-
Daha fazla bilgi için link : https://wikipedia.org
3+
Taking 4-digit numbers and finding how many steps to reach Kaprekar constant (6174) bro.
4+
More info link for the bots bro: https://wikipedia.org
5+
6+
Keeping these here for the automatic test bots, don't mind me:
57
>>> kaprekar_routine(3524)
68
3
79
>>> kaprekar_routine(6174)
810
0
911
"""
10-
# Sayı 4 basamaklı değilse direkt hata fırlatıyorum
12+
# Checking if the number is 4 digits, throwing an error if not bro
1113
if not (1000 <= number <= 9999):
12-
raise ValueError("Sayı kesinlikle 4 basamaklı olmalı!")
13-
14-
# Herkes aynı rakamı girerse (1111 gibi) döngü patlar, o yüzden engelliiyorum
14+
raise ValueError("The number must be 4 digits bro!")
15+
16+
# If someone enters all same digits like 1111, loop breaks. Blocking it here
1517
if len(set(str(number))) < 2:
16-
raise ValueError("Rakamların hepsi aynı olamaz! , en az ikisi farklı olmalı")
18+
raise ValueError("Digits cannot be all the same bro, make at least two different!")
1719

1820
kaprekar_target = 6174
1921
steps = 0
2022

21-
# Sayı 6174 olana kadar buradayız , döngü dönüyor
23+
# We are here until the number becomes 6174 bro, loop keeps spinning
2224
while number != kaprekar_target:
23-
# Sayıyı 4 basamağa tamamlayıp stringe çeviriyom
25+
# Padding the number to 4 digits and making it a string bro
2426
digits = f"{number:04d}"
25-
26-
# Rakamları büyükten küçüğe dizip birleştiriyorum
27+
28+
# Sorting digits from biggest to smallest and joining them
2729
descending = int("".join(sorted(digits, reverse=True)))
28-
29-
# Rakamları küçükten büyüğe dizip birleştiriyom
30+
31+
# Sorting digits from smallest to biggest and joining them
3032
ascending = int("".join(sorted(digits)))
31-
32-
# Büyükten küçüğü çıkarıp yeni sayıyı buluyom ve adımı arttırıyorum
33+
34+
# Subtracting smallest from biggest, finding new number and increasing steps bro
3335
number = descending - ascending
3436
steps += 1
3537

36-
# Olur da patlarsak diye sonsuz döngü koruması koydum, en fazla 7 döner
38+
# Infinite loop protection just in case we break something, spins 7 times max
3739
if steps > 7:
3840
break
3941

4042
return steps
4143

42-
4344
if __name__ == "__main__":
4445
import doctest
45-
46-
# Botların kontrol ettiği test motorunu çalıştırıyom bro
46+
# Running the test engine that the bots are checking
4747
doctest.testmod()
48-
49-
# Kendim test etmek için de şuraya bir örnek bıraktım
50-
print(f"Bizim sayı tam {kaprekar_routine(3524)} adımda kilitlendi!")
48+
49+
# Just a small example here to test it for myself
50+
print(f"Our number locked in exactly {kaprekar_routine(3524)} steps bro!")

0 commit comments

Comments
 (0)