diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-11 04:02:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-11 04:02:57 +0100 |
| commit | 95dc33ff238e2ba288a765870ce72002352b598b (patch) | |
| tree | d1db61eddeadb9eef4d04f14dc4dcc6b29691259 | |
| parent | 7ce2dbe9d1443072cd2bb2d86277148a11df5d6c (diff) | |
| parent | 00ea02d514df26af213f24a79f50d940c547a7a4 (diff) | |
| download | perlweeklychallenge-club-95dc33ff238e2ba288a765870ce72002352b598b.tar.gz perlweeklychallenge-club-95dc33ff238e2ba288a765870ce72002352b598b.tar.bz2 perlweeklychallenge-club-95dc33ff238e2ba288a765870ce72002352b598b.zip | |
Merge pull request #2249 from nunovieira220/challenge-077
Add solution to challenge 077
| -rw-r--r-- | challenge-077/nunovieira220/perl/ch-1.pl | 50 | ||||
| -rw-r--r-- | challenge-077/nunovieira220/perl/ch-2.pl | 45 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-077/nunovieira220/perl/ch-1.pl b/challenge-077/nunovieira220/perl/ch-1.pl new file mode 100644 index 0000000000..2291b27875 --- /dev/null +++ b/challenge-077/nunovieira220/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Storable qw(dclone); + +# Input +my $N = scalar @ARGV ? $ARGV[0] : 9; +my %aux = (); +$aux{$N} = []; + +# Fibonacci sum +my $i = 2; +while(1) { + my $f = fib($i); + last if($f > $N); + + for my $key (keys %aux) { + my $sum = $f + $key; + next if($sum > $N); + + $aux{$sum} = () if(!$aux{$sum}); + + my @matrix = @{dclone($aux{$key})}; + for my $line (@matrix) { + my @arr = @{$line}; + if (!(grep(/^$f$/, @arr))) { + push @arr, $f; + push @{$aux{$sum}}, \@arr; + } + } + + } + + push @{$aux{$f}}, [$f]; + $i++; +} + +# Output +print "0s" if (scalar @{$aux{$N}} == 0); +for my $c (@{$aux{$N}}) { + print(join(" + ", @{$c})."\n"); +} + +# Get fibonacci numbers +sub fib { + my $n = shift; + return 1 if ($n == 1 || $n == 2); + return fib($n - 1) + fib($n - 2); +}
\ No newline at end of file diff --git a/challenge-077/nunovieira220/perl/ch-2.pl b/challenge-077/nunovieira220/perl/ch-2.pl new file mode 100644 index 0000000000..64ebd26c93 --- /dev/null +++ b/challenge-077/nunovieira220/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# Input/Output +my @arr = ( + ['0', '0', 'X', '0'], + ['X', '0', '0', '0'], + ['X', '0', '0', 'X'], + ['0', 'X', '0', '0'] +); + +# Count lonely X +my $X = scalar(@arr); +my $Y = scalar(@{$arr[0]}); +my $counter = 0; + +for(my $i = 0; $i < $X; $i++) { + for(my $j = 0; $j < $Y; $j++) { + $counter++ if($arr[$i][$j] eq 'X' && is_lonely($i, $j)); + } +} + +print $counter."\n"; + +# Test if lonely +sub is_lonely { + my ($i, $j) = @_; + + return is_blank($i-1, $j-1) + && is_blank($i-1, $j) + && is_blank($i-1, $j+1) + && is_blank($i, $j-1) + && is_blank($i, $j+1) + && is_blank($i+1, $j-1) + && is_blank($i+1, $j) + && is_blank($i+1, $j+1); +} + +# Test if element is blank +sub is_blank { + my ($i, $j) = @_; + return $i < 0 || $i == $X || $j < 0 || $j == $Y || $arr[$i][$j] eq '0'; +}
\ No newline at end of file |
