diff options
| -rw-r--r-- | challenge-115/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-115/adam-russell/perl/ch-1.pl | 66 | ||||
| -rw-r--r-- | challenge-115/adam-russell/perl/ch-2.pl | 34 |
3 files changed, 101 insertions, 0 deletions
diff --git a/challenge-115/adam-russell/blog.txt b/challenge-115/adam-russell/blog.txt new file mode 100644 index 0000000000..f94d671780 --- /dev/null +++ b/challenge-115/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/06/05 diff --git a/challenge-115/adam-russell/perl/ch-1.pl b/challenge-115/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..70d7a90af1 --- /dev/null +++ b/challenge-115/adam-russell/perl/ch-1.pl @@ -0,0 +1,66 @@ +use strict; +use warnings; +## +# You are given an array of strings. +# Write a script to find out if the given strings can be chained to form a circle. +# Print 1 if found otherwise 0. +# A string $S can be put before another string $T in circle if the last character +# of $S is same as first character of $T. +## +use Graph; +use Graph::Easy::Parser; + +sub build_graph{ + my @words; + my %first_letter_name; + my $graph = new Graph(); + while(my $s = <DATA>){ + chomp($s); + my $first_letter = substr($s, 0, 1); + if($first_letter_name{$first_letter}){ + push @{$first_letter_name{$first_letter}}, $s; + } + else{ + $first_letter_name{$first_letter} = [$s]; + } + push @words, $s; + } + for my $word (@words){ + $graph->add_vertex($word) if !$graph->has_vertex($word); + my $child_nodes = $first_letter_name{substr($word, -1)}; + for my $n (@{$child_nodes}){ + $graph->add_vertex($n) if !$graph->has_vertex($n); + $graph->add_weighted_edge($word, $n, (-1 * length($n))) if !$graph->has_edge($word, $n); + $graph->delete_edge($word, $n) if $graph->has_a_cycle(); + } + } + return $graph; +} + +sub display_graph{ + my($graph) = @_; + my $s = $graph->stringify(); + my @s = split(/,/, $s); + my @lines; + for my $n (@s){ + my @a = split(/-/, $n); + push @lines, "[ $a[0] ] => [ ]" if @a == 1; + push @lines, "[ $a[0] ] => [ $a[1] ]" if @a > 1; + } + my $parser = new Graph::Easy::Parser(); + my $graph_viz = $parser->from_text(join("", @lines)); + print $graph_viz->as_ascii(); +} + +MAIN:{ + my $graph = build_graph(); + my @cc = $graph->weakly_connected_components(); + print "1\n" if @cc == 1; + print "0\n" if @cc != 1; + display_graph($graph); +} + +__DATA__ +abc +dea +cd diff --git a/challenge-115/adam-russell/perl/ch-2.pl b/challenge-115/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..ec3606df68 --- /dev/null +++ b/challenge-115/adam-russell/perl/ch-2.pl @@ -0,0 +1,34 @@ +use strict; +use warnings; +## +# You are given a list of positive integers (0-9), single digit. +# Write a script to find the largest multiple of 2 that can be formed from the list. +## +sub largest_multiple_2{ + my @numbers = @_; + return unless grep { $_ % 2 == 0 } @numbers; + my @sorted = sort {$b <=> $a} @numbers; + if(@sorted >= 2){ + my $ultima = @sorted[@sorted - 1]; + if($ultima % 2 != 0){ + my $swap_index = -1; + for(my $i = @sorted - 2; $i >= 0; $i--){ + $swap_index = $i if $sorted[$i] % 2 == 0; + last if $swap_index > 0; + } + $sorted[@sorted - 1] = $sorted[$swap_index]; + $sorted[$swap_index] = $ultima; + } + } + return join("", @sorted); +} + +MAIN:{ + my @N; + @N = (1, 0, 2, 6); + print largest_multiple_2(@N) . "\n"; + @N = (1, 4, 2, 8); + print largest_multiple_2(@N) . "\n"; + @N = (4, 1, 7, 6); + print largest_multiple_2(@N) . "\n"; +} |
