diff options
| author | Randy Lauen <randy.lauen@gmail.com> | 2019-09-07 09:16:03 -0500 |
|---|---|---|
| committer | Randy Lauen <randy.lauen@gmail.com> | 2019-09-07 09:16:03 -0500 |
| commit | 81b39226fc3996b6d46b1e9b9574b251a0fe55c5 (patch) | |
| tree | 2101f608bfacc6b0d055bd5446bbbcd1f32ff30d /challenge-024 | |
| parent | eeaf64155298cc2b923bfb16ff5697c012d2a0c3 (diff) | |
| download | perlweeklychallenge-club-81b39226fc3996b6d46b1e9b9574b251a0fe55c5.tar.gz perlweeklychallenge-club-81b39226fc3996b6d46b1e9b9574b251a0fe55c5.tar.bz2 perlweeklychallenge-club-81b39226fc3996b6d46b1e9b9574b251a0fe55c5.zip | |
more solutions
Diffstat (limited to 'challenge-024')
| -rwxr-xr-x | challenge-024/randy-lauen/perl5/ch-1.sh | 11 | ||||
| -rw-r--r-- | challenge-024/randy-lauen/perl5/ch-2.pl | 8 | ||||
| -rwxr-xr-x | challenge-024/randy-lauen/perl6/ch-1.sh | 11 | ||||
| -rw-r--r-- | challenge-024/randy-lauen/perl6/ch-2.p6 | 92 |
4 files changed, 117 insertions, 5 deletions
diff --git a/challenge-024/randy-lauen/perl5/ch-1.sh b/challenge-024/randy-lauen/perl5/ch-1.sh new file mode 100755 index 0000000000..00b871dc05 --- /dev/null +++ b/challenge-024/randy-lauen/perl5/ch-1.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Task: +# Create a smallest script in terms of size that on execution doesn’t throw any error. +# The script doesn’t have to do anything special. You could even come up with smallest one-liner. + +# Notes: +# An empty string would work and be even shorter, but I'll go with a non-zero +# script, with an emphasis on not doing anything special. + +perl -e ';' diff --git a/challenge-024/randy-lauen/perl5/ch-2.pl b/challenge-024/randy-lauen/perl5/ch-2.pl index 96c9eb10ae..ceedcae67a 100644 --- a/challenge-024/randy-lauen/perl5/ch-2.pl +++ b/challenge-024/randy-lauen/perl5/ch-2.pl @@ -7,7 +7,8 @@ Task: Notes: This script has a hardcoded list of documents. Run the script and pass a word as - the only argument to see which documents contain that word. + the only argument to see which documents contain that word. Results are ordered + by the document with the most occurrences of the word. Example Usage: $ perl ch-2.pl minds @@ -32,9 +33,7 @@ die "Must provide a keyword as an argument\n" unless length($keyword); my @matches = sort { $b->{freq} <=> $a->{freq} || $a->{doc} cmp $b->{doc} } $index{ $keyword }->@*; say "Found " . scalar(@matches) . " document(s) for '$keyword'"; -if ( @matches ) { - say qq[* "$_->{doc}": $_->{freq} occurence(s)] for @matches; -} +say qq[* "$_->{doc}": $_->{freq} occurence(s)] for @matches; exit 0; @@ -94,7 +93,6 @@ sub get_documents { dreadful marches to delightful measures. TXT -#'(Unconfuse VIM syntax highlighting) "Hitchhiker's Guide to the Galaxy" => <<~'TXT', Far back in the mists of ancient time, in the great and glorious days diff --git a/challenge-024/randy-lauen/perl6/ch-1.sh b/challenge-024/randy-lauen/perl6/ch-1.sh new file mode 100755 index 0000000000..9dd5dc1236 --- /dev/null +++ b/challenge-024/randy-lauen/perl6/ch-1.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Task: +# Create a smallest script in terms of size that on execution doesn’t throw any error. +# The script doesn’t have to do anything special. You could even come up with smallest one-liner. + +# Notes: +# An empty string would work and be even shorter, but I'll go with a non-zero +# script, with an emphasis on not doing anything special. + +perl6 -e ';' diff --git a/challenge-024/randy-lauen/perl6/ch-2.p6 b/challenge-024/randy-lauen/perl6/ch-2.p6 new file mode 100644 index 0000000000..2666696e78 --- /dev/null +++ b/challenge-024/randy-lauen/perl6/ch-2.p6 @@ -0,0 +1,92 @@ +#!/usr/bin/env perl6 + +=begin SYNOPSIS + +Task: + Create a script to implement full text search functionality using Inverted Index. + +Notes: + This script has a hardcoded list of documents. Run the script and pass a word as + the only argument to see which documents contain that word. Results are ordered + by the document with the most occurrences of the word. + +Example Usage: + $ perl6 ch-2.p6 minds + Found 2 document(s) for 'minds' + * "Pride and Prejudice": 1 occurence(s) + * "War of the Worlds": 1 occurence(s) + + $ perl6 ch-2.p6 universe + Found 0 document(s) for 'universe' + +=end SYNOPSIS + + +sub MAIN( Str $keyword where *.chars > 0 ) { + my %index = build_inverse_index( get_documents() ); + my @matches = %index{ $keyword.lc }:v.sort( { .<freq> } ).reverse; + say "Found @matches.elems() document(s) for '$keyword.lc()'"; + say "* '$_.<doc>': $_.<freq> occurrence(s)" for @matches; +} + + +sub build_inverse_index( %documents ) { + my %index; + + for %documents.kv -> $name, $text { + my $bag = bag $text.lc.words; + for $bag.kv -> $word, $freq { + %index{ $word }.push: %( doc => $name, freq => $freq ); + } + } + + return %index; +} + + +sub get_documents { + return %( + 'Pride and Prejudice' => q:to/TXT/, + It is a truth universally acknowledged, that + a single man in possession of a good fortune + must be in want of a wife. However little + known the feelings or views of such a man may + be on his first entering a neighbourhood, + this truth is so well fixed in the minds of + the surrounding families, that he is + considered the rightful property of some one + or other of their daughters. + TXT + 'War of the Worlds' => q:to/TXT/, + No one would have believed, in the last years + of the nineteenth century, that human affairs + were being watched from the timeless worlds + of space. No one could have dreamed that we + were being scrutinised as someone with a + microscope studies creatures that swarm and + multiply in a drop of water. And yet, across + the gulf of space, minds immeasurably + superior to ours regarded this Earth with + envious eyes, and slowly, and surely, they + drew their plans against us... + TXT + 'Richard III' => q:to/TXT/, + Now is the winter of our discontent made + glorious summer by this sun of York; and + all the clouds that lour'd upon our + house in the deep bosom of the ocean + buried. Now are our brows bound with + victorious wreaths; our bruised arms + hung up for monuments; our stern + alarums changed to merry meetings, our + dreadful marches to delightful + measures. + TXT + "Hitchhiker's Guide to the Galaxy" => q:to/TXT/, + Far back in the mists of ancient + time, in the great and glorious days + of the former Galactic Empire, life + was wild, rich and largely tax free. + TXT + ); +} |
