aboutsummaryrefslogtreecommitdiff
path: root/challenge-032
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2019-10-28 09:54:46 -0500
committerJoelle Maslak <jmaslak@antelope.net>2019-10-28 09:54:46 -0500
commit86cd1a128101c8552a6e9043b79d20e9adfc4d35 (patch)
tree89f58a00cde97bb6a46cb5cb0fec40b2a63bc66e /challenge-032
parent1d714bf7a33bb75cce5538be8e50a22a8dcb2595 (diff)
downloadperlweeklychallenge-club-86cd1a128101c8552a6e9043b79d20e9adfc4d35.tar.gz
perlweeklychallenge-club-86cd1a128101c8552a6e9043b79d20e9adfc4d35.tar.bz2
perlweeklychallenge-club-86cd1a128101c8552a6e9043b79d20e9adfc4d35.zip
Joelle's solutions for 32.1 in Raku and Perl 5
Diffstat (limited to 'challenge-032')
-rwxr-xr-xchallenge-032/joelle-maslak/perl5/ch-1.pl33
-rwxr-xr-xchallenge-032/joelle-maslak/perl6/ch-1.p624
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-032/joelle-maslak/perl5/ch-1.pl b/challenge-032/joelle-maslak/perl5/ch-1.pl
new file mode 100755
index 0000000000..db838f4c57
--- /dev/null
+++ b/challenge-032/joelle-maslak/perl5/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+use v5.14;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+my $csv;
+if (@ARGV and $ARGV[0] eq '--csv') {
+ $csv = 1;
+ shift @ARGV;
+}
+push @ARGV, "example.txt" unless @ARGV;
+
+MAIN: {
+ my %words;
+ while (<<>>) {
+ chomp;
+ $words{$_}++;
+ }
+
+ my @sorted = reverse sort { $words{$a} <=> $words{$b} } keys %words;
+ for my $word (@sorted) {
+ if ($csv) {
+ say "$word," . $words{$word};
+ } else {
+ say "$word\t" . $words{$word};
+ }
+ }
+}
+
diff --git a/challenge-032/joelle-maslak/perl6/ch-1.p6 b/challenge-032/joelle-maslak/perl6/ch-1.p6
new file mode 100755
index 0000000000..3358b1954a
--- /dev/null
+++ b/challenge-032/joelle-maslak/perl6/ch-1.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub MAIN(+@filenames, Bool :$csv) {
+ # Assumption: no line is blank.
+ # Assumption: Files will fit into RAM simultaniously
+
+ @filenames.push("example.txt") unless @filenames.elems;
+
+ my @words;
+ for @filenames -> $fn {
+ @words.push: | $fn.IO.lines.grep( * ne '' );
+ }
+ my $bag = bag @words;
+ my $sorted = $bag.pairs.sort( { $^a.value <=> $^b.value } ).reverse;
+
+ if $csv {
+ say $sorted.map( { "{$_.key},{$_.value}" } ).join("\n");
+ } else {
+ say $sorted.map( { "{$_.key}\t{$_.value}" } ).join("\n");
+ }
+}
+
+