From 4fc9e274bb211854a366dc01803c3dd0a5992df4 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Sun, 3 Nov 2019 19:00:55 -0500 Subject: Challenge 32 by Jaldhar H. Vyas --- challenge-032/jaldhar-h-vyas/blog.txt | 1 + challenge-032/jaldhar-h-vyas/perl5/ch-1.pl | 42 ++++++++++++++++++++++++++++++ challenge-032/jaldhar-h-vyas/perl5/ch-2.pl | 33 +++++++++++++++++++++++ challenge-032/jaldhar-h-vyas/perl6/ch-1.p6 | 27 +++++++++++++++++++ challenge-032/jaldhar-h-vyas/perl6/ch-2.p6 | 32 +++++++++++++++++++++++ 5 files changed, 135 insertions(+) create mode 100644 challenge-032/jaldhar-h-vyas/blog.txt create mode 100755 challenge-032/jaldhar-h-vyas/perl5/ch-1.pl create mode 100755 challenge-032/jaldhar-h-vyas/perl5/ch-2.pl create mode 100755 challenge-032/jaldhar-h-vyas/perl6/ch-1.p6 create mode 100755 challenge-032/jaldhar-h-vyas/perl6/ch-2.p6 diff --git a/challenge-032/jaldhar-h-vyas/blog.txt b/challenge-032/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..9016569083 --- /dev/null +++ b/challenge-032/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2019/11/perl_weekly_challenge_week_32.html diff --git a/challenge-032/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-032/jaldhar-h-vyas/perl5/ch-1.pl new file mode 100755 index 0000000000..e2f8179b4e --- /dev/null +++ b/challenge-032/jaldhar-h-vyas/perl5/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; +use English qw/ -no_match_vars /; +use Getopt::Long; + +my $csv = undef; + +GetOptions('csv' => \$csv); + +my @contents; +if (scalar @ARGV) { + for my $file (@ARGV) { + open my $fh, '<', $file or die "$OS_ERROR\n"; + local $INPUT_RECORD_SEPARATOR = undef; + push @contents, split /\n/, <$fh>; + close $fh; + } +} else { + local $INPUT_RECORD_SEPARATOR = undef; + push @contents, split /\n/, ; +} + +my %totals; +for my $item (@contents) { + chomp $item; + $totals{$item}++; +} + +if ($csv) { + for my $total (sort keys %totals) { + say "$total,$totals{$total}"; + } +} else { + my $width = + length ((sort {length $b <=> length $a} keys %totals)[0]); + + for my $total (sort keys %totals) { + printf("% -*s %s\n", $width, $total, $totals{$total}); + } +} diff --git a/challenge-032/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-032/jaldhar-h-vyas/perl5/ch-2.pl new file mode 100755 index 0000000000..bedcc5b13d --- /dev/null +++ b/challenge-032/jaldhar-h-vyas/perl5/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; +use constant SCALE => 4; + +sub generate_bar_graph { + my ($data, $bylabels) = @_; + + my @labels = sort { $data->{$b} <=> $data->{$a}} keys %{$data}; + my $smallest = $data->{$labels[$#labels]}; + + if (defined $bylabels) { + @labels = sort @labels; + } + + my $width = length ((sort {length $b <=> length $a} @labels)[0]); + my $bar_graph = q{}; + + for my $label (@labels) { + my $bar = ($data->{$label} / $smallest) * SCALE; + if ($data->{$label} % $smallest) { + $bar += SCALE / 2; + } + $bar_graph .= sprintf("% -*s | %s\n", $width, $label, '#' x $bar); + } + + return $bar_graph; +} + +my $data = { apple => 11, cherry => 5, banana => 2 }; + +print generate_bar_graph($data, 1); \ No newline at end of file diff --git a/challenge-032/jaldhar-h-vyas/perl6/ch-1.p6 b/challenge-032/jaldhar-h-vyas/perl6/ch-1.p6 new file mode 100755 index 0000000000..c84d18b956 --- /dev/null +++ b/challenge-032/jaldhar-h-vyas/perl6/ch-1.p6 @@ -0,0 +1,27 @@ +#!/usr/bin/perl6 + +sub MAIN( + Bool :$csv, #= output results in CSV format + *@files +) { + my %totals; + + if @files.elems { + for @files -> $file { + $file.IO.lines.map({ %totals{$_}++; }); + } + } else { + $*IN.lines.map({ %totals{$_}++; }); + } + + if $csv { + %totals.keys.sort.map({ say "$_,%totals{$_}"; }); + } else { + my $width = + %totals.keys.sort({$^b.chars <=> $^a.chars}).first.chars; + + %totals.keys.sort.map({ + printf("% -*s %s\n", $width, $_, %totals{$_}); + }); + } +} \ No newline at end of file diff --git a/challenge-032/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-032/jaldhar-h-vyas/perl6/ch-2.p6 new file mode 100755 index 0000000000..321f15cf9f --- /dev/null +++ b/challenge-032/jaldhar-h-vyas/perl6/ch-2.p6 @@ -0,0 +1,32 @@ +#!/usr/bin/perl6 + +sub generate_bar_graph(%data, Bool $bylabels = False) { + constant $SCALE = 4; + + my @labels = %data.keys.sort({ %data{$^b} <=> %data{$^a} }); + my $smallest = %data{@labels[@labels.end]}; + + if ($bylabels) { + @labels = @labels.sort; + } + + my $width = @labels.sort({$^b.chars <=> $^a.chars}).first.chars; + my $bar_graph = q{}; + + for @labels -> $label { + my $bar = (%data{$label} / $smallest) * $SCALE; + if %data{$label} !%% $smallest { + $bar += $SCALE / 2; + } + $bar_graph ~= sprintf("% -*s | %s\n", $width, $label, '#' x $bar); + } + + return $bar_graph; +} + + +sub MAIN() { + my %data = apple => 11, cherry => 5, banana => 2; + + print generate_bar_graph(%data); +} -- cgit