简体   繁体   中英

convert following iterative code to recursive Python

6174 is known as Kaprekar's constant[1][2][3] after the Indian mathematician DR Kaprekar. This number is notable for the following property:

Take any four-digit number, using at least two different digits. (Leading zeros are allowed.) Arrange the digits in descending and then in ascending order to get two four-digit numbers, adding leading zeros if necessary. Subtract the smaller number from the bigger number. Go back to step 2.

Dattaraya Ramchandra Kaprekar

number="0011" 

print(" helo world,  lets do this: " , number)
i = 0
while number != "6174":
 sortedS = sorted(number)                            
 String[] sortedString = array[4] av strangen number
 reversed = sorted(number, reverse=True)            
 sortedIntMin = int(sortedS[0]+sortedS[1]+sortedS[2]+sortedS[3])
 reversedIntMax = int(reversed[0]+reversed[1]+reversed[2]+reversed[3])  
 i += 1
 number = str(reversedIntMax - sortedIntMin)                                           
 reversedIntMax - sortedIntMin
 print("det behovdes " , i , "iterationer for processen")

This is my unsuccessful attempt

def Kaprekar(number, i):
if number == 6174:
    return
elif number != 6174:
    sortedString = sorted(number)
    reversedString = sorted(number, reverse=True)
    sortedIntMin = int(sortedString[0]+sortedString[1]+sortedString[2]+sortedString[3])
    reversedIntMax = int(reversedString[0]+reversedString[1]+reversedString[2]+reversedString[3])
    num = reversedIntMax - sortedIntMin
    print("processen kors", num )
    return 1 + Kaprekar(str(num), i)

print(" helo world,  lets do this: ")
print("det behovdes " , Kaprekar("1547", 0) , "iterationer for processen")

there are three things that are wrong: -

  1. You don't need i. remove it from function definition.
  2. The variable you are passing is a string and you are comparing it with an integer, convert it to a string while comparing.
  3. You need to return 1 when number='6174', while you are returning None.
  4. Also, it can be done a bit clearer if list is joined after sorted and it can be directly converted to integer, (thanks endzior for the edit)

    try this : -

     def Kaprekar(number): if number == '6174': return 1 elif number != '6174': sortedString = ''.join(sorted(number)) reversedString = ''.join(sorted(number, reverse=True)) sortedIntMin = int(sortedString) reversedIntMax = int(reversedString) num = reversedIntMax - sortedIntMin print("processen kors", num ) return 1 + Kaprekar(str(num)) print(" helo world, lets do this: ") print("det behovdes " , Kaprekar("1547") , "iterationer for processen") 

number is a string, so in the first 2 if statements :

if number == '6174':
    return 1
else:

And as in another answer i variable is not needed here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM