diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-11-04 00:49:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-11-04 00:49:33 +0000 |
| commit | 5862885deb83694ee6570ea070a86b3ce026ff93 (patch) | |
| tree | e25dcc434e36cdcad0f0502929684fd6d6f13c51 | |
| parent | 0969432420e2eeee07c806800ca830509309deda (diff) | |
| parent | 4fc9e274bb211854a366dc01803c3dd0a5992df4 (diff) | |
| download | perlweeklychallenge-club-5862885deb83694ee6570ea070a86b3ce026ff93.tar.gz perlweeklychallenge-club-5862885deb83694ee6570ea070a86b3ce026ff93.tar.bz2 perlweeklychallenge-club-5862885deb83694ee6570ea070a86b3ce026ff93.zip | |
Merge pull request #886 from jaldhar/challenge-032
Challenge 32 by Jaldhar H. Vyas
| -rw-r--r-- | challenge-032/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-032/jaldhar-h-vyas/perl5/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-032/jaldhar-h-vyas/perl5/ch-2.pl | 33 | ||||
| -rwxr-xr-x | challenge-032/jaldhar-h-vyas/perl6/ch-1.p6 | 27 | ||||
| -rwxr-xr-x | challenge-032/jaldhar-h-vyas/perl6/ch-2.p6 | 32 |
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); +} |
