aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2019-11-03 19:00:55 -0500
committerJaldhar H. Vyas <jaldhar@braincells.com>2019-11-03 19:00:55 -0500
commit4fc9e274bb211854a366dc01803c3dd0a5992df4 (patch)
tree716de98729c27b5bc62f82562c00bd67b8c6ced1
parent4ea534b8afcaa524e258936ac5eae51eaa5a2cc9 (diff)
downloadperlweeklychallenge-club-4fc9e274bb211854a366dc01803c3dd0a5992df4.tar.gz
perlweeklychallenge-club-4fc9e274bb211854a366dc01803c3dd0a5992df4.tar.bz2
perlweeklychallenge-club-4fc9e274bb211854a366dc01803c3dd0a5992df4.zip
Challenge 32 by Jaldhar H. Vyas
-rw-r--r--challenge-032/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-032/jaldhar-h-vyas/perl5/ch-1.pl42
-rwxr-xr-xchallenge-032/jaldhar-h-vyas/perl5/ch-2.pl33
-rwxr-xr-xchallenge-032/jaldhar-h-vyas/perl6/ch-1.p627
-rwxr-xr-xchallenge-032/jaldhar-h-vyas/perl6/ch-2.p632
5 files changed, 135 insertions, 0 deletions
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/, <STDIN>;
+}
+
+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);
+}