aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-10-28 22:11:15 +0000
committerGitHub <noreply@github.com>2019-10-28 22:11:15 +0000
commitb96640df909410369626f9c250012fcbc0c2a2ac (patch)
tree5ef4b784634904c1ad52f279c984a9cb00fc36e2
parent36ff3129b315442e008ea640bc16a3f1d64cbb0b (diff)
parenta099703bbdadb9578e4703b86d76ec4d1fda4811 (diff)
downloadperlweeklychallenge-club-b96640df909410369626f9c250012fcbc0c2a2ac.tar.gz
perlweeklychallenge-club-b96640df909410369626f9c250012fcbc0c2a2ac.tar.bz2
perlweeklychallenge-club-b96640df909410369626f9c250012fcbc0c2a2ac.zip
Merge pull request #866 from choroba/ech32
Solve 032 (ASCII Bar chart + Count instances)
-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
3 files changed, 92 insertions, 0 deletions
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');