[TriPython] A question of recursion and nested parsing

Bob Gailer bgailer at gmail.com
Fri Sep 15 08:47:58 EDT 2017


On Sep 14, 2017 9:33 AM, "Ken MacKenzie" <ken at mack-z.com> wrote:
>
>    So I have a bit of code I have created that works to solve the problem,
>    however as an academic exercise, I realized I could not reason of a
way to
>    solve it without recursion.

My understanding of computer science says that recursion is never
necessary. There is always a way to solve the problem without recursion.
>    The situation, I have a file with many possible delimiters and those
>    delimiters have levels so to speak so as I am kind of parsing a tree,
and
>    in this case a tree that is lists of lists of lists of x n...
>    Excuse the lack of real commenting in this sample.** But curious what
>    other ways I could solve it, or if there are ways I could perhaps make
it
>    perform faster.** It performs fast enough but I realize the file could
get
>    quite long.
It would be very helpful if you would give us a sample input and the
corresponding output. Your description above does not help.
Also your code has lots of** in it that I presume are either spaces or tabs.
I would appreciate it if you would post the code that is actually
executable.
My guess is that there's a much simpler way to do this.
>    =======
>    parse_1 = ["\n", "\x00", "\x01", "\x02", "\x03", "\x04", "\x05",
"\x06",
>    "\x07"]
>    parse_2 = [chr(255), chr(254), chr(253), chr(252)]
>    all_parsers = parse_1 + parse_2
>    def recursive_parse(in_var, parse_char):
>    ** ** if isinstance(in_var, list):
>    ** ** ** ** return [recursive_parse(x, parse_char) for x in in_var]
>    ** ** elif isinstance(in_var, str):
>    ** ** ** ** return in_var.split(parse_char)
>    def nested_parse(str_in):
>    ** ** big_list = []
>    ** ** temp = []
>    ** ** parse_list = all_parsers
>    ** ** max_lvl = len(parse_list) - 1
>    ** ** for lvl, psr_c in enumerate(parse_list):
>    ** ** ** ** # print("temp:", temp[0][:20])
>    ** ** ** ** if lvl == 0:
>    ** ** ** ** ** ** temp.append(str_in.split(psr_c))
>    ** ** ** ** else:
>    ** ** ** ** ** ** pre_lvl = lvl - 1
>    ** ** ** ** ** ** new_lvl = recursive_parse(temp[pre_lvl], psr_c)
>    ** ** ** ** ** ** temp.append(new_lvl)
>    ** ** big_list = temp[max_lvl]
>    ** ** return big_list
>
> _______________________________________________
> TriZPUG mailing list
> TriZPUG at python.org
> https://mail.python.org/mailman/listinfo/trizpug
> http://tripython.org is the Triangle Python Users Group
>
-------------- next part --------------
   On Sep 14, 2017 9:33 AM, "Ken MacKenzie" <[1]ken at mack-z.com> wrote:
   >
   > ** **So I have a bit of code I have created that works to solve the
   problem,
   > ** **however as an academic exercise, I realized I could not reason of a
   way to
   > ** **solve it without recursion.

   My understanding of computer science says that recursion is never
   necessary. There is always a way to solve the problem without recursion.
   > ** **The situation, I have a file with many possible delimiters and
   those
   > ** **delimiters have levels so to speak so as I am kind of parsing a
   tree, and
   > ** **in this case a tree that is lists of lists of lists of x n...
   > ** **Excuse the lack of real commenting in this sample.** But curious
   what
   > ** **other ways I could solve it, or if there are ways I could perhaps
   make it
   > ** **perform faster.** It performs fast enough but I realize the file
   could get
   > ** **quite long.
   It would be very helpful if you would give us a sample input and the
   corresponding output. Your description above does not help.
   Also your code has lots of** in it that I presume are either spaces or
   tabs.
   I would appreciate it if you would post the code that is actually
   executable.
   My guess is that there's a much simpler way to do this.
   > ** **=======
   > ** **parse_1 = ["\n", "\x00", "\x01", "\x02", "\x03", "\x04", "\x05",
   "\x06",
   > ** **"\x07"]
   > ** **parse_2 = [chr(255), chr(254), chr(253), chr(252)]
   > ** **all_parsers = parse_1 + parse_2
   > ** **def recursive_parse(in_var, parse_char):
   > ** **** ** if isinstance(in_var, list):
   > ** **** ** ** ** return [recursive_parse(x, parse_char) for x in in_var]
   > ** **** ** elif isinstance(in_var, str):
   > ** **** ** ** ** return in_var.split(parse_char)
   > ** **def nested_parse(str_in):
   > ** **** ** big_list = []
   > ** **** ** temp = []
   > ** **** ** parse_list = all_parsers
   > ** **** ** max_lvl = len(parse_list) - 1
   > ** **** ** for lvl, psr_c in enumerate(parse_list):
   > ** **** ** ** ** # print("temp:", temp[0][:20])
   > ** **** ** ** ** if lvl == 0:
   > ** **** ** ** ** ** ** temp.append(str_in.split(psr_c))
   > ** **** ** ** ** else:
   > ** **** ** ** ** ** ** pre_lvl = lvl - 1
   > ** **** ** ** ** ** ** new_lvl = recursive_parse(temp[pre_lvl], psr_c)
   > ** **** ** ** ** ** ** temp.append(new_lvl)
   > ** **** ** big_list = temp[max_lvl]
   > ** **** ** return big_list
   >
   > _______________________________________________
   > TriZPUG mailing list
   > [2]TriZPUG at python.org
   > [3]https://mail.python.org/mailman/listinfo/trizpug
   > [4]http://tripython.org is the Triangle Python Users Group
   >

References

   Visible links
   1. mailto:ken at mack-z.com
   2. mailto:TriZPUG at python.org
   3. https://mail.python.org/mailman/listinfo/trizpug
   4. http://tripython.org/


More information about the TriZPUG mailing list