aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-10-29 10:50:06 +0000
committerGitHub <noreply@github.com>2019-10-29 10:50:06 +0000
commite68cc4e87b70a31fe8bb7a08a1d6aaa3026c14b5 (patch)
tree487d8ddd682becbf6fc07959de589efe9f0b8bdd
parent071037c286248033ff5bf07ba115e789229f4832 (diff)
parent0cc2991ce6baa5b992d521700217bfb065b69010 (diff)
downloadperlweeklychallenge-club-e68cc4e87b70a31fe8bb7a08a1d6aaa3026c14b5.tar.gz
perlweeklychallenge-club-e68cc4e87b70a31fe8bb7a08a1d6aaa3026c14b5.tar.bz2
perlweeklychallenge-club-e68cc4e87b70a31fe8bb7a08a1d6aaa3026c14b5.zip
Merge pull request #867 from tagg/branch-for-challenge-032
Solutions to challenge 032
-rwxr-xr-xchallenge-032/lars-thegler/perl5/ch-1.pl41
-rwxr-xr-xchallenge-032/lars-thegler/perl5/ch-2.pl50
2 files changed, 91 insertions, 0 deletions
diff --git a/challenge-032/lars-thegler/perl5/ch-1.pl b/challenge-032/lars-thegler/perl5/ch-1.pl
new file mode 100755
index 0000000000..57d9fc59ea
--- /dev/null
+++ b/challenge-032/lars-thegler/perl5/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+
+use Modern::Perl;
+
+# Task #1
+# Contributed by Neil Bowers
+# 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 Getopt::Long;
+GetOptions("csv" => \my $csv) or die;
+
+my %count;
+while (<>) {
+ chomp;
+ $count{$_}++;
+}
+
+my $delim = $csv ? ',' : "\t";
+for my $k (sort keys %count) {
+ say "$k$delim$count{$k}";
+} \ No newline at end of file
diff --git a/challenge-032/lars-thegler/perl5/ch-2.pl b/challenge-032/lars-thegler/perl5/ch-2.pl
new file mode 100755
index 0000000000..c39d891648
--- /dev/null
+++ b/challenge-032/lars-thegler/perl5/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+use Modern::Perl;
+
+# Task #2
+# Contributed by Neil Bowers
+# 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.
+
+use List::Util 'max';
+use Getopt::Long;
+GetOptions( "val" => \my $order_by_values ) or die;
+
+my $data = { apple => 3, cherry => 2, banana => 1 };
+
+say generate_bar_graph( $data, $order_by_values );
+
+sub generate_bar_graph
+{
+ my $data = shift;
+ my $order_by_values = shift // 0;
+ my $max_bar_length = 12; # length of the longest bar
+
+ # determine highest value
+ my $max_value = max values $data->%*;
+
+ # determine longest label
+ my $max_length = max map { length $_ } keys $data->%*;
+
+ my $graph;
+ for my $label ( sort { $order_by_values ? $data->{$b} <=> $data->{$a} : $a cmp $b } keys $data->%* ) {
+ my $value = $data->{$label};
+ my $bar_length = int( $value / $max_value * $max_bar_length );
+ $graph .= sprintf "%*s | %s\n", $max_length, $label, '#' x $bar_length;
+ }
+
+ return $graph;
+}