-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Organising information: trees", exercise 1 #31
Comments
I feed the recursive function with the items of a list compiled by the function itself.
|
I tried my best to solve this exercise, but I keep getting recursion errors that should not happen and I don't understand why. This is what I came up with.
|
Both tests return True.
|
My function run as a depth first-visit function (development exercise) and order all the nodes by the len of their ancestors: from the shorter node (root node) to the longest ones.
|
It took me so much time that when it finally returned True I couldn't believe it, and ran a thousand additional tests. I pasted just the first one because trees take up quite a lot of space
|
I think it works but I got a bit lost on it... |
I'm confused about something @essepuntato - in your exercise description you say that the resulting list for the nodes from Listing 2 should be [book, chapter_1, chapter_2, text_8, paragraph_1, paragraph_2, paragraph_3, text_7, text_1, quotation_1, text_3, quotation_2, text_5, text_6, text_2, text_4]. But when I use the solution you gave and print the function (not the test) I get [Node('/book'), Node('/book/chapter1'), Node('/book/chapter2'), Node('/book/text8'), Node('/book/chapter1/paragraph1'), Node('/book/chapter1/paragraph2'), Node('/book/chapter1/paragraph3'), Node('/book/chapter2/text7'), Node('/book/chapter1/paragraph1/text1'), Node('/book/chapter1/paragraph1/quotation1'), Node('/book/chapter1/paragraph1/text3'), Node('/book/chapter1/paragraph1/quotation2'), Node('/book/chapter1/paragraph2/text5'), Node('/book/chapter1/paragraph3/text6'), Node('/book/chapter1/paragraph1/quotation1/text2'), Node('/book/chapter1/paragraph1/quotation2/text4')] What's confusing me is that based on how you wrote the exercise description I was expecting that running the function should print the list exactly how you have it in the description. But if I understand correctly that's not possible and instead it will always print with Node('node_name'), but those node values are equivalent to their titles (hence why it matches the bfv value you use in your solution)? I hope that makes sense? I've spent the past two weeks trying to understand how to get python to print the node's value name only because the way you formatted the description totally threw me off. lmk thanks |
actually I also just realized that the elements of Listing 2 needed renaming, i've been using them as they are in the tree_instructions.py file and that has totally thrown me off as well. |
I finally succeeded! I do not deny that I also viewed the solutions shared by others (especially the code written by Constance), but they helped me so much to get the sense. I know that it is missing the test, but I'll complete it in the next days. For now, I'm quite satisfied :')
|
Write in Python a recursive function
def breadth_first_visit(root_node)
. This function takes the root node of a tree and returns a list containing all the nodes of the tree according to a breadth-first order. The breadth-first order considers all the nodes of the first level, then those ones of the second level, and so forth. For instance, considering the nodes created in Listing 2, the function called on the node book should return the following list:[book, chapter_1, chapter_2, text_8, paragraph_1, paragraph_2, paragraph_3, text_7, text_1, quotation_1, text_3, quotation_2, text_5, text_6, text_2, text_4]
. Accompany the implementation of the function with the appropriate test cases.The text was updated successfully, but these errors were encountered: