diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-09-06 11:15:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-06 11:15:35 +0100 |
| commit | ec565211d9eec9cec100cd8c42144202646d869b (patch) | |
| tree | e26eac84e1fa2481d448fdb7ee361b4ff6213249 | |
| parent | 5990cedcae039fa36cdd979e9f2e42af843bdc35 (diff) | |
| parent | 62decd00f202639a773c3d2e5ed5d310663dfc24 (diff) | |
| download | perlweeklychallenge-club-ec565211d9eec9cec100cd8c42144202646d869b.tar.gz perlweeklychallenge-club-ec565211d9eec9cec100cd8c42144202646d869b.tar.bz2 perlweeklychallenge-club-ec565211d9eec9cec100cd8c42144202646d869b.zip | |
Merge pull request #599 from andrezgz/challenge-024
challenge-024 andrezgz solution
| -rw-r--r-- | challenge-024/andrezgz/perl5/ch-1.pl | 1 | ||||
| -rw-r--r-- | challenge-024/andrezgz/perl5/ch-2.pl | 42 | ||||
| -rw-r--r-- | challenge-024/andrezgz/perl5/father_sons.txt | 17 | ||||
| -rw-r--r-- | challenge-024/andrezgz/perl5/herdsman_bull.txt | 14 | ||||
| -rw-r--r-- | challenge-024/andrezgz/perl5/lion_mouse.txt | 15 | ||||
| -rw-r--r-- | challenge-024/andrezgz/perl5/wolf_lamb.txt | 15 |
6 files changed, 104 insertions, 0 deletions
diff --git a/challenge-024/andrezgz/perl5/ch-1.pl b/challenge-024/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..aacea2a264 --- /dev/null +++ b/challenge-024/andrezgz/perl5/ch-1.pl @@ -0,0 +1 @@ +print "This script is the smallest in terms of size that on execution doesn't throw any error, doesn't do anything special and explains what it does" diff --git a/challenge-024/andrezgz/perl5/ch-2.pl b/challenge-024/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..49aa67a02a --- /dev/null +++ b/challenge-024/andrezgz/perl5/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-024/ +# Task #2 +# Create a script to implement full text search functionality using Inverted Index. According to wikipedia: +# In computer science, an inverted index (also referred to as a postings file or inverted file) +# is a database index storing a mapping from content, such as words or numbers, to its locations in a table, +# or in a document or a set of documents (named in contrast to a forward index, which maps from documents to content). +# The purpose of an inverted index is to allow fast full-text searches, at a cost of increased processing +# when a document is added to the database. +# Here is a nice example of Inverted Index. +# https://en.wikipedia.org/wiki/Search_engine_indexing#Inverted_indices + +use strict; +use warnings; + +die "Usage: $0 <file1> [<fileN>]" if @ARGV == 0; + +# Prepare inverted index +my %index; +while (<>) { + chomp; # remove line trailing string + s/["'?!]/ /ig; # some cleaning + my @words = grep { # filter words... + length > 2 # with 3 or more... + && /^[a-z\-]+$/i # alphabetic and hyphen characters only + } + split /[.,; ]/; # split line in words + $index{lc $_}{$ARGV}++ for (@words); # store word usage in file +} +print 'Words added: ' . join(',',sort keys %index) . "\n"; + +# Search word +print "\nSearch word: "; +my $w = <>; +chomp $w; +if ( exists $index{lc $w} ) { + print "'$w' was found on ", join(',',keys %{$index{lc $w}}) . "\n"; +} +else { + print "'$w' wasn't found on any document\n"; +} diff --git a/challenge-024/andrezgz/perl5/father_sons.txt b/challenge-024/andrezgz/perl5/father_sons.txt new file mode 100644 index 0000000000..cf15c7f292 --- /dev/null +++ b/challenge-024/andrezgz/perl5/father_sons.txt @@ -0,0 +1,17 @@ +The Father and His Sons + +A FATHER had a family of sons who were perpetually quarreling +among themselves. When he failed to heal their disputes by his +exhortations, he determined to give them a practical illustration +of the evils of disunion; and for this purpose he one day told +them to bring him a bundle of sticks. When they had done so, he +placed the faggot into the hands of each of them in succession, +and ordered them to break it in pieces. They tried with all +their strength, and were not able to do it. He next opened the +faggot, took the sticks separately, one by one, and again put +them into his sons' hands, upon which they broke them easily. He +then addressed them in these words: "My sons, if you are of one +mind, and unite to assist each other, you will be as this faggot, +uninjured by all the attempts of your enemies; but if you are +divided among yourselves, you will be broken as easily as these +sticks." diff --git a/challenge-024/andrezgz/perl5/herdsman_bull.txt b/challenge-024/andrezgz/perl5/herdsman_bull.txt new file mode 100644 index 0000000000..f106e4f08c --- /dev/null +++ b/challenge-024/andrezgz/perl5/herdsman_bull.txt @@ -0,0 +1,14 @@ +The Herdsman and the Lost Bull + +A HERDSMAN tending his flock in a forest lost a Bull-calf from +the fold. After a long and fruitless search, he made a vow that, +if he could only discover the thief who had stolen the Calf, he +would offer a lamb in sacrifice to Hermes, Pan, and the Guardian +Deities of the forest. Not long afterwards, as he ascended a +small hillock, he saw at its foot a Lion feeding on the Calf. +Terrified at the sight, he lifted his eyes and his hands to +heaven, and said: "Just now I vowed to offer a lamb to the +Guardian Deities of the forest if I could only find out who had +robbed me; but now that I have discovered the thief, I would +willingly add a full-grown Bull to the Calf I have lost, if I may +only secure my own escape from him in safety." diff --git a/challenge-024/andrezgz/perl5/lion_mouse.txt b/challenge-024/andrezgz/perl5/lion_mouse.txt new file mode 100644 index 0000000000..36738f2918 --- /dev/null +++ b/challenge-024/andrezgz/perl5/lion_mouse.txt @@ -0,0 +1,15 @@ +The Lion and the Mouse + +A LION was awakened from sleep by a Mouse running over his face. +Rising up angrily, he caught him and was about to kill him, when +the Mouse piteously entreated, saying: "If you would only spare +my life, I would be sure to repay your kindness." The Lion +laughed and let him go. It happened shortly after this that the +Lion was caught by some hunters, who bound him by st ropes to the +ground. The Mouse, recognizing his roar, came gnawed the rope +with his teeth, and set him free, exclaim + +"You ridiculed the idea of my ever being able to help you, +expecting to receive from me any repayment of your favor; I now +you know that it is possible for even a Mouse to con benefits on +a Lion." diff --git a/challenge-024/andrezgz/perl5/wolf_lamb.txt b/challenge-024/andrezgz/perl5/wolf_lamb.txt new file mode 100644 index 0000000000..3b05b40064 --- /dev/null +++ b/challenge-024/andrezgz/perl5/wolf_lamb.txt @@ -0,0 +1,15 @@ +The Wolf and the Lamb + +WOLF, meeting with a Lamb astray from the fold, resolved not to +lay violent hands on him, but to find some plea to justify to the +Lamb the Wolf's right to eat him. He thus addressed him: +"Sirrah, last year you grossly insulted me." "Indeed," bleated +the Lamb in a mournful tone of voice, "I was not then born." Then +said the Wolf, "You feed in my pasture." "No, good sir," replied +the Lamb, "I have not yet tasted grass." Again said the Wolf, +"You drink of my well." "No," exclaimed the Lamb, "I never yet +drank water, for as yet my mother's milk is both food and drink +to me." Upon which the Wolf seized him and ate him up, saying, +"Well! I won't remain supperless, even though you refute every +one of my imputations." The tyrant will always find a pretext for +his tyranny. |
