From 86cd1a128101c8552a6e9043b79d20e9adfc4d35 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Mon, 28 Oct 2019 09:54:46 -0500 Subject: Joelle's solutions for 32.1 in Raku and Perl 5 --- challenge-032/joelle-maslak/perl5/ch-1.pl | 33 +++++++++++++++++++++++++++++++ challenge-032/joelle-maslak/perl6/ch-1.p6 | 24 ++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100755 challenge-032/joelle-maslak/perl5/ch-1.pl create mode 100755 challenge-032/joelle-maslak/perl6/ch-1.p6 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"); + } +} + + -- cgit From 93293fe76aeba01a4841f30243ae56b9c8b3074d Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Mon, 28 Oct 2019 10:03:38 -0500 Subject: Remove some cruft --- challenge-032/joelle-maslak/perl5/ch-1.pl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/challenge-032/joelle-maslak/perl5/ch-1.pl b/challenge-032/joelle-maslak/perl5/ch-1.pl index db838f4c57..fc0f9f75aa 100755 --- a/challenge-032/joelle-maslak/perl5/ch-1.pl +++ b/challenge-032/joelle-maslak/perl5/ch-1.pl @@ -3,10 +3,6 @@ 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; -- cgit From 8793c196ef10cc6ac28e92973a0f8e687e0d4860 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Mon, 28 Oct 2019 10:09:40 -0500 Subject: Add example word file --- challenge-032/joelle-maslak/perl5/example.txt | 6 ++++++ challenge-032/joelle-maslak/perl6/example.txt | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 challenge-032/joelle-maslak/perl5/example.txt create mode 100644 challenge-032/joelle-maslak/perl6/example.txt 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/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 -- cgit From a029e6e6de56a0bd366661672b8f38983f60c55a Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Mon, 28 Oct 2019 11:41:59 -0500 Subject: Joelle's Raku solution to 32.2 --- challenge-032/joelle-maslak/perl6/ch-2.p6 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 challenge-032/joelle-maslak/perl6/ch-2.p6 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; + } +} + -- cgit From ecbeda9b573c588eec9cf7f43a83844c87cf127b Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Tue, 29 Oct 2019 11:39:27 -0500 Subject: Joelle's Solution to 32.2 in Perl 5 --- challenge-032/joelle-maslak/perl5/ch-2.pl | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 challenge-032/joelle-maslak/perl5/ch-2.pl 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); + } +} -- cgit