Depth of nested list
|
09-08-2023, 11:08 PM
Post: #1
|
|||
|
|||
Depth of nested list
Is there a way to programmatically find how deeply nested a list is?
I'm looking for syntax like : { 1 { 2 { 3 { 4 }5 } 6 } 7 } NEST --> 4 Failing a command to do the trick, I thought of stringifying the list and counting the left braces. Is there a better way? Any suggestions are welcome. Charles? He was my grandfather. --Oh, *that* Charles. We share a common ancestor. |
|||
09-09-2023, 12:07 AM
Post: #2
|
|||
|
|||
RE: Depth of nested list
Are you seeking an RPL solution, or something for the HP Prime?
<0|ɸ|0> -Joe- |
|||
09-09-2023, 10:34 AM
Post: #3
|
|||
|
|||
RE: Depth of nested list | |||
09-09-2023, 11:33 AM
(This post was last modified: 09-09-2023 12:01 PM by DavidM.)
Post: #4
|
|||
|
|||
RE: Depth of nested list
(09-09-2023 10:34 AM)John Keith Wrote: On the 50g I would convert to string and use SREPL to count the braces. Counting the brackets would work in the given example, but not with others. Example: { { 1 } { 2 } { 3 } { 4 } { 5 } { 6 } { 7 } }, which I believe should have a nested depth of only 2. So there's a need to track the current depth as the opening and closing brackets are encountered when parsing. Then there's situations like { "{{{}}{{" }. While it's probably overkill for the specific need here, it's the type of thing that I think about when coding System RPL. Unanticipated input in that environment can have serious consequences. |
|||
09-09-2023, 11:59 AM
Post: #5
|
|||
|
|||
RE: Depth of nested list
depth(non-list) = 0
depth(lst) = 1 + max(map(depth, lst)) Code: function depth(t) lua> depth({ "{{{ Hello!", 1, {2, 3} , {4, {5}}}) 3 |
|||
09-09-2023, 03:00 PM
Post: #6
|
|||
|
|||
RE: Depth of nested list
(09-09-2023 11:33 AM)DavidM Wrote:(09-09-2023 10:34 AM)John Keith Wrote: On the 50g I would convert to string and use SREPL to count the braces. Oh yeah, duh! That's what happens when I post before coffee. |
|||
09-09-2023, 04:15 PM
Post: #7
|
|||
|
|||
RE: Depth of nested list
(09-09-2023 03:00 PM)John Keith Wrote: Oh yeah, duh! That's what happens when I post before coffee. Perfectly understandable. I think I more often respond with these kind of half-thought-out ideas for algorithms than any kind of bulletproof ideas. But I think we should encourage them, because it’s fertile ground for learning. You frequently spot good uses for the speedy SREPL, and sometimes I’m surprised at your creative ideas where it can be applied. I could be mistaken, but from previous posts I believe rickster is probably looking for a 48gx solution here, so SREPL may not be an option regardless. Perhaps he can specify which platform he’s targeting here. |
|||
09-09-2023, 05:22 PM
Post: #8
|
|||
|
|||
RE: Depth of nested list
A couple of vague ideas here:
Convert to string, initialize a counter, loop through the string and increment the counter when a left brace is encountered, and decrement when a right brace is encountered. The maximum value is the depth of nesting. Would be slow for large lists. Unpack the list, check the type of each item, if a list is encountered, unpack it and so on, keeping track of max. depth. Probably faster since one rarely encounters lists with nesting more than three levels deep. |
|||
09-11-2023, 12:55 PM
Post: #9
|
|||
|
|||
RE: Depth of nested list
(09-09-2023 05:22 PM)John Keith Wrote: A couple of vague ideas here: My first thought was the same as your first suggestion above. Then I (perhaps foolishly) wanted to see what it would take to use the second approach instead, as I thought it might be an opportunity to try a recursive implementation. The following visits each object in the list given as an argument, and tracks the depth as it dives into embedded lists. After visiting each embedded object, it simply outputs the deepest level it encountered: Code: \<< I have to say that this ended up being one of the ugliest RPL programs I've created in a while. I promise that was not intentional -- it just ended up being that way as I tried to save a few bytes here and there. Using IFTE instead of the traditional IF ... THEN ... ELSE ... END can definitely save a few bytes, especially when combined with the use of lists and null-tagged objects instead of executable objects. But all of that comes at a price, both in terms of readability and the quirkiness of using identifiers in lists. Unfortunately the compiled locals make it look even more cluttered. But it works as intended! |
|||
09-11-2023, 03:28 PM
Post: #10
|
|||
|
|||
RE: Depth of nested list
(09-11-2023 12:55 PM)DavidM Wrote: And without even trying, I proved what I told John above about half-thought-out program ideas. The program I posted fails miserably if the source list is either empty or contains an embedded list which is empty (the code snippet above shows why). A simple way to fix that is to simply make sure that each list has at least one value, since the quantity and type of non-list items doesn't really matter. A fairly simple fix (which unfortunately contributes to the program's ugliness) is to just add something to each list encountered. I do this below by adding NOVAL to each encountered list: Code: \<< I dislike this kind of patch, and in practice I would likely come up with a better solution for something like this. For now, it will suffice. |
|||
09-11-2023, 04:05 PM
(This post was last modified: 09-11-2023 04:32 PM by Gilles.)
Post: #11
|
|||
|
|||
RE: Depth of nested list
A solution in "RPL + ListExt library" or "newRPL + LstX library"
Code: « 1 → n The idea is very simple : 'explode' inner list (LXIL) while something changes. If you dont want to install the library, LXIL is : Code: 'LXIL' « 1 « IF DUP TYPE 62 == THEN LIST→ DROP END » DOSUBS » |
|||
09-11-2023, 05:46 PM
Post: #12
|
|||
|
|||
RE: Depth of nested list
a recursive solution. store as NEST
NEST \<< 0. + @ avoid empty lists 0. SWAP @ set up count 1. \<< IF DUP TYPE 5. SAME THEN NEST ELSE DROP 0. END MAX \>> DOSUBS 1. + \>> Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)