Perl: the Markov chain saw | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
At work, I needed to fix logging of changes in a tree structure. The language was unfortunately Java, but I started with a Perl prototype to test the approaches and border cases. The Java solution was more challenging for me, but the Perl implementation was still interesting enough to share. For those bored or interested, I present the problem below. The original domain was different, but I'm probably not allowed to speak about it outside of the firm.
We'll use the famous animal tree:
All the leaves are on the same level. Let's have a module Zoo.pm which implements the following methods: ParentStatic method. 'Zoo'->Parent($node) returns the parent of the given node in the tree, e.g. 'Zoo'->Parent('Owl') is 'Bird', 'Zoo'->Parent('Bird') is 'Reptile'. newThe constructor. Use a list of tree leaves as arguments:
get_leavesFor a given object, returns the list it was constructed from (order is not guaranteed).
The TaskWhen comparing too different zoos, we aren't interested in the animals only, but in their categories, too. Our task is to implement a static method 'Zoo'->diff($zoo1, $zoo2) which returns two array references, $add and $delete, such that:
For example,
should return
Similarly,
should return
Updates: Please, use <readmore> tags for the code. Use only the documented API in the solution (i.e. Parent and get_leaves). The tree structure is not accessible directly (in reality, it was stored in a database).
In reply to Tree Structure Challenge by choroba
|
|