Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7934777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:44:06+00:00 2026-06-03T21:44:06+00:00

I am working through How to Think Like a Computer Scientist, and am currently

  • 0

I am working through How to Think Like a Computer Scientist, and am currently trying to get a grasp on recursion. I am having trouble on one of the problems, so I am hoping you can help me out.

I am writing a function which finds the recursive minimum of a list, whose elements are integers, lists of integers, lists of lists of integers, etc.

Here is my current version:

def recursive_min(nested_num_list):
    """
      >>> recursive_min([9, [1, 13], 2, 8, 6])
      1
      >>> recursive_min([2, [[100, 1], 90], [10, 13], 8, 6])
      1
      >>> recursive_min([2, [[13, -7], 90], [1, 100], 8, 6])
      -7
      >>> recursive_min([[[-13, 7], 90], 2, [1, 100], 8, 6])
      -13
    """
    min = nested_num_list[0]
    while type(min) == type([]):
        min = min[0]
    for item in nested_num_list:
        if type(item) == type([]):
            recursive_min(item)
        elif item < min:
            min = item
    return min

However, this only works to find the minimum at the top level, so I now my code is not getting in to the depth of the list.

Now, looking at the answers, I know my version should read something like:

def recursive_min(nested_num_list):
    """
      >>> recursive_min([9, [1, 13], 2, 8, 6])
      1
      >>> recursive_min([2, [[100, 1], 90], [10, 13], 8, 6])
      1
      >>> recursive_min([2, [[13, -7], 90], [1, 100], 8, 6])
      -7
      >>> recursive_min([[[-13, 7], 90], 2, [1, 100], 8, 6])
      -13
    """
    min = nested_num_list[0]
    while type(min) == type([]):
        min = min[0]
    for item in nested_num_list:
        if type(item) == type([]):
            min_of_elm = recursive_min(item)
            if min_of_elm < min:
                min = min_of_elm
        elif item < min:
            min = item
    return min

Notice that instead of just executing recursive_min(item), it assigns it to a variable, and compares it to the current min. I don’t see why I need to do that, as in my mind, if I execute the entire function over the embedded list, and that is a list of integers, it should then get to the elif statement (rather than the if statement) and properly compare the values.

I know I must be missing something here about recursion. I would really appreciate any insight you might be able to give me to help me understand what makes the second version work but the first version fail.

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:44:08+00:00Added an answer on June 3, 2026 at 9:44 pm

    Why would it work? You call the function, and it recurses until it finds the min. Then what does it do? It exits back out of all the recursions in turn, until it gets back to the top level. At that point, what’s the value of min? Exactly what it was before you started recursing, because you never captured the return value from the recursive function.

    Don’t forget that each call of the function has its own local variables, therefore its own local value of min.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working through Allen Downey's How To Think Like A Computer Scientist , and
I've been working through a recent Computer Science homework involving recursion and big-O notation.
Still working through JQuery to get data and repopulate portions of a page. Right
I am working through trying to learn to program in Python and am focused
I am currently working through this tutorial: Getting Started with jQuery For the two
i'm working through SICP - one exercise is to implement foreach (doseq). This is
I'm working on a social network, like a subset of Facebook. I think this
In working through the issues with another question, I've found text files with embedded
I'm working through Aaron Hillegass' book, specifically the lottery example. I had a question
I've recently been working through a large codebase, refactoring and generally improving design to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.