diff options
| author | wanderdoc <wanderdoc@googlemail.com> | 2020-09-06 16:03:14 +0200 |
|---|---|---|
| committer | wanderdoc <wanderdoc@googlemail.com> | 2020-09-06 16:03:14 +0200 |
| commit | 9fdc169ab17cf6e4cdf3bedaf649bcb39abe3121 (patch) | |
| tree | 81b5c3f5191687d8416eb3419d99250821f97162 /challenge-076 | |
| parent | 5ea3691b7602fc2d06796d77497e207be4b333b8 (diff) | |
| download | perlweeklychallenge-club-9fdc169ab17cf6e4cdf3bedaf649bcb39abe3121.tar.gz perlweeklychallenge-club-9fdc169ab17cf6e4cdf3bedaf649bcb39abe3121.tar.bz2 perlweeklychallenge-club-9fdc169ab17cf6e4cdf3bedaf649bcb39abe3121.zip | |
Solutions to challenge-076.
Diffstat (limited to 'challenge-076')
| -rw-r--r-- | challenge-076/wanderdoc/perl/ch-1.pl | 83 | ||||
| -rw-r--r-- | challenge-076/wanderdoc/perl/ch-2.pl | 233 | ||||
| -rw-r--r-- | challenge-076/wanderdoc/perl/grid.txt | 19 |
3 files changed, 335 insertions, 0 deletions
diff --git a/challenge-076/wanderdoc/perl/ch-1.pl b/challenge-076/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..ba26ef2521 --- /dev/null +++ b/challenge-076/wanderdoc/perl/ch-1.pl @@ -0,0 +1,83 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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. Output: 2 as sum of 2 prime numbers i.e. 2 and 7 is same as the input number. 2 + 7 = 9. + +=cut + + + + + + + +use ntheory qw(primes); + + +my %primes; +my $sorted_primes = primes(1000); +@primes{@{$sorted_primes}} = undef; + +for my $NUM ( 5 .. 1_000 ) +{ + + if ( $NUM % 2 == 0 ) + { + my ($first, $second) = check_even($NUM); + print "${NUM} = ${first} + ${second}$/" unless $first == 0; + print "Something went wrong, probably too big number.$/" if $first == 0; + } + else + { + my @answer = sort {$a<=>$b} check_odd($NUM); + print "${NUM} = ", join(' + ', @answer), $/ unless $answer[0] == 0; + print "Something went wrong, probably too big number.$/" if $answer[0] == 0; + } + + +} + + + +# Algorithm: https://stackoverflow.com/questions/35755825 +sub check_even +{ + my $num = $_[0]; + + for my $prim ( @{$sorted_primes} ) + { + if ( exists $primes{$num - $prim} ) + { + + return ($prim, $num - $prim); + } + } + + return (0, 0); +} + +sub check_odd +{ + my $num = $_[0]; + if ( exists $primes{$num - 2} ) + { + + return ($num - 2, 2); + } + else + { + my ($first, $second) = check_even($num - 3); + if ( exists $primes{$first} ) + { + return ($first, $second, 3); + + } + else + { + return (0, 0, 0); + } + } +}
\ No newline at end of file diff --git a/challenge-076/wanderdoc/perl/ch-2.pl b/challenge-076/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..3038107cd7 --- /dev/null +++ b/challenge-076/wanderdoc/perl/ch-2.pl @@ -0,0 +1,233 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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. +Output +Found 55 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. +argos, constitution, margo, patna, traci, tracie, aimed, align, antes, arose, ashed, blunt, blunts, broad, buries, clove, cloven, constitution, constitutions, croon, depart, departed, enter, filch, garlic, goats, grieve, grieves, hazard, liens, malign, malignant, malls, midst, ought, ovary, parted, pudgiest, quash, quashed, raped, ruses, shrine, shrines, social, socializing, spasm, spasmodic, succor, succors, theorem, theorems, virus, viruses, wigged +=cut + + + + + + +use File::Basename; +use Mojo::UserAgent; +use URI; +use FindBin qw($Bin); +use List::Util qw(sum); +use v5.10; # state. + + + + +# Download dictionary if it was not downloaded yet. + +unless ( (-e "$Bin/words.txt") and (-s "$Bin/words.txt" > 5_000_000) ) +{ + my $url = URI->new( 'https://github.com/dwyl/english-words/raw/master/words.txt' ); + my $file = basename( $url->path ); + my $response = Mojo::UserAgent->new->max_redirects(5)->get( $url->as_string )->res; + die "Error while downloading a dictionary file!$/" unless 200 == $response->code; + open my $fh, '>', "$Bin/$file" or die "$!"; + print {$fh} $response->body; +} + + + +# Load dictionary. +my %dict; +{ + + open my $in, "<", "$Bin/words.txt" or die "$!"; + while ( my $line = <$in> ) + { + chomp $line; + + next unless length($line) >= 5; + $dict{lc $line} = undef; + } +} + + + +# Load grid. + +my @grid; +{ + open my $in, "<", "$Bin/grid.txt" or die "$!"; + while ( my $line = <$in> ) + { + chomp $line; + $line = lc $line; + my @string = split(/\s/,$line); + + push @grid, [@string]; + } +} + + + +print "First search:$/"; +grid_search(\@grid, 5); + +print "After 90-Rotation:$/"; +grid_search(rotate_90([@grid]), 5); + +print "After 45-Rotation:$/"; +grid_search(rotate_45([@grid]), 5); + +print "After 135-Rotation:$/"; +grid_search(rotate_45(rotate_90([@grid])), 5); + + +# my $counter; +sub grid_search +{ + my ($aref, $length) = @_; + state $counter; + for my $part ( @$aref ) + { + + for my $i ( 0 .. $#$part - $length ) + { + for my $j ( $length - 1 .. $#$part) + { + next unless $j - $i >= $length - 1; + my $chunk = join('',@{$part}[$i .. $j]); + my $reversed = reverse $chunk; + for my $candidate ( $chunk, $reversed ) + { + + print ++$counter, ' ', $candidate, $/ + if exists $dict{$candidate}; + } + } + } + } +} + + +sub rotate_90 +{ + my ($aref) = @_; + my $rotated; + for my $row_idx ( 0 .. $#$aref ) + { + for my $col_idx ( 0 .. $#{$aref->[$row_idx]} ) + { + $rotated->[$col_idx][$#$aref - $row_idx] = + $aref->[$row_idx][$col_idx]; + } + } + return $rotated; +} + +sub rotate_45 +{ + my $aref = $_[0]; + my $rotated; + for my $row_idx ( 0 .. $#$aref ) + { + for my $col_idx (0 .. $#{$aref->[$row_idx]}) + { + push @{$rotated->[ sum($row_idx, $col_idx)]}, + $aref->[$row_idx][$col_idx]; + } + } + return $rotated; +} + +=output +First search: +1 aimed +2 succor +3 succors +4 wigged +5 buries +6 butea +7 izing +8 socializing +9 social +10 midst +11 midsts +12 broad +13 goats +14 ovary +15 patna +16 malignant +17 angil +18 align +19 malign +20 virus +21 viruses +22 ruses +23 garlic +24 filch +25 sices +After 90-Rotation: +26 hazard +27 shazar +28 shrines +29 shrine +30 lunts +31 blunts +32 grieves +33 grieve +34 acies +35 tracie +36 traci +37 ought +38 spasmodic +39 pasmo +40 spasm +41 malls +42 liens +43 parted +44 departed +45 parte +46 depart +47 raped +48 ashed +49 quashed +50 quash +51 hugin +52 margo +53 argos +54 clune +55 antes +56 enter +57 buffa +58 pudgiest +After 45-Rotation: +59 resor +60 roser +61 arose +62 cloven +63 clove +64 neuma +65 wifie +66 ileac +67 caeli +68 soyas +69 staun +70 cosin +71 ation +72 talos +73 talose +After 135-Rotation: +74 tallu +75 const +76 constitution +77 constitutions +78 raias +79 duddie +80 grith +81 theor +82 theorem +83 theorems +84 meroe +=cut
\ No newline at end of file diff --git a/challenge-076/wanderdoc/perl/grid.txt b/challenge-076/wanderdoc/perl/grid.txt new file mode 100644 index 0000000000..c5766eae93 --- /dev/null +++ b/challenge-076/wanderdoc/perl/grid.txt @@ -0,0 +1,19 @@ +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 +U S E I R U B U T E A S L A G U +N G N I Z I L A I C O S C N U D +T G M I D S T S A R A R E I F G +S R E N M D C H A S I V E E L I +S C S H A E U E B R O A D M T E +H W O V L P E D D L A I U L S S +R Y O N L A S F C S T A O G O T +I G U S S R R U G O V A R Y O C +N R G P A T N A N G I L A M O O +E I H A C E I V I R U S E S E D +S E T S U D T T G A R L I C N H +H V R M X L W I U M S N S O T B +A E A O F I L C H T O D C A E U +Z S C D F E C A A I I R L N R F +A R I I A N Y U T O O O U T P F +R S E C I S N A B O S C N E R A +D R S M P C U U N E L T E S I L
\ No newline at end of file |
