0

Sorry for my ugly English. This is one of my homework.

I'm making function that finds the max integer in any list, tuple, integer.. like "max_val((5, (1,2), [[1],[2]])) returns 5"

When I ran my code, there was no syntax error. I ran as many various cases I can. But the homework system told me this code was incorrect. Anyone give me hint?

numList = []

def max_val(t): 
 
    if type(t) is int:
        numList.append(t)

    else:
        for i in range(len(t)):
            if t[i] is int:
                numList.append(t[i])
            else:
                max_val(t[i])

        return max(numList)
2
  • 1
    What is the purpose of the function? What does t represent? Also you should un-indent your return statement so that both cases return. Commented Sep 29, 2020 at 12:10
  • t is list or tuple or integer and it's mixed like [5, (1,2,[9,(101)]), [1],3,5,6,100,(5,7,8),[2,7]] Commented Sep 29, 2020 at 12:12

2 Answers 2

2

Your code gives wrong results when called several times:

>>> max_val((5,4,3))
5
>>> max_val((2, 1))
5

That's because numList is a global variable that you don't "reset" between calls of your function.

You can simplify your code quite a bit, without needing that global variable:

def max_val(t): 
    if isinstance(t, int):
        return t  # t is the only element, so it's by definition the biggest
    else:
        # Assuming max_val works correctly for an element of t,
        # return the largest result
        return max(max_val(element) for element in t)
Sign up to request clarification or add additional context in comments.

Comments

1

As explained in L3viathan's answer, the main issue with your code is that numList is a global variable. Here is a simple way to fix it without changing the logic of your code:

def max_val(t):
  numList = []                # local variable
  max_val_helper(t, numList)  # fill numList with elements from t
  return max(numList)

def max_val_helper(t, numList):  # this function modifies its second argument and doesn't return a value
  if type(t) is int:
    numList.append(t)
  else:
    for i in range(len(t)):
      max_val_helper(t[i], numList)

The function max_val_helper is recursive and appends all numbers in the nested iterables to its argument numList. This function doesn't have a return value; the effect of calling it is that it modifies its argument. This kind of function is sometimes called a "procedure".

The function max_val, on the other hand, is a "pure" function: it returns a value without any side-effect, like modifying its argument or a global variable. It creates a local variable numList, and passes this local variable to max_val_helper which fills it with the numberss from the nested iterables.

The code suggested in L3viathan's answer is arguably more elegant than this one, but I think it's important to understand why your code didn't work properly and how to fix it.

It's also good practice to differentiate between functions with side-effects (like modifying an argument, modifying a global variable, or calls to print) and functions without side-effects.

Comments

Your Answer

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.