Hello guys thank you for see my question :)
I'm trying to make KaprekarsConstant but it's not running because Hnum, Lnum is not int.
So i want to make it to int, but i don't know how to do it
Please help me
Thank you for your reading.
def KaprekarsConstant(num):
count = 1
while num != 6174:
Hnum = "".join(sorted(num))
Lnum = "".join(sorted(num, reverse=True))
num = Lnum - Hnum
count += 1
return count
1 Answer 1
To sort the number in ascending/descending order, you have to first cast it to a string, and then change the result back to an int for your computations, as follows:
Hnum = int("".join(sorted(str(num))))
Lnum = int("".join(sorted(str(num), reverse=True)))
This should allow your algorithm to run.
answered Jan 26, 2018 at 12:15
Arnav Borborah
11.8k8 gold badges51 silver badges95 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Choiuijin1125
I really appreciate your help. I just tried to use int at first, but it was not work. String is the anwser. Thank you again.
lang-py