2

So I'm having trouble with printing out the largest number of a nested tuple. Let me backtrack here a bit. So I got a really good idea of how to separately print out elements, how to assign separated elements into a new tuple, and how to print out the largest number of a regular tuple. Well, I was given a challenge not that long ago that wants me to write a program that prints out the largest number of a nested tuple. At first, I thought this might be easy, but I keep printing out the same variables. Let me show you what I have so far.

my_nested_tuple = ((2,12), (24, 7), (9, 18), (22, 13))

def highest_element(l):
    my_max = l[0]
    for num in l:
        if my_max < num:
            my_max = num
    return my_max

print highest_element (my_nested_tuple)

This is as far as I gotten. The program keeps printing out (24,7), but I want it to print out the highest element only (which is 24).

Any help or hints will be much appreciated. Thanks!

EDIT: Keep in mind that I'm in the data structures lesson in intro computer science and haven't gone any further than that.

Dillion
  • 37
  • 4
  • 2
    Will the tuples be arbitrarily nested, or only nested using the structure you've described here. In other words are these valid inputs: `((2,12,24),(1,2))`, `((2, (12, 24)), (1,2))`, `(2, (1,2))`? – Robᵩ Feb 13 '18 at 18:01
  • Your algorithm doesn't work correctly. Try `my_nested_tuple = ((2,12), (24, 7), (9, 18), (1, 600))` and it still picks `(24,7)` because your code is comparing tuples, not integers. See: https://stackoverflow.com/q/5292303/3901060 – FamousJameous Feb 13 '18 at 18:02
  • Not sure it is a duplicate: you are suggesting to combine two questions to answer this. Plus, it is not even the only approach (although the most general) to solve the problem. – FLab Feb 13 '18 at 18:55

5 Answers5

3

You can calculate the maximum among the maximum values within each tuple.

Here is a short solution:

max(map(max, my_nested_tuple))

which is equivalent to:

max(max(el) for el in my_nested_tuple)
FLab
  • 7,136
  • 5
  • 36
  • 69
1

Here is one way, calculating a single max on a lazy generator.

from itertools import chain

my_nested_tuple = ((2,12), (24, 7), (9, 18), (22, 13))

max(chain.from_iterable(my_nested_tuple))

# 24
jpp
  • 159,742
  • 34
  • 281
  • 339
0

Convert to one tuple then using max

max(sum(my_nested_tuple,()))
Out[720]: 24

Notice :

well, I don't have an online reference handy, but it is quadratic time, since + between sequences is linear. Indeed, the sum function itself prevents you if you try to use strings, e.g. see what happens if you try TypeError: sum() can't sum strings [use ''.join(seq) instead] Finally, from the docstring: "This function is intended specifically for use with numeric values and may reject non-numeric types."

BENY
  • 317,841
  • 20
  • 164
  • 234
  • this: `sum(my_nested_tuple,())` is an anti-pattern. Avoid it. – juanpa.arrivillaga Feb 13 '18 at 18:02
  • @juanpa.arrivillaga - I'm not doubting you, but do you have an online reference for that statement? – Robᵩ Feb 13 '18 at 18:04
  • @Robᵩ well, I don't have an online reference handy, but it is quadratic time, since `+` between sequences is linear. Indeed, the `sum` function itself prevents you if you try to use strings, e.g. see what happens if you try `TypeError: sum() can't sum strings [use ''.join(seq) instead]` Finally, from the docstring: "This function is intended specifically for use with numeric values and may reject non-numeric types." – juanpa.arrivillaga Feb 13 '18 at 18:10
0
from itertools import chain
max(chain(*my_nested_tuple))

It flattens the tuple first, then computes the max.

kszl
  • 1,203
  • 1
  • 11
  • 18
0

You can sort it, first by the first element of each tuple and then by the second element, and finally compare the biggest of the two:

by_first = sorted(my_nested_tuple)
by_second = sorted(my_nested_tuple, key=lambda tup: tup[1])
print(max(by_first[-1][0], by_second[-1][1]))

Or use directly the max() function like this:

by_first = max(my_nested_tuple)[0]
by_second = max(my_nested_tuple, key=lambda tup: tup[1])[1]
print(max(by_first, by_second))
Andreas K.
  • 9,282
  • 3
  • 40
  • 45