@namark "Loosey-Goosey" How so? I can certainly try to tighten it up some.
@billstclair @namark aiming for simple or elegant. Would be happy with "pythonical" :)
@billstclair @namark Oh, yeah, I know my values can't contain ',' characters. I may redo it with some other control or npc
Okay, I figured it out. I am open to critique on this version:
@Absinthe got a new challenge soon, been a bit busy but ready for the next!
@billstclair @namark @khird as it is the "value" was a string since it was indicative of where it was. So "root", "left" "left.left" and so forth. But left and right are Nodes as created by the Node class in the example
@billstclair @namark @khird The leaf nodes would simply have a value, and left and right would be None
@namark @billstclair @khird Tree serialization seems to be one of those things that is generally easy to do with recursion. And so I did it. However, with the thoughts of deserialization, I figured I could serialize it to it's own deserialization command and run eval. Both pylint and the whole of the boaty mcBoatface crowd agree that eval is almost never the right answer for anything :) So I figure I would like to have a "parallel" deserialization, but somehow I have to be able to traverse something recursively while maintaining state. Or at least keep my place through multiple calls. :)
#toyprogrammingchallenge
#Python @billstclair @namark @khird
I think there should be a nice recursive way to deserialize it similar to the way it gets serialized. I am thinking some way to have a deserializer that returns a Node from a string, but I need to do something like Node(value from serialized string), deserialize(for the left node), deserialize(for the right node)) ...
I don't know how to partially consume the string to get the left node, and then take up where it left off for the right node. I was thinking maybe using an index but that would have to be similar to a C static variable. or globalized somehow so that it maintained state. I am missing a python paradigm somewhere. :)
@Absinthe I have hardly any experience with Python but I think you ought to write your own parser that reports an error on input that can't represent an actual tree (this is what I did in my solution, anyway). The other issue is making sure you escape any special characters in Node.val so your parser doesn't treat them as control characters.
@Ghosty valgrind I your friend :)
Unsafe code behaviour
@khird kind of figured there were good reasons not to use eval, but what is an appropriate alternative?
@alex It is a matter of consistency. And though I do hate to be "that person" I do make a point of informing cheese eating vegetarians about rennet.
As a beekeeper, I do feel the need to share my knowledge with vegans. If you concern is the subjugation or enslavement of a species regardless of mistreatment or harm or mutual benefit, then to be right, you should probably restrict your diet to grains and leafy greens avoiding most fruit bearing choices such as melons, cucumbers, tree fruits and tree nuts, and vine fruits. Not to mention cotton and canola. These products are pollinated by bees, and those bees are placed in those environment by beekeepers with managed hives for pay.
Of course, you might also reconsider many of the grains and green leafy vegetables as well unless they are organically farmed, since billions of insects are indiscriminately murdered with chemical warfare.
@alex what bees go through for the pollination of almonds is far more harmful than a little harvest of surplus honey.
Though I am very polite with my girls, and they willingly share extra honey with me when I ask for it. I do not take any if they don't have enough stored to get through the winter. We have an agreement. :)
@alex I trust you don't eat almonds or other almond products either
@Absinthe ancient abbreviations? and I was hoping they were some cool french words...
@namark well the internet knows all :)
https://www.gnu.org/software/emacs/manual/html_node/eintr/Strange-Names.html#Strange-Names
Here's a python solution
Need a better solution for deserialization than eval()
#toyprogrammingchallenge
#python Here's another freebie, I assume it is python specific because they start with a base of python code. But if it makes sense, try it in whatever language you like:
This problem was asked by Google.
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
For example, given the following Node class
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
The following test should pass:
node = Node('root', Node('left', Node('left.left')), Node('right'))
assert deserialize(serialize(node)).left.left.val == 'left.left'
The green faerie