diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-18 11:54:30 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-18 11:54:30 +0100 |
| commit | 78fe2f72354f77a833346bcf8571780c6fd2be5e (patch) | |
| tree | 7e12b9201969ab64dc6a11ccab7acbf13b813077 /challenge-061 | |
| parent | 20b2caef18b138191e9c9263a35abfd6fc2b9249 (diff) | |
| download | perlweeklychallenge-club-78fe2f72354f77a833346bcf8571780c6fd2be5e.tar.gz perlweeklychallenge-club-78fe2f72354f77a833346bcf8571780c6fd2be5e.tar.bz2 perlweeklychallenge-club-78fe2f72354f77a833346bcf8571780c6fd2be5e.zip | |
- Added solutions by Javier Luque.
Diffstat (limited to 'challenge-061')
| -rw-r--r-- | challenge-061/javier-luque/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-061/javier-luque/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-061/javier-luque/perl/ch-2.pl | 59 | ||||
| -rw-r--r-- | challenge-061/javier-luque/raku/ch-1.p6 | 42 | ||||
| -rw-r--r-- | challenge-061/javier-luque/raku/ch-2.p6 | 49 |
5 files changed, 197 insertions, 0 deletions
diff --git a/challenge-061/javier-luque/blog.txt b/challenge-061/javier-luque/blog.txt new file mode 100644 index 0000000000..af053ded18 --- /dev/null +++ b/challenge-061/javier-luque/blog.txt @@ -0,0 +1 @@ +https://perlchallenges.wordpress.com/2020/05/18/perl-weekly-challenge-061/ diff --git a/challenge-061/javier-luque/perl/ch-1.pl b/challenge-061/javier-luque/perl/ch-1.pl new file mode 100644 index 0000000000..f2f77436bf --- /dev/null +++ b/challenge-061/javier-luque/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl +# Test: ./ch-1.pl 30 +use strict; +use warnings; +use feature qw /say/; + +say max_product([ 2, 5, -1, 3 ]); + +sub max_product { + my $list = shift; + + # Index of the array that will return + # the max product + my $left = 0; + my $right = scalar(@$list); + + # Max product + my $max_product = 1; + + # Loop through the list + for (my $i = 0; $i < scalar(@$list); $i++ ) { + my $j = $i; + + # Temp variable + my $current_product = 1; + + # Loop through the list another time + while ($j < scalar(@$list)) { + $current_product = + $current_product * $list->[$j]; + + if ($current_product >= $max_product) { + $max_product = $current_product; + $left = $i; + $right = $j; + } + + $j++; + } + } + + return '[' . + (join ', ', @{$list}[$left .. $right]) . + ']' . ' which gives a maximum product of ' . + $max_product; +} diff --git a/challenge-061/javier-luque/perl/ch-2.pl b/challenge-061/javier-luque/perl/ch-2.pl new file mode 100644 index 0000000000..4e6f94c681 --- /dev/null +++ b/challenge-061/javier-luque/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +# Test: ./ch-2.pl +use strict; +use warnings; +use feature qw /say/; +use Algorithm::Combinatorics qw(combinations); + +my $ip_address = $ARGV[0] || '25525511135'; +partition_ip_string($ip_address); + +# Partition the ip string +sub partition_ip_string { + my $string = shift; + my $length = length($string) - 2; + + # Validate string + return undef if + ( length($string) < 4 || + length($string) > 12 || + !($string =~ /^\d+$/) + ); + + # Find all the combinations for the possible ips + my @dot_positions = (0 .. $length); + my $iter = combinations(\@dot_positions, 3); + + # Process the combinations + while (my $combos = $iter->next) { + my $to_test = $string; + my $offset = 0; + + for my $dot_position (@$combos) { + my $position = + ($dot_position + $offset) + 1; + + # Append to the test string; + $to_test = + (substr $to_test, 0, $position) + . '.' . + (substr $to_test, $position); + + # Offset the string + $offset++; + } + + say $to_test + if (validate_ip_string($to_test)); + } +} + +# Validate the IP String +sub validate_ip_string { + for my $digit (split('\.', shift)) { + return 0 if ($digit > 255); + return 0 if ($digit =~ /^0\d+$/); + } + + return 1; +} diff --git a/challenge-061/javier-luque/raku/ch-1.p6 b/challenge-061/javier-luque/raku/ch-1.p6 new file mode 100644 index 0000000000..000a8b6140 --- /dev/null +++ b/challenge-061/javier-luque/raku/ch-1.p6 @@ -0,0 +1,42 @@ +# Test: perl6 ch-1.p6 +sub MAIN() { + say max-product([ 2, 5, -1, 3 ]); +} + +sub max-product(@list) { + + # Index of the array that will return + # the max product + my $left = 0; + my $right = @list.elems; + + # Max product + my $max_product = 1; + + # Loop through the list + for ^@list -> $i { + my $j = $i; + + # Temp variable + my $current_product = 1; + + # Loop through the list another time + while ($j < @list.elems) { + $current_product = + $current_product * @list[$j]; + + if ($current_product >= $max_product) { + $max_product = $current_product; + $left = $i; + $right = $j; + } + + $j++; + } + } + + return '[' ~ + @list[$left .. $right].join(', ') ~ + ']' ~ ' which gives a maximum product of ' ~ + $max_product;; +} diff --git a/challenge-061/javier-luque/raku/ch-2.p6 b/challenge-061/javier-luque/raku/ch-2.p6 new file mode 100644 index 0000000000..0d06952652 --- /dev/null +++ b/challenge-061/javier-luque/raku/ch-2.p6 @@ -0,0 +1,49 @@ +# Test: perl6 ch-2.p6 +multi MAIN() { + MAIN('25525511135'); +} + +multi MAIN(Str $str) { + partition-ip-string($str); +} + +# Partition the ip string +sub partition-ip-string (Str $str) { + my $length = $str.chars - 2; + + # Find all the combinations for the possible ips + my @dot_positions = (0 .. $length); + my @combos = @dot_positions.combinations: 3; + + # Process the combinations + for @combos -> @combo { + my $to_test = $str; + my $offset = 0; + + for (@combo) -> $dot_position { + my $position = + ($dot_position + $offset) + 1; + + # Append to the test string; + $to_test = + $to_test.substr(0, $position) + ~ '.' ~ + $to_test.substr($position); + + # Offset the string + $offset++; + } + + say $to_test + if (validate-ip-string($to_test)); + } +} + +# Validate the IP String +sub validate-ip-string(Str $str) { + for $str.split('.') -> $digit { + return False if ($digit > 255); + return False if ($digit ~~ /^0\d+$/); + } + return True; +} |
