diff options
| -rwxr-xr-x | challenge-024/randy-lauen/perl5/ch-1.sh | 11 | ||||
| -rw-r--r-- | challenge-024/randy-lauen/perl5/ch-2.pl | 106 | ||||
| -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, 220 insertions, 0 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 new file mode 100644 index 0000000000..ceedcae67a --- /dev/null +++ b/challenge-024/randy-lauen/perl5/ch-2.pl @@ -0,0 +1,106 @@ +#!/usr/bin/env perl + +=head2 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: + $ perl ch-2.pl minds + Found 2 document(s) for 'minds' + * "Pride and Prejudice": 1 occurence(s) + * "War of the Worlds": 1 occurence(s) + + $ perl ch-2.pl universe + Found 0 document(s) for 'universe' + +=cut + +use v5.26; +use strict; +use warnings; + +use List::MoreUtils qw( frequency ); + +my %index = build_inverse_index( get_documents() ); +my $keyword = lc $ARGV[0] // ''; +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'"; +say qq[* "$_->{doc}": $_->{freq} occurence(s)] for @matches; + +exit 0; + + +sub build_inverse_index { + my %documents = @_; + + my %index; + + while ( my ($name, $text) = each %documents ) { + my @words = map { lc $_ } $text =~ /\w+/g; + my %freq = frequency @words; + foreach my $word ( keys %freq ) { + push $index{ $word }->@*, { doc => $name, freq => $freq{$word} }; + } + } + + return %index; +} + + +sub get_documents { + return ( + 'Pride and Prejudice' => <<~'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' => <<~'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' => <<~'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" => <<~'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 + ); +} + + + 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 + ); +} |
