diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-25 20:07:40 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-26 00:06:54 +0000 |
| commit | 3fa58628535d4041c7cc648c005080ca88f18c18 (patch) | |
| tree | 336fe3cc14f518f05e871ab974cc86a09a2fd8f6 /challenge-076 | |
| parent | 3d3900a2f0f69c54a34683e4e1b5da007b4af9d9 (diff) | |
| download | perlweeklychallenge-club-3fa58628535d4041c7cc648c005080ca88f18c18.tar.gz perlweeklychallenge-club-3fa58628535d4041c7cc648c005080ca88f18c18.tar.bz2 perlweeklychallenge-club-3fa58628535d4041c7cc648c005080ca88f18c18.zip | |
Replace tabs by spaces so that indentation looks correct
Diffstat (limited to 'challenge-076')
| -rw-r--r-- | challenge-076/paulo-custodio/perl/ch-1.pl | 90 | ||||
| -rw-r--r-- | challenge-076/paulo-custodio/perl/ch-2.pl | 86 | ||||
| -rw-r--r-- | challenge-076/paulo-custodio/test.pl | 8 |
3 files changed, 92 insertions, 92 deletions
diff --git a/challenge-076/paulo-custodio/perl/ch-1.pl b/challenge-076/paulo-custodio/perl/ch-1.pl index 5619acd725..1eeef4b579 100644 --- a/challenge-076/paulo-custodio/perl/ch-1.pl +++ b/challenge-076/paulo-custodio/perl/ch-1.pl @@ -1,18 +1,18 @@ #!/usr/bin/perl # Challenge 076 -# +# # TASK #1 › Prime Sum # Submitted by: Mohammad S Anwar # Reviewed by: Ryan Thompson # You are given a number $N. Write a script to find the minimum number of prime numbers required, whose summation gives you $N. -# +# # For the sake of this task, please assume 1 is not a prime number. -# +# # Example: # Input: # $N = 9 -# +# # Ouput: # 2 as sum of 2 prime numbers i.e. 2 and 7 is same as the input number. # 2 + 7 = 9. @@ -27,70 +27,70 @@ my($N) = shift; my @solution = find_set($N); if (@solution) { - say join(" + ", sort @solution)." = $N"; + say join(" + ", sort @solution)." = $N"; } else { - die "no solution found for $N\n"; + die "no solution found for $N\n"; } # check if number is prime sub is_prime { - my($n) = @_; + my($n) = @_; return 0 if $n <= 1; return 1 if $n <= 3; - + return 0 if ($n % 2)==0 || ($n % 3)==0; - + for (my $i = 5; $i*$i <= $n; $i += 6) { - return 0 if ($n % $i)==0 || ($n % ($i+2))==0; - } - + return 0 if ($n % $i)==0 || ($n % ($i+2))==0; + } + return 1; } # next prime sub next_prime { - my($n) = @_; - - return 2 if $n <= 1; - my $p = $n; - 1 while !is_prime(++$p); - return $p; + my($n) = @_; + + return 2 if $n <= 1; + my $p = $n; + 1 while !is_prime(++$p); + return $p; } # get all primes up to N sub primes { - my($n) = @_; - my $p = 2; - my @primes; - while ($p <= $n) { - push @primes, $p; - $p = next_prime($p); - } - return @primes; + my($n) = @_; + my $p = 2; + my @primes; + while ($p <= $n) { + push @primes, $p; + $p = next_prime($p); + } + return @primes; } # check all combinations for the shortest that adds up to N sub find_set { - my($n) = @_; - - # get all primes up to N, 1 not included; - # append primes 2 and 3 to be able to solve special cases 4 and 6 - my @primes = (primes($n), 2, 3); - - my @solution; - for my $k (1 .. scalar(@primes)) { - my $combinat = Math::Combinatorics->new(count => $k, data => \@primes); - while(my @set = $combinat->next_combination) { - if (sum(@set) == $n) { - if (!@solution || @solution > @set) { - @solution = @set; - return @solution if @solution == 1; - } - } - } - } - return @solution; + my($n) = @_; + + # get all primes up to N, 1 not included; + # append primes 2 and 3 to be able to solve special cases 4 and 6 + my @primes = (primes($n), 2, 3); + + my @solution; + for my $k (1 .. scalar(@primes)) { + my $combinat = Math::Combinatorics->new(count => $k, data => \@primes); + while(my @set = $combinat->next_combination) { + if (sum(@set) == $n) { + if (!@solution || @solution > @set) { + @solution = @set; + return @solution if @solution == 1; + } + } + } + } + return @solution; } diff --git a/challenge-076/paulo-custodio/perl/ch-2.pl b/challenge-076/paulo-custodio/perl/ch-2.pl index 7587f16983..f401b68f31 100644 --- a/challenge-076/paulo-custodio/perl/ch-2.pl +++ b/challenge-076/paulo-custodio/perl/ch-2.pl @@ -1,14 +1,14 @@ #!/usr/bin/perl # Challenge 076 -# +# # TASK #2 › Word Search # Submitted by: Neil Bowers # Reviewed by: Ryan Thompson # Write a script that takes two file names. The first file would contain word search grid as shown below. The second file contains list of words, one word per line. You could even use local dictionary file. -# +# # Print out a list of all words seen on the grid, looking both orthogonally and diagonally, backwards as well as forwards. -# +# # Search Grid # B I D E M I A T S U C C O R S T # L D E G G I W Q H O D E E H D P @@ -31,7 +31,7 @@ # D R S M P C U U N E L T E S I L # Output # Found 54 words of length 5 or more when checked against the local dictionary. You may or may not get the same result but that is fine. -# +# # aimed, align, antes, argos, arose, ashed, blunt, blunts, broad, buries, clove, cloven, constitution, constitutions, croon, depart, departed, enter, filch, garlic, goats, grieve, grieves, hazard, liens, malign, malignant, malls, margo, midst, ought, ovary, parted, patna, pudgiest, quash, quashed, raped, ruses, shrine, shrines, social, socializing, spasm, spasmodic, succor, succors, theorem, theorems, traci, tracie, virus, viruses, wigged use strict; @@ -51,50 +51,50 @@ say join(", ", sort @found); # parse grid file, return matrix m x n or letters sub parse_grid { - my($file) = @_; - my @grid = path($file)->lines; - for (@grid) { - s/\s+//g; - $_ = [split //, $_]; - } - return @grid; + my($file) = @_; + my @grid = path($file)->lines; + for (@grid) { + s/\s+//g; + $_ = [split //, $_]; + } + return @grid; } -# extract all possible words with the given minimum length +# extract all possible words with the given minimum length # from the grid in all 8 directions sub grid_words { - my($min_len, $grid) = @_; - my %words; - for my $r0 (0 .. $#{$grid}) { - for my $c0 (0 .. $#{$grid->[0]}) { - for my $dr (-1 .. 1) { - for my $dc (-1 .. 1) { - if (!($dr==0 && $dc==0)) { - my $word = ""; - for (my $len = 0; 1; $len++) { - my($r, $c) = ($r0+$len*$dr, $c0+$len*$dc); - last if $r < 0 || $r > $#{$grid} || $c < 0 || $c > $#{$grid->[0]}; - $word .= $grid->[$r][$c]; - if (length($word) >= $min_len) { - $words{lc($word)} = 1; - } - } - } - } - } - } - } - return %words; -} + my($min_len, $grid) = @_; + my %words; + for my $r0 (0 .. $#{$grid}) { + for my $c0 (0 .. $#{$grid->[0]}) { + for my $dr (-1 .. 1) { + for my $dc (-1 .. 1) { + if (!($dr==0 && $dc==0)) { + my $word = ""; + for (my $len = 0; 1; $len++) { + my($r, $c) = ($r0+$len*$dr, $c0+$len*$dc); + last if $r < 0 || $r > $#{$grid} || $c < 0 || $c > $#{$grid->[0]}; + $word .= $grid->[$r][$c]; + if (length($word) >= $min_len) { + $words{lc($word)} = 1; + } + } + } + } + } + } + } + return %words; +} # return all wards from dictionary that exist in the given hash sub find_dict { - my($dict, $words) = @_; - my @found; - open(my $fh, "<", $dict) or die "$dict: $!\n"; - while (<$fh>) { - chomp; - push @found, $_ if exists $words->{$_}; - } - return @found; + my($dict, $words) = @_; + my @found; + open(my $fh, "<", $dict) or die "$dict: $!\n"; + while (<$fh>) { + chomp; + push @found, $_ if exists $words->{$_}; + } + return @found; } diff --git a/challenge-076/paulo-custodio/test.pl b/challenge-076/paulo-custodio/test.pl index 96cf1f638b..9570a78a66 100644 --- a/challenge-076/paulo-custodio/test.pl +++ b/challenge-076/paulo-custodio/test.pl @@ -30,8 +30,8 @@ END done_testing; sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; + my($cmd) = @_; + my $out = `$cmd`; + $out =~ s/[ \t\v\f\r]*\n/\n/g; + return $out; } |
