From 9673cb4f1df96f0fbf5787a6f5f74d3178e21a9b Mon Sep 17 00:00:00 2001 From: Randy Lauen Date: Tue, 3 Sep 2019 19:57:49 -0500 Subject: perl5 solution for task 2 --- challenge-024/randy-lauen/perl5/ch-2.pl | 108 ++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 challenge-024/randy-lauen/perl5/ch-2.pl 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..8f65832fd5 --- /dev/null +++ b/challenge-024/randy-lauen/perl5/ch-2.pl @@ -0,0 +1,108 @@ +#!/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. + +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'"; +if ( @matches ) { + 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 +#'(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 + of the former Galactic Empire, life + was wild, rich and largely tax free. + TXT + ); +} + + + -- cgit From eeaf64155298cc2b923bfb16ff5697c012d2a0c3 Mon Sep 17 00:00:00 2001 From: Randy Lauen Date: Tue, 3 Sep 2019 20:07:59 -0500 Subject: remove tab chars --- challenge-024/randy-lauen/perl5/ch-2.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-024/randy-lauen/perl5/ch-2.pl b/challenge-024/randy-lauen/perl5/ch-2.pl index 8f65832fd5..96c9eb10ae 100644 --- a/challenge-024/randy-lauen/perl5/ch-2.pl +++ b/challenge-024/randy-lauen/perl5/ch-2.pl @@ -11,9 +11,9 @@ Notes: 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) + 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' -- cgit From 81b39226fc3996b6d46b1e9b9574b251a0fe55c5 Mon Sep 17 00:00:00 2001 From: Randy Lauen Date: Sat, 7 Sep 2019 09:16:03 -0500 Subject: more solutions --- challenge-024/randy-lauen/perl5/ch-1.sh | 11 ++++ challenge-024/randy-lauen/perl5/ch-2.pl | 8 ++- challenge-024/randy-lauen/perl6/ch-1.sh | 11 ++++ challenge-024/randy-lauen/perl6/ch-2.p6 | 92 +++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 5 deletions(-) create mode 100755 challenge-024/randy-lauen/perl5/ch-1.sh create mode 100755 challenge-024/randy-lauen/perl6/ch-1.sh create mode 100644 challenge-024/randy-lauen/perl6/ch-2.p6 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( { . } ).reverse; + say "Found @matches.elems() document(s) for '$keyword.lc()'"; + say "* '$_.': $_. 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 + ); +} -- cgit