aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-032/joelle-maslak/perl5/ch-1.pl29
-rwxr-xr-xchallenge-032/joelle-maslak/perl5/ch-2.pl54
-rw-r--r--challenge-032/joelle-maslak/perl5/example.txt6
-rwxr-xr-xchallenge-032/joelle-maslak/perl6/ch-1.p624
-rwxr-xr-xchallenge-032/joelle-maslak/perl6/ch-2.p631
-rw-r--r--challenge-032/joelle-maslak/perl6/example.txt6
6 files changed, 150 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..fc0f9f75aa
--- /dev/null
+++ b/challenge-032/joelle-maslak/perl5/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use v5.14;
+use strict;
+use warnings;
+
+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/perl5/ch-2.pl b/challenge-032/joelle-maslak/perl5/ch-2.pl
new file mode 100755
index 0000000000..5242b69ae9
--- /dev/null
+++ b/challenge-032/joelle-maslak/perl5/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/env perl
+use v5.24;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+use Getopt::Long;
+use List::Util qw(max min);
+
+MAIN: {
+ my $csv;
+ my $screen_width = 80;
+ my $base;
+ my $deliminator = "\t";
+
+ GetOptions(
+ "csv" => \$csv,
+ "screen-width=i" => \$screen_width,
+ "base=i" => \$base,
+ "deliminator=s" => \$deliminator,
+ );
+
+ my @words;
+ while (my $lines = <<>>) {
+ chomp $lines;
+ my (@parts) = split "$deliminator", $lines;
+
+ die "All lines must have one and only one deliminator" unless scalar(@parts) == 2;
+
+ push @words, \@parts;
+ }
+
+ my $maxlen = max map { length $_->[0] } @words;
+ my $maxval = max map { $_->[1] } @words;
+ my $minval = min map { $_->[1] } @words;
+
+ $base //= $minval;
+ die "Base should be less or equal to minimum value" unless $base <= $minval;
+
+ my $spread = $maxval - $base; # How far apart are max and min?
+ my $maxbar = $screen_width - $maxlen - 4; # How big the bar can be, we don't use the last column
+ my $unitsize = $maxbar ? ($spread / $maxbar) : 0; # What a '#' represents
+
+ foreach my $ele (@words) {
+ my $hashes = int(($ele->[1] - $base) / $unitsize);
+ $hashes = $maxbar if $unitsize == 0;
+
+ say sprintf("%-${maxlen}s", $ele->[0]) . " | " . ("#" x $hashes);
+ }
+}
diff --git a/challenge-032/joelle-maslak/perl5/example.txt b/challenge-032/joelle-maslak/perl5/example.txt
new file mode 100644
index 0000000000..00fe021a08
--- /dev/null
+++ b/challenge-032/joelle-maslak/perl5/example.txt
@@ -0,0 +1,6 @@
+apple
+banana
+apple
+cherry
+cherry
+apple
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");
+ }
+}
+
+
diff --git a/challenge-032/joelle-maslak/perl6/ch-2.p6 b/challenge-032/joelle-maslak/perl6/ch-2.p6
new file mode 100755
index 0000000000..63c7542cb6
--- /dev/null
+++ b/challenge-032/joelle-maslak/perl6/ch-2.p6
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub MAIN($input-file?, :$deliminator = "\t", UInt:D :$screen-width = 80, Int :$base) {
+ my $fh = $input-file ?? $input-file.IO !! $*IN;
+ my @words = $fh.lines.map(*.split($deliminator));
+ return unless @words.elems; # Make sure at least one line is present
+
+ die "All lines must have one and only one deliminator" if @words.first( { $_.elems ≠ 2 } );
+
+ my $max-len = @words»[0]».chars.max; # Max label length
+ my $max-value = @words»[1].max; # Max value
+ my $min-value = @words»[1].min; # Min value
+
+ if $base.defined {
+ die "Base should be less or equal to minimum value" if $base > $min-value;
+ $min-value = $base;
+ }
+
+ my $spread = $max-value - $min-value; # How far apart are max and min?
+ my $max-bar = $screen-width - $max-len - 4; # How big the bar can be, we don't use last column
+ my $unit-size = $max-bar ?? ($spread / $max-bar) !! 0; # What a '#' represents
+
+ for @words -> $ele {
+ my $hashes = (($ele[1] - $min-value) / $unit-size).Int;
+ $hashes = $max-bar if $unit-size == 0;
+
+ say $ele[0].fmt("%-{$max-len}s") ~ " | " ~ "#" x $hashes;
+ }
+}
+
diff --git a/challenge-032/joelle-maslak/perl6/example.txt b/challenge-032/joelle-maslak/perl6/example.txt
new file mode 100644
index 0000000000..00fe021a08
--- /dev/null
+++ b/challenge-032/joelle-maslak/perl6/example.txt
@@ -0,0 +1,6 @@
+apple
+banana
+apple
+cherry
+cherry
+apple