Created
March 13, 2012 08:25
-
-
Save moritz/2027585 to your computer and use it in GitHub Desktop.
Tree depth in Perl 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
# inspired by http://www.perlmonks.org/?node_id=959260 | |
my $graphspec = '1 -> 2 2 -> 0 3 -> 4 4 -> 2 5 -> 10 6 -> 10 7 -> 9 8 -> 9 | |
9 -> 6 10 -> 4 11 -> 10 12 -> 10 13 -> 12 14 -> 10 15 -> 2'; | |
my %graph; | |
for $graphspec.match(:g, rx:s/(\d+) '->' (\d+)/) -> $/ { | |
%graph{$0} = +$1; | |
} | |
sub depth($source) { | |
($source, { %graph{$_} } ... 0) - 1; | |
} | |
say max %graph.keys.map: &depth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment