0

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

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.