I was working on a recursion algorithm and needed some help, so I came here and did some digging. What I found works for what I wanted it to do (Link: python minmax using only recursion), but I don't understand why and how it works. Here is the code:
def minMax(A,n):
if A:
header = A[0]
tailend = A[1:]
if tailend != []:
minimum, maximum = minMax(tailend,n-1)
return [header, minimum][minimum < header], [header, maximum][maximum > header]
return header, header
return A
intlist = [30, 50, 20, 70, 10, 80, 25, 100, 60, 40]
print(minMax(intlist,len(intlist)))
What I dont understand is this piece: return [header, minimum][minimum < header], [header, maximum][maximum > header] How do the brackets work? As far as I know, brackets are used for slicing, lists, indexing, and the sort. But here, it appears that there are some sort of keywords going on from behind the scenes and I just can't figure it out. Any help would be appreciated.