aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-11-04 00:46:57 +0000
committerGitHub <noreply@github.com>2019-11-04 00:46:57 +0000
commit1a310e1027b0886a22ff824beeeb5411f6de1b13 (patch)
tree6aa2116625b8a9ca9b3e3d4e75d333472f99b143
parentb532cfb8efdd4bea94b8ee2f5475cd87012ca9bb (diff)
parent111bfb0975070659e01a7fecb03b902d007fdfb4 (diff)
downloadperlweeklychallenge-club-1a310e1027b0886a22ff824beeeb5411f6de1b13.tar.gz
perlweeklychallenge-club-1a310e1027b0886a22ff824beeeb5411f6de1b13.tar.bz2
perlweeklychallenge-club-1a310e1027b0886a22ff824beeeb5411f6de1b13.zip
Merge pull request #885 from dcw803/master
my solutions for challenge 32.. done fast and dirty
-rw-r--r--challenge-032/duncan-c-white/README49
-rwxr-xr-xchallenge-032/duncan-c-white/perl5/ch-1.pl52
-rwxr-xr-xchallenge-032/duncan-c-white/perl5/ch-2.pl61
3 files changed, 155 insertions, 7 deletions
diff --git a/challenge-032/duncan-c-white/README b/challenge-032/duncan-c-white/README
index 5e1aee6697..a1e1d686c5 100644
--- a/challenge-032/duncan-c-white/README
+++ b/challenge-032/duncan-c-white/README
@@ -1,11 +1,46 @@
-Challenge 1: "Create a function to check divide by zero error without
- checking if the denominator is zero."
+Challenge 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.
-My notes: so eval then?
+So with the following input in file example.txt
+apple
+banana
+apple
+cherry
+cherry
+apple
-Challenge 2: "Create a script to demonstrate creating dynamic variable
- name, assign a value to the variable and finally print the
- variable. The variable name is passed as a command line argument."
+the script would display something like:
-My notes: so eval again?
+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
+
+My notes: Straightforward use of a hash.
+
+
+Challenge 2: "ASCII bar chart: 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.
+
+The input could be something like:
+
+$data = { apple => 3, cherry => 2, banana => 1 };
+generate_bar_graph($data);
+
+And would then generate something like this:
+
+ apple | ############
+cherry | ########
+banana | ####
+
+If you fancy then please try this as well: (a) the function could let you specify whether the chart should be ordered by (1) the labels, or (2) the values.
+
+My notes: easy, especially using a histogram module I wrote recently.
diff --git a/challenge-032/duncan-c-white/perl5/ch-1.pl b/challenge-032/duncan-c-white/perl5/ch-1.pl
new file mode 100755
index 0000000000..4361fc22b7
--- /dev/null
+++ b/challenge-032/duncan-c-white/perl5/ch-1.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+#
+# Challenge 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
+#
+# My notes: Straightforward use of a hash.
+#
+
+use v5.10; # to get "say"
+use strict;
+use warnings;
+use Getopt::Long;
+
+my $csv = 0;
+die "Usage: ch-1.pl [--csv] filter_args\n"
+ unless GetOptions( "csv" => \$csv );
+my $sep = $csv ? ',' : ' ';
+
+my %freq;
+while( <> )
+{
+ chomp;
+ $freq{$_}++;
+}
+
+foreach my $str (sort { $freq{$b} <=> $freq{$a} } keys %freq)
+{
+ my $v = $freq{$str};
+ say "$str$sep$v";
+}
diff --git a/challenge-032/duncan-c-white/perl5/ch-2.pl b/challenge-032/duncan-c-white/perl5/ch-2.pl
new file mode 100755
index 0000000000..27b1f29207
--- /dev/null
+++ b/challenge-032/duncan-c-white/perl5/ch-2.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+#
+# Challenge 2: "ASCII bar chart: 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.
+#
+# The input could be something like:
+#
+# $data = { apple => 3, cherry => 2, banana => 1 };
+# generate_bar_graph($data);
+#
+# And would then generate something like this:
+#
+# apple | ############
+# cherry | ########
+# banana | ####
+#
+# If you fancy then please try this as well: (a) the function could let
+# you specify whether the chart should be ordered by (1) the labels, or
+# (2) the values.
+#
+# My notes: easy, especially using a histogram module I wrote recently.
+#
+
+use v5.10; # for "say"
+use strict;
+use warnings;
+use Data::Dumper;
+use List::Util qw(max);
+use Function::Parameters;
+
+#
+# my $str = generate_bar_graph( $data, $width, $bylabels );
+# Generate a bar graph from the frequency data in $data (a hashref),
+# where the maximum frequency occupies $width characters,
+# either sorted by labels (if $bylabels is true) or
+# by numeric values, returning a printable string form.
+#
+fun generate_bar_graph( $data, $width, $bylabels )
+{
+ my $maxv = max( values %$data );
+ my $fieldw = max( map { length($_) } keys %$data );
+ my $scale = int($width/$maxv);
+ #die "debug: g_b_g: maxv=$maxv, fieldw=$fieldw, scale=$scale\n";
+ my @str;
+ my $fw = $fieldw+2;
+ my @data = sort {
+ $bylabels ? $a cmp $b : $data->{$b} <=> $data->{$a}
+ } keys %$data;
+ foreach my $k (@data)
+ {
+ my $v = $data->{$k};
+ my $row = sprintf( '%'.$fw.'s | ', $k ). ('#' x ($v*$scale));
+ push @str, $row;
+ }
+ return join("\n", @str );
+}
+
+my $data = { apple => 3, cherry => 2, banana => 1 };
+my $str = generate_bar_graph( $data, 40, 1 );
+say $str;