1

I am trying to return the difference of max and min of float items in the sequence. The output shall be an int, but the algorithm given below returns a list instead. Could someone let me know what I am missing?

def flatten(*args):
    res = []
    for el in args:
        if isinstance(el,(tuple)):
            if el != ():
                res.extend(flatten(*el))
                continue
            else:
                return "Empty"
        res.append(el)
    diff = [max(res)-min(res)]
    return diff
>>> a
(1, 2)
>>> b
(1, (7, 8))
>>> flatten(a,b)
[1]
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Yu Ni
  • 65
  • 4
  • 8

2 Answers2

0

The following flatten(*args) function returns the difference between the highest number parameter value given and the lowest number parameter value given:

def flatten(*args):
  return max(args) - min(args)

example calls to flatten(*args):

>>> flatten(1,2)
1
>>> flatten(10,2)
8
>>> flatten(10,2,20)
18
nuiun
  • 764
  • 8
  • 19
0

When you assign diff, you have max(res) - min(res) inside square brackets, which is used only when you have elements in a list.

Instead, do the following:

diff = max(res) - min(res)