-2

Doing it for a homework,but don't get right results.

I am trying to write a function which returns the maximum value from either a list or tuple or tuple containing a list. Facing difficulty in determining how to exactly loop through a tuple and if it has a list look into it,and when all's done,find the maximum value recursively. Read the for example for what I mean.

def max_val(t): 
    """ t, tuple or list
        Each element of t is either an int, a tuple, or a list
        No tuple or list is empty
        Returns the maximum int in t or (recursively) in an element of t """ 
    # Your code here

For example,

• max_val((5, (1,2), [[1],[2]])) returns 5. • max_val((5, (1,2), [[1],[9]])) returns 9.

So far, I have tried the max and sorting t,but that does not give right answer.

raven_richard
  • 29
  • 2
  • 6
  • What have you tried so far? (Hint, this should help: https://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable) – havanagrawal Jul 29 '17 at 16:10
  • Alternatively, since you know that the input will either be an int, or a tuple or a list, and nothing else, you can use the inbuilt `type` function. Read the documentation here: https://docs.python.org/3/library/functions.html#type – havanagrawal Jul 29 '17 at 16:16
  • you should really not use type to typecheck in python ... it creates bad habits that will fail with more complicated types (ie inheritance) – Joran Beasley Jul 29 '17 at 16:19
  • You can make it a string first (see answer below) – whackamadoodle3000 Jul 30 '17 at 23:31

4 Answers4

4

(Disclaimer: as you mentioned in the question that this is a homework assignment, I strongly suggest you to first have a go solving the problem yourself, before "peeking" into the following solution (although there are many ways to do this). A good way to learn solving a complex problem is to first break it down into smaller "chunks", come up with some very simple inputs and expected outputs, followed by implementing your own codes. Use Google searches to find tips / hints. I'm putting my own solution here purely for the benefit of sparking inspirations. There may be more elegant / efficient way to do this.)

 Step 1: Break this problem into two smaller parts

For ease of understanding I'd break this down into two steps:

 Step 2: Visualise sample inputs and expected outputs

Before even attempting implementing codes, I always find it beneficial to "visualise" some very simple example inputs and expected outputs. Let's visualise some scenarios.

 Scenario 1 - Input is an already flatten list

  • input: [1, 2, 3, 4, 5, 6]
  • part 1 (deep flattening) output: [1, 2, 3, 4, 5, 6]
  • part 2 (max element in list) output: 6

 Scenario 2 - Input is an already flatten tuple

  • input: (1, 2, 3, 4, 5, 6)
  • part 1 (deep flattening) output: [1, 2, 3, 4, 5, 6]
  • part 2 (max element in list) output: 6

 Scenario 3 - Input is a deeply nested list

  • input: [1, [2, [3, 4, 5, 6]]]
  • part 1 (deep flattening) output: [1, 2, 3, 4, 5, 6]
  • part 2 (max element in list) output: 6

 Scenario 4 - Input is a deeply nested tuple

  • input: (1, (2, (3, 4, 5, 6)))
  • part 1 (deep flattening) output: [1, 2, 3, 4, 5, 6]
  • part 2 (max element in list) output: 6

 Scenario 5 - Input is a deeply nested list/tuple combined. eg 1

  • input: [1, (2, [3, 4, 5, 6])]
  • part 1 (deep flattening) output: [1, 2, 3, 4, 5, 6]
  • part 2 (max element in list) output: 6

 Scenario 6 - Input is a deeply nested list/tuple combined. eg 2

  • input: (1, [2, (3, 4, 5, 6)])
  • part 1 (deep flattening) output: [1, 2, 3, 4, 5, 6]
  • part 2 (max element in list) output: 6

 Scenario 7 - Input is an integer

  • input: 6
  • part 1 (deep flattening) output: [6]
  • part 2 (max element in list) output: 6

 Scenario 8 - (optional) Input is an empty list

  • input: []
  • part 1 (deep flattening) output: []
  • part 2 (max element in list) output: None

 Scenario 9 - (optional) Input is an empty tuple

  • input: ()
  • part 1 (deep flattening) output: []
  • part 2 (max element in list) output: None

 Step 3 - implement Part 1 (deep flattening)

This is my code (works for some test case scenarios above. Tweak as needed to make it more robust).

def deep_flatten(t):

    # if t is an int, just return a list with that int in it. No recursions needed.
    if isinstance(t, int):
        return [t]

    # if t is a list or tuple, do some recursions to deep-flatten it:        
    elif isinstance(t, (list, tuple)):

        # initialise result
        result = []

        # define a recursive function
        def update_result(t):
            for item in t:
                if isinstance(item,(list, tuple)):
                    update_result(item)
                else:    
                    result.append(item)

        # mutate result by performing recursions
        update_result(t)  

        # return it
        return result

 Step 4 - implement Part 2 (max element of a list)

This is the ultimate code in question, using the deep_flatten helper function.

def max_val(t):
    """ t, tuple or list
        Each element of t is either an int, a tuple, or a list
        No tuple or list is empty
        Returns the maximum int in t or (recursively) in an element of t """

    # use our helper deep flattening function to output a flatten list
    t_flatten = deep_flatten(t)

    # empty tuple / list case
    if len(t_flatten) == 0:
        return None

    # return the max element in a non-empty list / tuple input
    return max(t_flatten)

 Step 5 - do some testing

Do some testings against the scenarios. print out the variables to check result is as expected.

Scenario 1

t1 = [1, 2, 3, 4, 5, 6]
t1_flatten = deep_flatten(t1) # -> [1, 2, 3, 4, 5, 6]
t1_max = max_val(t1)  # -> 6

Scenario 2

t2 = (1, 2, 3, 4, 5, 6)
t2_flatten = deep_flatten(t2) # -> [1, 2, 3, 4, 5, 6]
t2_max = max_val(t2)  # -> 6

Scenario 3

t3 = [1, [2, [3, 4, 5, 6]]]
t3_flatten = deep_flatten(t3) # -> [1, 2, 3, 4, 5, 6]
t3_max = max_val(t3)  # -> 6

Scenario 4

t4 = (1, (2, (3, 4, 5, 6)))
t4_flatten = deep_flatten(t4) # -> [1, 2, 3, 4, 5, 6]
t4_max = max_val(t4)  # -> 6

Scenario 5

t5 = [1, (2, [3, 4, 5, 6])]
t5_flatten = deep_flatten(t5) # -> [1, 2, 3, 4, 5, 6]
t5_max = max_val(t5)  # -> 6

Scenario 6

t6 = (1, [2, (3, 4, 5, 6)])
t6_flatten = deep_flatten(t6) # -> [1, 2, 3, 4, 5, 6]
t6_max = max_val(t6)  # -> 6

Scenario 7

t7 = 6
t7_flatten = deep_flatten(t7) # -> [6]
t7_max = max_val(t7)  # -> 6

Scenario 8

t8 = []
t8_flatten = deep_flatten(t8) # -> []
t8_max = max_val(t8)  # -> None

Scenario 9

t9 = ()
t9_flatten = deep_flatten(t9) # -> []
t9_max = max_val(t9)  # -> None

 Sample case as per the question:

print(  max_val((5, (1,2), [[1],[2]]))  ) # -> 5

print(  max_val((5, (1,2), [[1],[9]]))  )  # -> 9
Atlas7
  • 2,726
  • 4
  • 27
  • 36
  • 2
    +1 for putting in far more effort than this question deserves ... but i doubt that this will satisfy his homework requirement as i suspect recursivly building a 1d list is insufficient and the teacher expects a recursive solution that only is O(N) (ie not simply calling max on a modified list) +1 all the same though.... – Joran Beasley Jul 29 '17 at 22:57
  • 2
    Fantastic explanation Atlas7 English is little week, but this how I learn, reading code. I tried but couldn't flatten the tuple or list. knew to call max. – raven_richard Jul 30 '17 at 05:27
  • 2
    @Joran Beasley, thank for suggestion,but this solution works. – raven_richard Jul 30 '17 at 05:30
1

Im not going to do your homework for you ... but ill give you a hint

def max_val(t): 
    """ t, tuple or list
        Each element of t is either an int, a tuple, or a list
        No tuple or list is empty
        Returns the maximum int in t or (recursively) in an element of t """ 
    current_max = float("-inf")
    for item in t:
        if isinstance(item,(list,tuple)):
            #do one thing here
        else:
           #do a different thing here
    return current_max
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • problem is,I don't get how to find a max for either a tuple or a list. meaning,if I try and find a max in a tuple, tuple has max value,what if list nested in it has higher value? – raven_richard Jul 29 '17 at 18:21
  • maybe where it says do one thing here ... try printing your "item" and in the other case also think about what you need to do... – Joran Beasley Jul 29 '17 at 22:59
  • +1 for a very short and neat template. @raven_richard I strongly recommend you to try implementing a solution based on this template too - it could be more efficient than mine and you will learn a great deal in solving the same problem in multiple way. – Atlas7 Jul 30 '17 at 10:12
0
def max_val(t):
   weird_list = t
   weird_string = str(weird_list)
   weird_string = weird_string.replace('[','').replace(']','').replace(' ','').replace('(','').replace(')','')
   nice_list = list(map(int, weird_string.split(',')))
   max_number = max(nice_list)
   return max_number

One approach is to convert the tuple to a string first.

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
0

The main idea about this problem is think recursively:

def max_val(t): 
    """ t, tuple 
        Each element of t is either an int, a tuple, or a list
        No tuple or list is empty
        Returns the maximum int in t or (recursively) in an element of t """ 
    # Your code here
    result = 0
    for e in t:
        try:
            if e > result:
                result = e
        except:
            if max_val(e) > result:
                result = max_val(e)
    return result

print(max_val((5, (1, 2), [[1], [2]])))
print(max_val((5, (1, 2), [[1], [9]])))

will return 5 and 9 respectively.

Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
Manu
  • 112
  • 4