aboutsummaryrefslogtreecommitdiff
path: root/challenge-024
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-09-06 15:33:52 +0100
committerGitHub <noreply@github.com>2019-09-06 15:33:52 +0100
commit6bed3f6a2e65201abe4f3685d2b1febd3ae07bb8 (patch)
treefc458961e7e2904dacc07f307e4ab8c8fe5710e1 /challenge-024
parentaed0db39edba13ff8a6df3c7f25cec35bd1e2cb3 (diff)
parent1e534120e5bcd7cae116d2090dd8e8d0c3080c6c (diff)
downloadperlweeklychallenge-club-6bed3f6a2e65201abe4f3685d2b1febd3ae07bb8.tar.gz
perlweeklychallenge-club-6bed3f6a2e65201abe4f3685d2b1febd3ae07bb8.tar.bz2
perlweeklychallenge-club-6bed3f6a2e65201abe4f3685d2b1febd3ae07bb8.zip
Merge pull request #600 from drclaw1394/master
ruben/drclaw solutions for w24 ch1 ch2. p5 and p6
Diffstat (limited to 'challenge-024')
-rw-r--r--challenge-024/ruben-westerberg/README19
-rw-r--r--challenge-024/ruben-westerberg/perl5/ch-1.pl0
-rwxr-xr-xchallenge-024/ruben-westerberg/perl5/ch-2.pl24
-rw-r--r--challenge-024/ruben-westerberg/perl6/ch-1.p60
-rwxr-xr-xchallenge-024/ruben-westerberg/perl6/ch-2.p628
5 files changed, 61 insertions, 10 deletions
diff --git a/challenge-024/ruben-westerberg/README b/challenge-024/ruben-westerberg/README
index fa2d20850a..bd6dab73ef 100644
--- a/challenge-024/ruben-westerberg/README
+++ b/challenge-024/ruben-westerberg/README
@@ -2,17 +2,16 @@ Solution by Ruben Westerberg
ch-1.pl and ch-1.p6
===
-Run the program to calculate the forward difference of a number sequence entered on the command line.
-If no order is specified an order of 1 is used and if no sequence is specified a example sequence is used
-
- ch-1.pl usage:
- ./ch-1.pl -o 2 -- 1 3 6 9
- ch-2.p6 usage:
- ./ch-1.p6 --order=2 1 3 6 9
+Shortest script that doesn't throw an error...
+ usage
+ perl ch-1.pl
+ perl6 ch-1.pl
+ Scripts are 0 bytes in size
ch-2.pl and ch-2.p6
===
-Run the program to display the prime decomposition of the number intered on the command line.
-If no number is specifed the demo value of 228 is decomposed.
-
+Builds and prints out a inverted index of all words in in put documents. Text files are provided as command line arguments.
+The output of the program show the word, the locations of this word in each of the files.
+usage example:
+ ./ch-2.pl ../text/{beowulf.txt,pride.txt}
diff --git a/challenge-024/ruben-westerberg/perl5/ch-1.pl b/challenge-024/ruben-westerberg/perl5/ch-1.pl
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-024/ruben-westerberg/perl5/ch-1.pl
diff --git a/challenge-024/ruben-westerberg/perl5/ch-2.pl b/challenge-024/ruben-westerberg/perl5/ch-2.pl
new file mode 100755
index 0000000000..90fdecf927
--- /dev/null
+++ b/challenge-024/ruben-westerberg/perl5/ch-2.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+my %index;
+for my $path (@ARGV) {
+ open my $h, "<",$path;
+ my $number=1;
+ while (my $line=<$h>) {
+ my @words=split /\W+/, $line;
+ for my $w (@words) {
+ $index{$w}{$path}{count}++;
+ push @{$index{$w}{$path}{lines}}, $number;
+ }
+ $number++;
+ }
+ close $h;
+}
+for (sort keys %index) {
+ print "Word: $_\n";
+ my $e=$index{$_};
+ for (sort keys %$e) {
+ printf "\tFile: %s\t Count: %s\t Line: %s\n",$_,$$e{$_}{count},join " ", @{$$e{$_}{lines}};
+ }
+}
diff --git a/challenge-024/ruben-westerberg/perl6/ch-1.p6 b/challenge-024/ruben-westerberg/perl6/ch-1.p6
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-024/ruben-westerberg/perl6/ch-1.p6
diff --git a/challenge-024/ruben-westerberg/perl6/ch-2.p6 b/challenge-024/ruben-westerberg/perl6/ch-2.p6
new file mode 100755
index 0000000000..32ebcba314
--- /dev/null
+++ b/challenge-024/ruben-westerberg/perl6/ch-2.p6
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl6
+#
+my %index;
+
+#build the index tree
+$*ARGFILES.handles.map({
+ .encoding('utf8');
+ my $path=.path;
+ my $line=1;
+ sink .lines.map({
+ sink .comb(/\w+/). map({
+ %index{$_}{$path}<count>++;
+ %index{$_}{$path}<lines>.push($line);
+ });
+ $line++;
+ });
+});
+
+#print out the index tree sorted by words
+%index.keys.sort.map({
+ print "Word: $_\n";
+ my $e=%index{$_};#$_;
+ sink $e.keys.sort.map({
+ printf "\tFile: %s\t Count: %s\t Line: %s\n", $_, $e{$_}.<count>, $e{$_}.<lines>;
+ });
+});
+
+