diff options
| author | Ruben Westerberg <drclaw@mac.com> | 2019-11-04 08:05:37 +1000 |
|---|---|---|
| committer | Ruben Westerberg <drclaw@mac.com> | 2019-11-04 08:05:37 +1000 |
| commit | b79c7509c16a1b77fde3de1a3bb94566705aea23 (patch) | |
| tree | c993071ad1ac74d398eda09c8eac4c6b1c392745 /challenge-032 | |
| parent | 3ed7a325a37048c0a466c001065157d5854823ab (diff) | |
| download | perlweeklychallenge-club-b79c7509c16a1b77fde3de1a3bb94566705aea23.tar.gz perlweeklychallenge-club-b79c7509c16a1b77fde3de1a3bb94566705aea23.tar.bz2 perlweeklychallenge-club-b79c7509c16a1b77fde3de1a3bb94566705aea23.zip | |
Added combined ch-1 and ch-2 solutions p5 & p6
Diffstat (limited to 'challenge-032')
| -rw-r--r-- | challenge-032/ruben-westerberg/README | 26 | ||||
| -rwxr-xr-x | challenge-032/ruben-westerberg/perl5/ch-1_and_2.pl | 34 | ||||
| -rwxr-xr-x | challenge-032/ruben-westerberg/perl6/ch-1_and_2.p6 | 27 | ||||
| -rw-r--r-- | challenge-032/ruben-westerberg/test.data | 6 |
4 files changed, 83 insertions, 10 deletions
diff --git a/challenge-032/ruben-westerberg/README b/challenge-032/ruben-westerberg/README index 2ca4621c32..967735670b 100644 --- a/challenge-032/ruben-westerberg/README +++ b/challenge-032/ruben-westerberg/README @@ -1,12 +1,18 @@ Solution by Ruben Westerberg -ch-1.pl and ch-1.p6 -=== -Run the program with two command line arguments. First is the numerator, second is the denominator. Output will print successful result unless a divide by zero is detected. -If no arguments are provided numerator is set to 1 and denominator is set to 0 - -ch-2.pl and ch-2.p6 -=== -Run the program with two arguments. The first is the name of ther variable to create and the second is the value to assign. -Output is the name and value of the dynamicaly named variable -If no arguments are given a random name and value are created; +Challenge 1 and 2 contained in ch1.pl and ch1.p6 files. +Output is a histogram listing of repeated values. Numeric values and a graph are output + +Usage examples: + ch-1_and_2.p6 INPUTFILE + ch-1_and_2.pl INPUT FILE + + ch-1_and_2.p6 -vsort INPUTFILE + ch-1_and_2.pl -v INPUTFILE + +-v/-vsort will sort the entries by count value not key value + + + + + diff --git a/challenge-032/ruben-westerberg/perl5/ch-1_and_2.pl b/challenge-032/ruben-westerberg/perl5/ch-1_and_2.pl new file mode 100755 index 0000000000..23c05b0ef9 --- /dev/null +++ b/challenge-032/ruben-westerberg/perl5/ch-1_and_2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Getopt::Std; +use List::Util; +my %options=(); +getopts("v", \%options); + +my %data; + +while (<>) { + chomp; + $data{$_}++; +} + +histogram(\%data,$options{v},0); +histogram(\%data,$options{v},1); + +sub histogram { + my $h=shift; + my $valueSort=shift; + my $chart=shift; + my @keys= sort keys %$h;# { length $b > length $a} keys %$h; + my $maxKeyLength=List::Util::max map { length $_} keys %$h; + print "\n"; + if ($valueSort) { + @keys=sort { $$h{$b} > $$h{$a} } keys %$h; + } + for (@keys) { + my $v=$$h{$_}; + $v= "#" x $$h{$_} if $chart; + printf("%".$maxKeyLength."s| %s\n", $_, $v); + } +} diff --git a/challenge-032/ruben-westerberg/perl6/ch-1_and_2.p6 b/challenge-032/ruben-westerberg/perl6/ch-1_and_2.p6 new file mode 100755 index 0000000000..368563c978 --- /dev/null +++ b/challenge-032/ruben-westerberg/perl6/ch-1_and_2.p6 @@ -0,0 +1,27 @@ +#!/usr/bin/env perl6 + +sub MAIN(*@files,:$vsort=False) { + my %data; + + for @files { + for .IO.lines() { + %data{$_}++; + } + } + histogram(%data,$vsort,False); + histogram(%data,$vsort,True); +} + +sub histogram(%h,$valueSort,$chart) { + my @keys=%h.keys.sort;#(*.chars < *.chars); + my $maxKeyLength=@keys>>.chars.max; + put ""; + if ($valueSort) { + @keys=%h.keys.sort(-> $a,$b {%h{$a} < %h{$b}}); + } + for @keys { + my $v=%h{$_}.Str; + $v="#" x %h{$_} if $chart; + printf("%"~$maxKeyLength~"s| %s\n",$_,$v); + } +} diff --git a/challenge-032/ruben-westerberg/test.data b/challenge-032/ruben-westerberg/test.data new file mode 100644 index 0000000000..fe73137577 --- /dev/null +++ b/challenge-032/ruben-westerberg/test.data @@ -0,0 +1,6 @@ +banana +apple +apple +cherry +cherry +cherry |
