diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-11 23:02:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-11 23:02:58 +0100 |
| commit | 033e175a03862da00e81e4978061615dbc5bdc5f (patch) | |
| tree | e22bb42464d4b1d26083c5dbece67877c3f12098 | |
| parent | a506610165c37d05f8ba13d1e960ed20a9ddf25b (diff) | |
| parent | 0e2021549cc7b943267e9a58c7271006c0f12551 (diff) | |
| download | perlweeklychallenge-club-033e175a03862da00e81e4978061615dbc5bdc5f.tar.gz perlweeklychallenge-club-033e175a03862da00e81e4978061615dbc5bdc5f.tar.bz2 perlweeklychallenge-club-033e175a03862da00e81e4978061615dbc5bdc5f.zip | |
Merge pull request #2497 from adamcrussell/challenge-081
solutions for challenge 081
| -rw-r--r-- | challenge-081/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-081/adam-russell/perl/ch-1.pl | 51 | ||||
| -rw-r--r-- | challenge-081/adam-russell/perl/ch-2.pl | 48 |
3 files changed, 100 insertions, 0 deletions
diff --git a/challenge-081/adam-russell/blog.txt b/challenge-081/adam-russell/blog.txt new file mode 100644 index 0000000000..d13725d219 --- /dev/null +++ b/challenge-081/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/2020/10/11#pwc081 diff --git a/challenge-081/adam-russell/perl/ch-1.pl b/challenge-081/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..09e74484b9 --- /dev/null +++ b/challenge-081/adam-russell/perl/ch-1.pl @@ -0,0 +1,51 @@ +use strict; +use warnings; +## +# You are given 2 strings, $A and $B. +# Write a script to find out common base strings in $A and $B. +## +use boolean; + +sub contains{ + my($s0) = @_; + return sub{ + my($s) = @_; + return [true, $s0] if($s =~ m/^($s0)+$/g); + return [false, $s0]; + } +} + +sub make_checks{ + my($s) = @_; + my @letters = split(//, $s); + my @checks; + for my $i (0 .. @letters - 1){ + push @checks, contains(join("", @letters[0 .. $i])); + } + return @checks; +} + +MAIN:{ + my($A, $B); + $A = "abcdabcd"; + $B = "abcdabcdabcdabcd"; + my @checks; + @checks = make_checks($A); + print "$A, $B --> ("; + for my $check (@checks){ + if($check->($A)->[0] && $check->($B)->[0]){ + print $check->($A)->[1] . " "; + } + } + print "\b)\n"; + $A = "aaa"; + $B = "aa"; + @checks = make_checks($A); + print "$A, $B --> ("; + for my $check (@checks){ + if($check->($A)->[0] && $check->($B)->[0]){ + print $check->($A)->[1] . " "; + } + } + print "\b)\n"; +}
\ No newline at end of file diff --git a/challenge-081/adam-russell/perl/ch-2.pl b/challenge-081/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..dd4ad4be2e --- /dev/null +++ b/challenge-081/adam-russell/perl/ch-2.pl @@ -0,0 +1,48 @@ +use strict; +use warnings; +## +# You are given file named input. +# Write a script to find the frequency of all the words. +# It should print the result as first column of each line should be the frequency of the +# word followed by all the words of that frequency arranged in lexicographical order. Also +# sort the words in the ascending order of frequency. +## +MAIN:{ + my %counts; + my %count_words; + my $s; + { local $/; + $s = <DATA>; + } + $s =~ s/'s//g; + $s =~ tr/."(),//d; + $s =~ tr/-/ /; + my @words = split(/\s+/, $s); + for my $word (@words){ + $counts{$word}++; + } + for my $k (keys %counts){ + my $count = $counts{$k}; + push @{$count_words{$count}}, $k; + } + for my $k (sort keys %count_words){ + print $k . "\t" . join(" ", sort {$a cmp $b} @{$count_words{$k}}) . "\n"; + } +} + + +__DATA__ +West Side Story + +The award-winning adaptation of the classic romantic tragedy "Romeo and +Juliet". The feuding families become two warring New York City gangs, +the white Jets led by Riff and the Latino Sharks, led by Bernardo. Their +hatred escalates to a point where neither can coexist with any form of +understanding. But when Riff's best friend (and former Jet) Tony and +Bernardo's younger sister Maria meet at a dance, no one can do anything +to stop their love. Maria and Tony begin meeting in secret, planning to +run away. Then the Sharks and Jets plan a rumble under the +highway--whoever wins gains control of the streets. Maria sends Tony to +stop it, hoping it can end the violence. It goes terribly wrong, and +before the lovers know what's happened, tragedy strikes and doesn't stop +until the climactic and heartbreaking ending.
\ No newline at end of file |
