aboutsummaryrefslogtreecommitdiff
path: root/challenge-032
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-032')
-rw-r--r--challenge-032/dave-cross/perl5/ch-2.pl1
-rwxr-xr-xchallenge-032/e-choroba/perl5/ch-1.pl26
-rwxr-xr-xchallenge-032/e-choroba/perl5/ch-1.sh2
-rwxr-xr-xchallenge-032/e-choroba/perl5/ch-2.pl64
-rw-r--r--challenge-032/markus-holzer/perl6/ch-1-and-2.pl626
-rw-r--r--challenge-032/markus-holzer/perl6/ch-1.p626
-rw-r--r--challenge-032/markus-holzer/perl6/ch-2.p626
-rw-r--r--challenge-032/markus-holzer/perl6/simpsons.txt10
-rw-r--r--challenge-032/markus-holzer/perl6/test.bat6
-rw-r--r--challenge-032/steven-wilson/perl5/ch-1.pl53
10 files changed, 239 insertions, 1 deletions
diff --git a/challenge-032/dave-cross/perl5/ch-2.pl b/challenge-032/dave-cross/perl5/ch-2.pl
index 37fbda8759..5c786b2721 100644
--- a/challenge-032/dave-cross/perl5/ch-2.pl
+++ b/challenge-032/dave-cross/perl5/ch-2.pl
@@ -27,7 +27,6 @@ sub text_bar {
}
my ($width) = GetTerminalSize;
- say $width;
my $keylen = max map { length } keys %$data;
my $maxval = max values %$data;
diff --git a/challenge-032/e-choroba/perl5/ch-1.pl b/challenge-032/e-choroba/perl5/ch-1.pl
new file mode 100755
index 0000000000..4fbaef2346
--- /dev/null
+++ b/challenge-032/e-choroba/perl5/ch-1.pl
@@ -0,0 +1,26 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+
+use Getopt::Long;
+use Text::CSV_XS qw{ csv };
+use List::Util qw{ max };
+
+use open IO => ':encoding(UTF-8)', ':std';
+
+GetOptions(csv => \ my $csv_output);
+
+my %count;
+chomp, ++$count{$_} while <>;
+
+if ($csv_output) {
+ csv(in => [
+ map [$_, $count{$_} ],
+ sort { $count{$b} <=> $count{$a} }
+ keys %count]);
+
+} else {
+ my $max_length = max(map length, keys %count);
+ printf "%${max_length}s %d\n", $_, $count{$_}
+ for sort { $count{$a} <=> $count{$b} } keys %count;
+}
diff --git a/challenge-032/e-choroba/perl5/ch-1.sh b/challenge-032/e-choroba/perl5/ch-1.sh
new file mode 100755
index 0000000000..2f88949be9
--- /dev/null
+++ b/challenge-032/e-choroba/perl5/ch-1.sh
@@ -0,0 +1,2 @@
+#! /bin/bash
+cat "$@" | sort | uniq -c | sort -nr
diff --git a/challenge-032/e-choroba/perl5/ch-2.pl b/challenge-032/e-choroba/perl5/ch-2.pl
new file mode 100755
index 0000000000..752d3dc050
--- /dev/null
+++ b/challenge-032/e-choroba/perl5/ch-2.pl
@@ -0,0 +1,64 @@
+#! /usr/bin/perl
+{ package My::BarChart;
+ use Moo;
+
+ use Function::Parameters;
+ use Types::Standard qw{ HashRef Num Str Enum };
+ use List::Util qw{ max };
+
+ sub SortBy { Enum[qw[ labels values labels_desc values_desc ]] }
+
+ use namespace::clean;
+
+ has width => (is => 'lazy', isa => Num);
+ has data => (is => 'ro', isa => HashRef[Num], required => 1);
+ has separator => (is => 'ro', isa => Str, default => ' | ');
+
+ has _bar_width => (is => 'lazy', isa => Num);
+ has _max_length => (is => 'lazy', isa => Num);
+
+ method generate (SortBy $sort_by = 'keys') {
+ my $data = $self->data;
+ my $max = max(values %$data);
+
+ my $sort = {labels => \&_by_key,
+ values => \&_by_value,
+ labels_desc => \&_by_key_desc,
+ values_desc => \&_by_value_desc}->{$sort_by};
+
+ for my $key (sort { $self->$sort } keys %$data) {
+ printf '%' . $self->_max_length . "s%s%s\n",
+ $key,
+ $self->separator,
+ '#' x ($self->_bar_width / $max * $data->{$key});
+ }
+ }
+
+ method BUILD ($) {
+ die "Chart is too wide.\n" if $self->_bar_width <= 0;
+ }
+
+ method _build_width () { $ENV{COLUMNS} || qx{tput cols} || 80 }
+
+ method _build__max_length () { max(map length, keys %{ $self->data }) }
+
+ method _build__bar_width () {
+ $self->width - $self->_max_length - length $self->separator
+ }
+
+ method _by_key () { $a cmp $b }
+ method _by_key_desc () { $b cmp $a }
+ method _by_value () { $self->data->{$a} <=> $self->data->{$b} }
+ method _by_value_desc () { $self->data->{$b} <=> $self->data->{$a} }
+}
+
+use warnings;
+use strict;
+use feature qw{ say };
+
+my $data = { apple => 3, cherry => 2, banana => .5 };
+my $chart = 'My::BarChart'->new(data => $data);
+
+say $chart->generate('labels');
+say $chart->generate('values');
+say $chart->generate('values_desc');
diff --git a/challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 b/challenge-032/markus-holzer/perl6/ch-1-and-2.pl6
new file mode 100644
index 0000000000..f1eb4d25dc
--- /dev/null
+++ b/challenge-032/markus-holzer/perl6/ch-1-and-2.pl6
@@ -0,0 +1,26 @@
+my %*SUB-MAIN-OPTS = :named-anywhere;
+
+multi sub MAIN( *@files, Bool :$csv, Bool :$graph, Bool :$sort-by-label )
+{
+ CATCH { return .message.say }
+
+ my @words = @files
+ ?? @files.map( |*.IO.lines )
+ !! |$*ARGFILES.lines;
+
+ my $weights = Bag.new( @words );
+ my $lngst = max $weights.keys.map( *.chars );
+
+ my $format = $csv ?? "%s, %s" !!
+ $graph ?? "%{$lngst}s | %s " !!
+ "%-{$lngst}s %s " ;
+
+ my &sorter = $sort-by-label
+ ?? { $^a.key cmp $^b.key }
+ !! { $^b.value <=> $^a.value };
+
+ .say for $weights
+ .sort( &sorter )
+ .map({ .key => $graph ?? "#" x .value !! .value })
+ .map({ sprintf $format, .key, .value });
+} \ No newline at end of file
diff --git a/challenge-032/markus-holzer/perl6/ch-1.p6 b/challenge-032/markus-holzer/perl6/ch-1.p6
new file mode 100644
index 0000000000..f1eb4d25dc
--- /dev/null
+++ b/challenge-032/markus-holzer/perl6/ch-1.p6
@@ -0,0 +1,26 @@
+my %*SUB-MAIN-OPTS = :named-anywhere;
+
+multi sub MAIN( *@files, Bool :$csv, Bool :$graph, Bool :$sort-by-label )
+{
+ CATCH { return .message.say }
+
+ my @words = @files
+ ?? @files.map( |*.IO.lines )
+ !! |$*ARGFILES.lines;
+
+ my $weights = Bag.new( @words );
+ my $lngst = max $weights.keys.map( *.chars );
+
+ my $format = $csv ?? "%s, %s" !!
+ $graph ?? "%{$lngst}s | %s " !!
+ "%-{$lngst}s %s " ;
+
+ my &sorter = $sort-by-label
+ ?? { $^a.key cmp $^b.key }
+ !! { $^b.value <=> $^a.value };
+
+ .say for $weights
+ .sort( &sorter )
+ .map({ .key => $graph ?? "#" x .value !! .value })
+ .map({ sprintf $format, .key, .value });
+} \ No newline at end of file
diff --git a/challenge-032/markus-holzer/perl6/ch-2.p6 b/challenge-032/markus-holzer/perl6/ch-2.p6
new file mode 100644
index 0000000000..f1eb4d25dc
--- /dev/null
+++ b/challenge-032/markus-holzer/perl6/ch-2.p6
@@ -0,0 +1,26 @@
+my %*SUB-MAIN-OPTS = :named-anywhere;
+
+multi sub MAIN( *@files, Bool :$csv, Bool :$graph, Bool :$sort-by-label )
+{
+ CATCH { return .message.say }
+
+ my @words = @files
+ ?? @files.map( |*.IO.lines )
+ !! |$*ARGFILES.lines;
+
+ my $weights = Bag.new( @words );
+ my $lngst = max $weights.keys.map( *.chars );
+
+ my $format = $csv ?? "%s, %s" !!
+ $graph ?? "%{$lngst}s | %s " !!
+ "%-{$lngst}s %s " ;
+
+ my &sorter = $sort-by-label
+ ?? { $^a.key cmp $^b.key }
+ !! { $^b.value <=> $^a.value };
+
+ .say for $weights
+ .sort( &sorter )
+ .map({ .key => $graph ?? "#" x .value !! .value })
+ .map({ sprintf $format, .key, .value });
+} \ No newline at end of file
diff --git a/challenge-032/markus-holzer/perl6/simpsons.txt b/challenge-032/markus-holzer/perl6/simpsons.txt
new file mode 100644
index 0000000000..fafebd89f4
--- /dev/null
+++ b/challenge-032/markus-holzer/perl6/simpsons.txt
@@ -0,0 +1,10 @@
+Bart
+Bart
+Maggie
+Marge
+Homer
+Bart
+Homer
+Bart
+Marge
+Marge \ No newline at end of file
diff --git a/challenge-032/markus-holzer/perl6/test.bat b/challenge-032/markus-holzer/perl6/test.bat
new file mode 100644
index 0000000000..8da2aff480
--- /dev/null
+++ b/challenge-032/markus-holzer/perl6/test.bat
@@ -0,0 +1,6 @@
+@echo off
+type simpsons.txt|call perl6 ch-1-and-2.pl6
+type simpsons.txt|call perl6 ch-1-and-2.pl6 --csv
+call perl6 ch-1-and-2.pl6 simpsons.txt simpsons.txt
+call perl6 ch-1-and-2.pl6 simpsons.txt --graph
+call perl6 ch-1-and-2.pl6 --csv simpsons.txt --sort-by-label
diff --git a/challenge-032/steven-wilson/perl5/ch-1.pl b/challenge-032/steven-wilson/perl5/ch-1.pl
new file mode 100644
index 0000000000..99b1e44d31
--- /dev/null
+++ b/challenge-032/steven-wilson/perl5/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+# Author: Steven Wilson
+# Date: 2019-10-28
+# Week: 032
+
+# Task #1
+
+# Count instances
+# Create a script that either reads standard input or one or more files
+# specified on the command-line. Count the number of times and then
+# print a summary, sorted by the count of each entry.
+# So with the following input in file example.txt
+# apple
+# banana
+# apple
+# cherry
+# cherry
+# apple
+# the script would display something like:
+# apple 3
+# cherry 2
+# banana 1
+# For extra credit, add a -csv option to your script, which would generate:
+# apple,3
+# cherry,2
+# banana,1
+
+use strict;
+use warnings;
+use feature qw/ say /;
+
+my %word_count;
+my $delimiter = "\t";
+if ( $ARGV[0] eq "-csv" ) {
+ shift @ARGV;
+ $delimiter = ",";
+}
+
+for my $file (@ARGV) {
+ open my $fh, '<', $file or die "Can't open < $file: $!";
+ while ( !eof $fh ) {
+ my $word = readline $fh;
+ chomp $word;
+ $word_count{$word} += 1;
+ }
+}
+
+my @word_count_desc
+ = reverse sort { $word_count{$a} <=> $word_count{$b} } keys %word_count;
+
+for my $word (@word_count_desc) {
+ say "$word$delimiter$word_count{$word}";
+}