Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursive functions
#1
Greetings,
I've been doing this course on Udacity and this one problem has been stressing me for a while and I don't know why this keeps coming back to me and I can't really get a idea of it due to the fact that I find recursive functions super confusing and complicated.

I would like to find the solution by myself but I need some help to how this works and why it doesn't output in my desired manner. Thank you.

    # Single Gold Star
    
    # Family Trees
    
    # In the lecture, we showed a recursive definition for your ancestors. For this
    # question, your goal is to define a procedure that finds someone's ancestors,
    # given a Dictionary that provides the parent relationships.
    
    # Here's an example of an input Dictionary:
    
    ada_family = { 'Judith Blunt-Lytton': ['Anne Isabella Blunt', 'Wilfrid Scawen Blunt'],
                  'Ada King-Milbanke': ['Ralph King-Milbanke', 'Fanny Heriot'],
                  'Ralph King-Milbanke': ['Augusta Ada King', 'William King-Noel'],
                  'Anne Isabella Blunt': ['Augusta Ada King', 'William King-Noel'],
                  'Byron King-Noel': ['Augusta Ada King', 'William King-Noel'],
                  'Augusta Ada King': ['Anne Isabella Milbanke', 'George Gordon Byron'],
                  'George Gordon Byron': ['Catherine Gordon', 'Captain John Byron'],
                  'John Byron': ['Vice-Admiral John Byron', 'Sophia Trevannion'] }
    
    # Define a procedure, ancestors(genealogy, person), that takes as its first input
    # a Dictionary in the form given above, and as its second input the name of a
    # person. It should return a list giving all the known ancestors of the input
    # person (this should be the empty list if there are none). The order of the list
    # does not matter and duplicates will be ignored.

    output = []
    def ancestors(genealogy, person):
        if person in genealogy:
            for candidate in genealogy[person]:
                output.append(candidate)
                ancestors(genealogy, candidate)
            return output
        else:
            return []
    
    
    
    
    # Here are some examples:
    
    print (ancestors(ada_family, 'Augusta Ada King'))
    #>>> ['Anne Isabella Milbanke', 'George Gordon Byron',
    #    'Catherine Gordon','Captain John Byron']
    
    print (ancestors(ada_family, 'Judith Blunt-Lytton'))
    #>>> ['Anne Isabella Blunt', 'Wilfrid Scawen Blunt', 'Augusta Ada King',
    #    'William King-Noel', 'Anne Isabella Milbanke', 'George Gordon Byron',
    #    'Catherine Gordon', 'Captain John Byron']
    
    print (ancestors(ada_family, 'Dave'))
    #>>> []
Reply


Messages In This Thread
Recursive functions - by Ayman_2001 - Jun-27-2020, 10:37 AM
RE: Recursive functions - by Gribouillis - Jun-27-2020, 12:02 PM
RE: Recursive functions - by Ayman_2001 - Jun-27-2020, 01:17 PM
RE: Recursive functions - by ndc85430 - Jun-27-2020, 03:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python recursive functions Ayman_2001 6 2,632 Jun-24-2020, 07:54 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020