diff options
| author | Prajith Ndz <prajithpalakkuda@gmail.com> | 2019-10-30 12:02:59 +0530 |
|---|---|---|
| committer | Prajith Ndz <prajithpalakkuda@gmail.com> | 2019-10-30 12:02:59 +0530 |
| commit | 44db085fde18bfcd21273fcbd595974f5252adcf (patch) | |
| tree | 1a2bac1f435e4f12a795acf221441b48d976b12c | |
| parent | 071037c286248033ff5bf07ba115e789229f4832 (diff) | |
| download | perlweeklychallenge-club-44db085fde18bfcd21273fcbd595974f5252adcf.tar.gz perlweeklychallenge-club-44db085fde18bfcd21273fcbd595974f5252adcf.tar.bz2 perlweeklychallenge-club-44db085fde18bfcd21273fcbd595974f5252adcf.zip | |
solutions for challenge-032
| -rw-r--r-- | challenge-032/prajith-p/perl5/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-032/prajith-p/perl5/ch-2.pl | 40 |
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-032/prajith-p/perl5/ch-1.pl b/challenge-032/prajith-p/perl5/ch-1.pl new file mode 100644 index 0000000000..b0459128a3 --- /dev/null +++ b/challenge-032/prajith-p/perl5/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw<say>; + +use Data::Dumper; + +my %counts = (); + +sub Count { + my $file = shift; + open(my $fh, '<', $file) or warn $!; + while (my $line = <$fh>) { + chomp $line; + next unless length $line; + $counts{$line} += 1; + } + close($fh); +} + +for my $file (@ARGV) { + Count($file); +} + +foreach my $k ( sort { $counts{$b} <=> $counts{$a}} keys %counts) { + say $k . ',' . $counts{$k}; +} diff --git a/challenge-032/prajith-p/perl5/ch-2.pl b/challenge-032/prajith-p/perl5/ch-2.pl new file mode 100644 index 0000000000..4d7c1b424a --- /dev/null +++ b/challenge-032/prajith-p/perl5/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +# https://alexwlchan.net/2018/05/ascii-bar-charts + +use List::Util qw<max>; +use utf8; +use feature qw<say>; + +binmode STDOUT, ":utf8"; + +sub divmod { + my $number = shift; + my $divisor = shift; + + my $remainder = $number % $divisor; + my $quotient = ($number - $remainder) / $divisor; + + return ($quotient, $remainder); +} + +my $data = { apple => 3, cherry => 2, banana => 1 }; + +my $max_value = max(values %{$data}); +my $increment = $max_value / 25; + +my $longest_label_length = max map { length($_) } keys %{$data}; + +foreach my $label (sort { $data->{$b} <=> $data->{$a}} keys %{$data}) { + my $count = $data->{$label}; + + my ($bar_chunks, $remainder) = divmod(($count * 8 / $increment), 8); + my $bar = '█' x $bar_chunks; + + if ( $remainder > 0 ) { + $bar .= chr(ord('█') + (8 - $remainder)); + } + $bar ||= '▏'; + my $fmt = "%-$longest_label_length" .'s ' . ' |' . "%-4d"; + say sprintf($fmt, $label, $count) . $bar; +} |
