From 0cc2991ce6baa5b992d521700217bfb065b69010 Mon Sep 17 00:00:00 2001 From: Lars Thegler Date: Tue, 29 Oct 2019 10:52:30 +0100 Subject: Solutions to challenge 032 --- challenge-032/lars-thegler/perl5/ch-1.pl | 41 ++++++++++++++++++++++++++ challenge-032/lars-thegler/perl5/ch-2.pl | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100755 challenge-032/lars-thegler/perl5/ch-1.pl create mode 100755 challenge-032/lars-thegler/perl5/ch-2.pl 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; +} -- cgit