Python Forum
Beginner, my recursion returns None, and related problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner, my recursion returns None, and related problem
#1
Absolute beginner at coding attempting to learn Python. Current chapter of "Slither into Python" is on recursion; my current task is to identify the largest number in a set recursively. The author's example works; mine does not. I don't understand why mine doesn't work or why the author's does work. Mine returns "None" despite a few attempts to define my way out of my issue. The author's returns the correct number even though it looks like it's winding up with the wrong one.
import sys

def max_check(arr):       # My version, always returns None
	print(arr) 
	if len(arr) == 1:
		print(arr)        # It looks like the largest number is here!
		return arr[0]     # But now it's gone!
	elif arr[0] < arr[1]: # Not very tidy, but is this approach a problem?
		arr.pop(0)
		max_check(arr)
	elif arr[0] >= arr[1]:
		arr.pop(1)
		max_check(arr)


def maximum(lst):         # Author's version, returns correct number
    print(lst)
    if len(lst) == 1:
        print(lst)        # Why doesn't this print the largest number to console?
        return lst[0]
    else:
        m = maximum(lst[1:])
        if m > lst[0]:
            return m
        else:
            return lst[0]
	

def main():
	print(max_check([1, 2, 3, 2]))
	print(maximum([1, 2, 3, 2]))
	
if __name__ == "__main__":
    main()
Gives me...

Output:
[1, 2, 3, 2] [2, 3, 2] [3, 2] [3] [3] # Mission accomplished? None # Nope. [1, 2, 3, 2] [2, 3, 2] [3, 2] [2] [2] # huh? 3 # huh??
I didn't really know what to search for, because I don't know what I'm failing to understand. Help?
Reply


Messages In This Thread
Beginner, my recursion returns None, and related problem - by zpacemanzpiff - Jul-02-2020, 03:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange problem related to "python service" Pavel_47 1 1,407 Dec-07-2021, 12:52 PM
Last Post: Pavel_47
  I have an problem related to ujson in python dixitaditya248 2 2,665 Apr-12-2021, 08:11 AM
Last Post: bowlofred
  Although this is a talib related Q it's mostly related to python module installing.. Evalias123 4 5,686 Jan-10-2021, 11:39 PM
Last Post: Evalias123
  small beginner problem FelixReiter 2 1,865 Nov-17-2020, 03:26 PM
Last Post: FelixReiter
  Beginner having Syntax Error problem RyanHo 3 2,378 Sep-10-2020, 08:33 AM
Last Post: cnull
  R-PYTHON INTEGRATION RELATED PROBLEM arnab93 0 1,448 Jun-05-2020, 02:07 PM
Last Post: arnab93
  Beginner problem, replace function with for loop Motley_Cow 9 4,632 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  recursion function (python beginner) everafter 3 2,894 Aug-19-2019, 07:24 AM
Last Post: buran
  Beginner problem in python script Cedmo 3 2,781 Jul-04-2019, 08:22 PM
Last Post: Cedmo
  Beginner Problem python 2.7 Jonathan_levy 2 2,681 Jul-04-2018, 08:46 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020