aboutsummaryrefslogtreecommitdiff
path: root/challenge-032
diff options
context:
space:
mode:
authorndelucca <nazadelucca@gmail.com>2019-10-30 22:07:37 -0300
committerndelucca <nazadelucca@gmail.com>2019-10-30 22:07:37 -0300
commitff7e7496958eb37e734a03a95f1ad64e381ef780 (patch)
treef3b78ea171190581bc75b2ff7afce2c32f882229 /challenge-032
parent8b8f31ae1cd3bafe336df787e5a79b3a34730647 (diff)
downloadperlweeklychallenge-club-ff7e7496958eb37e734a03a95f1ad64e381ef780.tar.gz
perlweeklychallenge-club-ff7e7496958eb37e734a03a95f1ad64e381ef780.tar.bz2
perlweeklychallenge-club-ff7e7496958eb37e734a03a95f1ad64e381ef780.zip
challenge-032/ndelucca
Diffstat (limited to 'challenge-032')
-rw-r--r--challenge-032/ndelucca/perl5/ch-1.pl44
-rw-r--r--challenge-032/ndelucca/perl5/ch-2.pl67
-rw-r--r--challenge-032/ndelucca/perl5/example1.txt6
-rw-r--r--challenge-032/ndelucca/perl5/example2.txt6
4 files changed, 123 insertions, 0 deletions
diff --git a/challenge-032/ndelucca/perl5/ch-1.pl b/challenge-032/ndelucca/perl5/ch-1.pl
new file mode 100644
index 0000000000..b148e06373
--- /dev/null
+++ b/challenge-032/ndelucca/perl5/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+# 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.
+
+# 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 %list = ();
+my $csv = shift if @ARGV && grep { $_ eq "-csv" } @ARGV;
+
+chomp,$list{ $_ }++ while(<>);
+
+my $format = $csv ? "%s,%s\n" : "%s\t%s\n";
+
+map { printf $format,$_,$list{$_} } sort { $list{$b} <=> $list{$a} } keys %list
+
+# perl ch-1.pl example1.txt
+
+# apple 3
+# cherry 2
+# banana 1
+
+# perl ch-1.pl example1.txt example2.txt
+
+# apple 6
+# cherry 4
+# banana 2
+
+# perl ch-1.pl -csv example1.txt example2.txt
+
+# apple,6
+# cherry,4
+# banana,2
diff --git a/challenge-032/ndelucca/perl5/ch-2.pl b/challenge-032/ndelucca/perl5/ch-2.pl
new file mode 100644
index 0000000000..36ec4c1bcd
--- /dev/null
+++ b/challenge-032/ndelucca/perl5/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+# 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;
+
+sub generate_bar_graph{
+
+ my $dataRef = shift;
+ my $sort_bit = shift || 2;
+
+ my @sorted_by_value = sort { $dataRef->{$b} <=> $dataRef->{$a} } keys(%$dataRef);
+ # We allways get the max value so we can graph in the same scale
+ # Sacrifice speed (when asked to sort by label, we do it twice) over functionality
+ my $max = $dataRef->{$sorted_by_value[0]};
+
+ my @sorted_keys = @sorted_by_value;
+ @sorted_keys = sort { $a cmp $b } keys(%$dataRef) if $sort_bit == 1;
+
+ print "$_\t".bar($dataRef->{$_},$max)."\n" for @sorted_keys;
+
+}
+
+sub bar{
+ my ($value,$max) = @_;
+ my $scale = 60;
+ my $bar_size = $value*$scale/$max;
+
+ return "#"x$bar_size;
+}
+
+# By Value
+generate_bar_graph({
+ apple => 35.5,
+ cherry => 12,
+ banana => 60.9
+});
+
+# banana ############################################################
+# apple ##################################
+# cherry ###########
+
+generate_bar_graph({
+ apple => 35.5,
+ cherry => 12,
+ banana => 60.9
+},1);
+
+# By Label
+# apple ##################################
+# banana ############################################################
+# cherry ###########
diff --git a/challenge-032/ndelucca/perl5/example1.txt b/challenge-032/ndelucca/perl5/example1.txt
new file mode 100644
index 0000000000..00fe021a08
--- /dev/null
+++ b/challenge-032/ndelucca/perl5/example1.txt
@@ -0,0 +1,6 @@
+apple
+banana
+apple
+cherry
+cherry
+apple
diff --git a/challenge-032/ndelucca/perl5/example2.txt b/challenge-032/ndelucca/perl5/example2.txt
new file mode 100644
index 0000000000..00fe021a08
--- /dev/null
+++ b/challenge-032/ndelucca/perl5/example2.txt
@@ -0,0 +1,6 @@
+apple
+banana
+apple
+cherry
+cherry
+apple