aboutsummaryrefslogtreecommitdiff
path: root/challenge-032
diff options
context:
space:
mode:
authorrage311 <rage_311@hotmail.com>2019-10-29 22:49:40 -0600
committerrage311 <rage_311@hotmail.com>2019-10-29 22:49:40 -0600
commit2062fa8cc1cb8b067659b2102ddf4d9b276d8e76 (patch)
tree8abc03d63b2286938c35be4070e4f0aa9f80944e /challenge-032
parent8b8f31ae1cd3bafe336df787e5a79b3a34730647 (diff)
downloadperlweeklychallenge-club-2062fa8cc1cb8b067659b2102ddf4d9b276d8e76.tar.gz
perlweeklychallenge-club-2062fa8cc1cb8b067659b2102ddf4d9b276d8e76.tar.bz2
perlweeklychallenge-club-2062fa8cc1cb8b067659b2102ddf4d9b276d8e76.zip
Added solutions for 032
Diffstat (limited to 'challenge-032')
-rw-r--r--challenge-032/rage311/perl5/ch-1.pl62
-rw-r--r--challenge-032/rage311/perl5/ch-2.pl50
-rw-r--r--challenge-032/rage311/perl5/example.txt6
-rw-r--r--challenge-032/rage311/perl5/example2.txt6
-rw-r--r--challenge-032/rage311/perl5/example3.txt6
5 files changed, 130 insertions, 0 deletions
diff --git a/challenge-032/rage311/perl5/ch-1.pl b/challenge-032/rage311/perl5/ch-1.pl
new file mode 100644
index 0000000000..b082d1fc7e
--- /dev/null
+++ b/challenge-032/rage311/perl5/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+# 032-1
+# 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.
+
+use 5.030;
+use strict;
+use warnings;
+
+use Getopt::Long;
+
+my $csv = 0;
+GetOptions csv => \$csv;
+
+undef $/;
+my %count;
+
+# file names
+if (@ARGV) {
+ while (my $file = shift @ARGV) {
+ open my $fh, '<', $file or warn "$!" and next;
+ $count{$_}++ for split /\n/, <$fh>;
+ }
+}
+# STDIN
+else {
+ $count{$_}++ for split /\n/, <<>>;
+}
+
+say for map {
+ join $csv ? ',' : "\t", $_, $count{$_}
+ } sort {
+ $count{$b} <=> $count{$a}
+ } keys %count;
+
+
+__DATA__
+
+Output:
+
+cat example.txt | perl ch-1.pl
+apple 3
+cherry 2
+banana 1
+
+cat example.txt | perl ch-1.pl --csv
+apple,3
+cherry,2
+banana,1
+
+perl ch-1.pl example.txt
+apple 3
+cherry 2
+banana 1
+
+perl ch-1.pl --csv *.txt
+apple,9
+cherry,6
+banana,3
+
diff --git a/challenge-032/rage311/perl5/ch-2.pl b/challenge-032/rage311/perl5/ch-2.pl
new file mode 100644
index 0000000000..6390418598
--- /dev/null
+++ b/challenge-032/rage311/perl5/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+# 032-2
+# Write a function that takes a hashref where the keys are labels and the
+# values are integer or floating point values. Generate a bar graph of the data
+# and display it to stdout.
+
+use 5.030;
+use strict;
+use warnings;
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+sub graph ($hr, $by_label = 0) {
+ my $max_len = length((sort { length $b <=> length $a } keys %$hr)[0]);
+ printf "%*s | %s\n", $max_len, $_, '#' x $hr->{$_} for
+ sort { $by_label ? $a cmp $b : $hr->{$b} <=> $hr->{$a} } keys %$hr;
+}
+
+graph { apple => 3, cherry => 2, banana => 1 };
+print "\n";
+graph { apple => 14, cherry => 0, banana => 21, durian => 7 }, 1;
+print "\n";
+graph { apple => -1, cherry => 40, banana => 12, watermelon => 7 }, 1;
+print "\n";
+graph { apple => 1, cherry => 9, banana => 7 }, 0;
+
+
+__DATA__
+
+Output:
+
+ apple | ###
+cherry | ##
+banana | #
+
+ apple | ##############
+banana | #####################
+cherry |
+durian | #######
+
+ apple |
+ banana | ############
+ cherry | ########################################
+watermelon | #######
+
+cherry | #########
+banana | #######
+ apple | #
+
diff --git a/challenge-032/rage311/perl5/example.txt b/challenge-032/rage311/perl5/example.txt
new file mode 100644
index 0000000000..00fe021a08
--- /dev/null
+++ b/challenge-032/rage311/perl5/example.txt
@@ -0,0 +1,6 @@
+apple
+banana
+apple
+cherry
+cherry
+apple
diff --git a/challenge-032/rage311/perl5/example2.txt b/challenge-032/rage311/perl5/example2.txt
new file mode 100644
index 0000000000..00fe021a08
--- /dev/null
+++ b/challenge-032/rage311/perl5/example2.txt
@@ -0,0 +1,6 @@
+apple
+banana
+apple
+cherry
+cherry
+apple
diff --git a/challenge-032/rage311/perl5/example3.txt b/challenge-032/rage311/perl5/example3.txt
new file mode 100644
index 0000000000..00fe021a08
--- /dev/null
+++ b/challenge-032/rage311/perl5/example3.txt
@@ -0,0 +1,6 @@
+apple
+banana
+apple
+cherry
+cherry
+apple