aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-10-28 13:59:21 +0000
committerGitHub <noreply@github.com>2019-10-28 13:59:21 +0000
commit881066709e84c09b4c840f32c2f3b4b9169268db (patch)
tree3b2ee219861b7a681e48c5b396d8e4aaf100b620
parent04d3e980ff8736b3efee19551fc9c9f6fc388dcb (diff)
parent43029e2c6da54416fbb1ec97d804f1ed2308fb87 (diff)
downloadperlweeklychallenge-club-881066709e84c09b4c840f32c2f3b4b9169268db.tar.gz
perlweeklychallenge-club-881066709e84c09b4c840f32c2f3b4b9169268db.tar.bz2
perlweeklychallenge-club-881066709e84c09b4c840f32c2f3b4b9169268db.zip
Merge pull request #856 from davorg/master
Challenge #032
-rw-r--r--challenge-032/dave-cross/perl5/ch-1.pl22
-rw-r--r--challenge-032/dave-cross/perl5/ch-2.pl46
-rw-r--r--challenge-032/dave-cross/perl5/input6
3 files changed, 74 insertions, 0 deletions
diff --git a/challenge-032/dave-cross/perl5/ch-1.pl b/challenge-032/dave-cross/perl5/ch-1.pl
new file mode 100644
index 0000000000..42a706b7ed
--- /dev/null
+++ b/challenge-032/dave-cross/perl5/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+my $csv;
+if ($ARGV[0] eq '-csv') {
+ shift;
+ $csv = 1;
+}
+
+my %count;
+
+while (<>) {
+ chomp;
+ $count{$_}++;
+}
+
+for (sort { $count{$b} <=> $count{$a} } keys %count) {
+ say $_, ($csv ? ',' : "\t"), $count{$_};
+}
diff --git a/challenge-032/dave-cross/perl5/ch-2.pl b/challenge-032/dave-cross/perl5/ch-2.pl
new file mode 100644
index 0000000000..37fbda8759
--- /dev/null
+++ b/challenge-032/dave-cross/perl5/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+use List::Util 'max';
+use Term::ReadKey;
+
+my $input = {
+ apple => 3,
+ cherry => 2,
+ banana => 1,
+};
+
+say "Sort by keys:";
+say text_bar($input);
+say "\nSort by values:";
+say text_bar($input, 'v');
+
+sub text_bar {
+ my ($data) = @_;
+
+ my $sort_by_keys = 1;
+ if (lc ($_[1] // '') eq 'v') {
+ $sort_by_keys = 0;
+ }
+
+ my ($width) = GetTerminalSize;
+ say $width;
+
+ my $keylen = max map { length } keys %$data;
+ my $maxval = max values %$data;
+
+ my $step = int($width - $keylen - 3) / $maxval;
+
+ if ($sort_by_keys) {
+ for (sort keys %$data) {
+ printf "%${keylen}s : %s\n", $_, '#' x ($data->{$_} * $step);
+ }
+ } else{
+ for (sort { $data->{$b} <=> $data->{$a} } keys %$data) {
+ printf "%${keylen}s : %s\n", $_, '#' x ($data->{$_} * $step);
+ }
+ }
+}
diff --git a/challenge-032/dave-cross/perl5/input b/challenge-032/dave-cross/perl5/input
new file mode 100644
index 0000000000..00fe021a08
--- /dev/null
+++ b/challenge-032/dave-cross/perl5/input
@@ -0,0 +1,6 @@
+apple
+banana
+apple
+cherry
+cherry
+apple