aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2019-10-29 08:21:03 -0300
committerandrezgz <andrezgz@gmail.com>2019-10-29 08:21:03 -0300
commit21660918a2e82887725a573973dc99c5025f2d9d (patch)
treec13c10ede85d93975b96c5bf98ba5c6f713b688a
parente393eee06601fa3cebae7167e486323ef8fe3ca9 (diff)
downloadperlweeklychallenge-club-21660918a2e82887725a573973dc99c5025f2d9d.tar.gz
perlweeklychallenge-club-21660918a2e82887725a573973dc99c5025f2d9d.tar.bz2
perlweeklychallenge-club-21660918a2e82887725a573973dc99c5025f2d9d.zip
challenge-032 andrezgz solution
-rw-r--r--challenge-032/andrezgz/perl5/ch-1.pl67
-rw-r--r--challenge-032/andrezgz/perl5/ch-2.pl73
2 files changed, 140 insertions, 0 deletions
diff --git a/challenge-032/andrezgz/perl5/ch-1.pl b/challenge-032/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..4d63fc361c
--- /dev/null
+++ b/challenge-032/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-032/
+# Task #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
+
+use strict;
+use warnings;
+
+my $csv = @ARGV && $ARGV[0] eq '-csv' ? 1 : 0;
+shift @ARGV if $csv;
+
+print "Ctrl + D to finish\n" unless @ARGV; # when entering input from STDIN
+
+my %entries;
+chomp, $entries{$_}++ while (<>); # count instances
+
+print "Instances count:\n";
+print join "\n", # Print a line
+ map { sprintf '%s%s%d', # composed by
+ $_, # the entry name,
+ $csv ? ',' : "\t", # the separator (comma or tab),
+ $entries{$_} # and the number of instances
+ }
+ reverse sort { $entries{$a} <=> $entries{$b} } # from the sorted by number of instances (desc)
+ keys %entries; # entry list
+
+print "\n";
+
+__END__
+ ./ch-1.pl
+Ctrl + D to finish
+apple
+cherry
+apple
+Instances count:
+apple 2
+cherry 1
+
+./ch-1.pl w1.txt
+Instances count:
+apple 3
+cherry 2
+banana 1
+
+./ch-1.pl -csv example.txt
+Instances count:
+apple,3
+cherry,2
+banana,1
diff --git a/challenge-032/andrezgz/perl5/ch-2.pl b/challenge-032/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..cecd15ab99
--- /dev/null
+++ b/challenge-032/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-032/
+# Task #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.
+
+use strict;
+use warnings;
+
+use List::Util qw/max/;
+
+my $source = { apple => 3, cherry => 2, banana => 1, orange => 2.5 };
+
+print "Sorted by name\n";
+generate_bar_graph($source,{sort => 'labels' });
+
+print "Sorted by value\n";
+generate_bar_graph($source,{sort => 'values' });
+
+sub generate_bar_graph {
+ my $data = shift;
+ my $args = shift;
+
+ my @sorted_keys = keys %$data;
+ if ($args->{sort} eq 'values') {
+ # keys sorted by values (desc)
+ @sorted_keys = reverse sort { $data->{$a} <=> $data->{$b} } @sorted_keys;
+ }
+ else{
+ # keys sorted (asc)
+ @sorted_keys = sort @sorted_keys;
+ }
+
+ my $inc = 50 / max (values %$data); # percentage increment
+ print join "\n",
+ map { sprintf '%-10s| %s',
+ $_,
+ '#' x ($data->{$_} * $inc)
+ }
+ @sorted_keys;
+
+ print "\n";
+ return;
+}
+
+__END__
+
+./ch-2.pl
+Sorted by name
+apple | ##################################################
+banana | ################
+cherry | #################################
+orange | #########################################
+Sorted by value
+apple | ##################################################
+orange | #########################################
+cherry | #################################
+banana | ################