python - Is this an issue with my method of recursion? -
in response this question tried come own solution.
my solution in python 3.x
def efficientalgo(number, x): print("start number... " + str(number)) # see if number dividable three, if divide 3 if number % 3 == 0: print("dividing three...") number /= 3 print(number) # use recursion see if number can divided again number += efficientalgo(number, x) print("returning number division 3 now...") return number # divide number 2 if evenly dividable 2 if number % 2 == 0: print("dividing two...") number /= 2 print(number) # use recursion see if number can divided again number += efficientalgo(number, x) print("returning number division 2 now...") print(number) return number # if number not one, subtract 1 , call if number != 1: print("subtracting 1 now...") number -= 1 print(number) # use recursion see if number can divided again number += efficientalgo(number, x) print(number) return number # if number one, return , finish. if number == 1: print("returning 1 now... " + str(number)) return number print(efficientalgo(100, 1))
here "working" pythonfiddle
the output
start number... 100 dividing two... 50.0 start number... 50.0 dividing two... 25.0 start number... 25.0 subtracting 1 now... 24.0 start number... 24.0 dividing three... 8.0 start number... 8.0 dividing two... 4.0 start number... 4.0 dividing two... 2.0 start number... 2.0 dividing two... 1.0 start number... 1.0 returning 1 now... 1.0 -----above line correct output want----- ---------below line, have no idea going on-------- returning number division 2 now... 2.0 returning number division 2 now... 4.0 returning number division 2 now... 8.0 returning number division 3 now... 40.0 returning number division 2 now... 65.0 returning number division 2 now... 115.0 115.0
as can see, have implemented recursion incorrectly , getting sort of feedback loop in program gets down correct answer, keeps going after ive returned final number, in case one.
i not understand happening below lines have marked out in output
number should set = efficientalgo(..)
not +=
:
def efficientalgo(number): print("start number... " + str(number)) # see if number dividable three, if divide 3 if number % 3 == 0: print("dividing three...") number /= 3 print(number) # use recursion see if number can divided again number = efficientalgo(number) # divide number 2 if evenly dividable 2 if number % 2 == 0: print("dividing two...") number /= 2 print(number) # use recursion see if number can divided again number = efficientalgo(number) # if number not one, subtract 1 , call if number != 1: print("subtracting 1 now...") number -= 1 print(number) # use recursion see if number can divided again number = efficientalgo(number) return number
once expected output:
in [4]: efficientalgo(100) start number... 100 dividing two... 50.0 start number... 50.0 dividing two... 25.0 start number... 25.0 subtracting 1 now... 24.0 start number... 24.0 dividing three... 8.0 start number... 8.0 dividing two... 4.0 start number... 4.0 dividing two... 2.0 start number... 2.0 dividing two... 1.0 start number... 1.0 out[4]: 1.0
or return:
def efficientalgo(number): print("start number... " + str(number)) # see if number dividable three, if divide 3 if number % 3 == 0: print("dividing three...") number /= 3 print(number) # use recursion see if number can divided again return efficientalgo(number) # divide number 2 if evenly dividable 2 if number % 2 == 0: print("dividing two...") number /= 2 print(number) # use recursion see if number can divided again return efficientalgo(number) # if number not one, subtract 1 , call if number != 1: print("subtracting 1 now...") number -= 1 print(number) # use recursion see if number can divided again return efficientalgo(number) return number
Comments
Post a Comment