diff options
| author | Ysmael Ebreo <Ysmael.Ebreo@latticesemi.com> | 2019-10-28 23:04:34 +0800 |
|---|---|---|
| committer | Ysmael Ebreo <Ysmael.Ebreo@latticesemi.com> | 2019-10-28 23:04:34 +0800 |
| commit | 0b94465535c2e5d5bf31022cf583b021add9fd59 (patch) | |
| tree | d204d9c69636ae8c2910aa77401eda306acb0546 /challenge-032 | |
| parent | 2233bf4b1c5a282095ae34b4bfb277a8b6da0f58 (diff) | |
| parent | 1d714bf7a33bb75cce5538be8e50a22a8dcb2595 (diff) | |
| download | perlweeklychallenge-club-0b94465535c2e5d5bf31022cf583b021add9fd59.tar.gz perlweeklychallenge-club-0b94465535c2e5d5bf31022cf583b021add9fd59.tar.bz2 perlweeklychallenge-club-0b94465535c2e5d5bf31022cf583b021add9fd59.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-032')
| -rw-r--r-- | challenge-032/dave-cross/perl5/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-032/dave-cross/perl5/ch-2.pl | 46 | ||||
| -rw-r--r-- | challenge-032/dave-cross/perl5/input | 6 | ||||
| -rw-r--r-- | challenge-032/simon-proctor/all.csv | 7 | ||||
| -rw-r--r-- | challenge-032/simon-proctor/all.txt | 7 | ||||
| -rw-r--r-- | challenge-032/simon-proctor/large-test.txt | 100 | ||||
| -rw-r--r-- | challenge-032/simon-proctor/medium-test.txt | 50 | ||||
| -rw-r--r-- | challenge-032/simon-proctor/perl6/ch-1.p6 | 36 | ||||
| -rw-r--r-- | challenge-032/simon-proctor/perl6/ch-2.p6 | 84 | ||||
| -rw-r--r-- | challenge-032/simon-proctor/small-test.txt | 6 |
10 files changed, 364 insertions, 0 deletions
diff --git a/challenge-032/dave-cross/perl5/ch-1.pl b/challenge-032/dave-cross/perl5/ch-1.pl new file mode 100644 index 0000000000..42a706b7ed --- /dev/null +++ b/challenge-032/dave-cross/perl5/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +my $csv; +if ($ARGV[0] eq '-csv') { + shift; + $csv = 1; +} + +my %count; + +while (<>) { + chomp; + $count{$_}++; +} + +for (sort { $count{$b} <=> $count{$a} } keys %count) { + say $_, ($csv ? ',' : "\t"), $count{$_}; +} diff --git a/challenge-032/dave-cross/perl5/ch-2.pl b/challenge-032/dave-cross/perl5/ch-2.pl new file mode 100644 index 0000000000..37fbda8759 --- /dev/null +++ b/challenge-032/dave-cross/perl5/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +use List::Util 'max'; +use Term::ReadKey; + +my $input = { + apple => 3, + cherry => 2, + banana => 1, +}; + +say "Sort by keys:"; +say text_bar($input); +say "\nSort by values:"; +say text_bar($input, 'v'); + +sub text_bar { + my ($data) = @_; + + my $sort_by_keys = 1; + if (lc ($_[1] // '') eq 'v') { + $sort_by_keys = 0; + } + + my ($width) = GetTerminalSize; + say $width; + + my $keylen = max map { length } keys %$data; + my $maxval = max values %$data; + + my $step = int($width - $keylen - 3) / $maxval; + + if ($sort_by_keys) { + for (sort keys %$data) { + printf "%${keylen}s : %s\n", $_, '#' x ($data->{$_} * $step); + } + } else{ + for (sort { $data->{$b} <=> $data->{$a} } keys %$data) { + printf "%${keylen}s : %s\n", $_, '#' x ($data->{$_} * $step); + } + } +} diff --git a/challenge-032/dave-cross/perl5/input b/challenge-032/dave-cross/perl5/input new file mode 100644 index 0000000000..00fe021a08 --- /dev/null +++ b/challenge-032/dave-cross/perl5/input @@ -0,0 +1,6 @@ +apple +banana +apple +cherry +cherry +apple diff --git a/challenge-032/simon-proctor/all.csv b/challenge-032/simon-proctor/all.csv new file mode 100644 index 0000000000..a9dc63c1c6 --- /dev/null +++ b/challenge-032/simon-proctor/all.csv @@ -0,0 +1,7 @@ +"apple",29 +"cherry",29 +"strawberry",28 +"lime",24 +"pear",23 +"lemon",22 +"banana",1 diff --git a/challenge-032/simon-proctor/all.txt b/challenge-032/simon-proctor/all.txt new file mode 100644 index 0000000000..1c38af6052 --- /dev/null +++ b/challenge-032/simon-proctor/all.txt @@ -0,0 +1,7 @@ +cherry 29 +apple 29 +strawberry 28 +lime 24 +pear 23 +lemon 22 +banana 1 diff --git a/challenge-032/simon-proctor/large-test.txt b/challenge-032/simon-proctor/large-test.txt new file mode 100644 index 0000000000..996a4625e3 --- /dev/null +++ b/challenge-032/simon-proctor/large-test.txt @@ -0,0 +1,100 @@ +apple +lemon +cherry +apple +cherry +strawberry +pear +cherry +pear +apple +strawberry +strawberry +cherry +lime +lemon +lemon +lemon +pear +cherry +apple +cherry +pear +lemon +pear +strawberry +apple +pear +pear +lime +strawberry +apple +strawberry +cherry +lemon +apple +lime +lemon +apple +apple +lemon +strawberry +strawberry +lime +lime +lime +strawberry +pear +lemon +apple +lemon +lime +pear +strawberry +cherry +strawberry +pear +strawberry +lime +pear +lemon +strawberry +apple +lime +lime +apple +pear +lime +pear +apple +lime +lemon +cherry +apple +lime +lime +cherry +apple +cherry +lime +cherry +cherry +pear +cherry +lemon +strawberry +pear +apple +lemon +cherry +lime +apple +cherry +pear +strawberry +cherry +apple +cherry +lemon +pear +apple diff --git a/challenge-032/simon-proctor/medium-test.txt b/challenge-032/simon-proctor/medium-test.txt new file mode 100644 index 0000000000..db5f561f55 --- /dev/null +++ b/challenge-032/simon-proctor/medium-test.txt @@ -0,0 +1,50 @@ +strawberry +cherry +pear +lemon +strawberry +strawberry +cherry +pear +pear +lemon +lemon +lemon +apple +cherry +apple +strawberry +strawberry +lemon +apple +strawberry +lime +cherry +cherry +strawberry +lime +apple +lime +strawberry +cherry +strawberry +strawberry +lime +strawberry +cherry +cherry +strawberry +apple +pear +lime +lemon +apple +apple +lime +lime +strawberry +pear +pear +lemon +cherry +lime diff --git a/challenge-032/simon-proctor/perl6/ch-1.p6 b/challenge-032/simon-proctor/perl6/ch-1.p6 new file mode 100644 index 0000000000..8253bf56a8 --- /dev/null +++ b/challenge-032/simon-proctor/perl6/ch-1.p6 @@ -0,0 +1,36 @@ +#!/usr/bin/env perl6 + +use v6; + +subset ValidFile of Str where *.IO.f; +my %*SUB-MAIN-OPTS = :named-anywhere; + +#| Print help text +multi sub MAIN( Bool :h($help) where so * ) { + say $*USAGE; +} + +#| Read date from standard out. +#| Returns a list of name and count sorted by count +multi sub MAIN( + Bool :$csv = False #= Output in CSV +) { + read-files( IO::CatHandle.new( $*IN ), $csv ); +} + +#| Given a list of filenames reads each in turn +multi sub MAIN( + *@files where all(@files) ~~ ValidFile, #= Files to read + Bool :$csv = False #= Output in CSV +) { + read-files( IO::CatHandle.new( @files ), $csv ); +} + +sub read-files( IO::CatHandle $files, $csv ) { + my %results := BagHash.new(); + %results{$_}++ for $files.lines; + my $k-dist = %results.keys.map( *.codes ).max; + my $v-dist = %results.values.map( *.codes ).max; + my $fmt = $csv ?? '"%s",%d' !! "% -{$k-dist+2}s%{$v-dist+2}d"; + .say for %results.sort( *.value <=> *.value ).reverse.map( { sprintf($fmt,$_.key,$_.value) } ); +} diff --git a/challenge-032/simon-proctor/perl6/ch-2.p6 b/challenge-032/simon-proctor/perl6/ch-2.p6 new file mode 100644 index 0000000000..6e497adf27 --- /dev/null +++ b/challenge-032/simon-proctor/perl6/ch-2.p6 @@ -0,0 +1,84 @@ +#!/usr/bin/env perl6 + +use v6; + +enum SortType <value key>; +enum SortDir <asc desc>; +subset ValidFile of Str where *.IO.f; +my %*SUB-MAIN-OPTS = :named-anywhere; + +#| Print help text +multi sub MAIN( Bool :h($help) where so * ) { + say $*USAGE; +} + +sub read-data( IO::CatHandle $files, Bool $csv ) { + my &line-reader = $csv ?? &parse-csv !! &parse-space-sep; + my %results := BagHash.new(); + $files.lines.map( &line-reader ).map( -> ( $k, $v ) { %results{$k} = $v } ); + return %results; +} + +#| Read data from standard out. +#| Prints a bar chart of the data +multi sub MAIN( + Bool :$csv = False, #= Input in CSV + SortType :$sort-type=value, #= Sort Type + SortDir :$sort-dir=desc, #= Sort Direction +) { + draw-graph( read-data( IO::CatHandle.new( $*IN ), $csv ), $sort-type, $sort-dir ); +} + +#| Given a list of filenames reads each in turn +multi sub MAIN( + *@files where all(@files) ~~ ValidFile, #= Files to read + Bool :$csv = False, #= Output in CSV + SortType :$sort-type=value, #= Sort Type + SortDir :$sort-dir=desc, #= Sort Direction +) { + draw-graph( read-data( IO::CatHandle.new( @files ), $csv ), $sort-type, $sort-dir ); +} + +sub draw-graph( %data, SortType $sort-type, SortDir $sort-dir ) { + my $k-width = %data.keys.map(*.codes).max; + my $max-val = %data.values.max; + my $screen-width = get-screen-width(); + + my &sorter = make-sorter( $sort-type, $sort-dir ); + + my $available = $screen-width - $k-width - 5; + .say for %data.sort( &sorter ).map( { sprintf( "% -{$k-width}s | %s", $_.key, get-bar( $available, $max-val, $_.value ) ) } ); +} + +sub make-sorter( SortType $sort-type, SortDir $sort-dir ) { + given $sort-dir { + when asc { + -> $a, $b { $a.^lookup($sort-type)($a) cmp $b.^lookup($sort-type)($b) } + } + when desc { + -> $a, $b { $b.^lookup($sort-type)($b) cmp $a.^lookup($sort-type)($a) } + } + } +} + +sub get-bar( Int $available, $max, $value ) { + '#' x ceiling( $available * ( $value / $max ) ); +} + +sub get-screen-width() { + run("tput","cols",:out).out.slurp.chomp; +} + +sub parse-space-sep( Str $line ) { + if ( my $match = $line ~~ m!^ (\S+) \s+ (\S+) $! ) { + return $match[0], $match[1]; + } + die "Line parser didn't work on $line"; +} + +sub parse-csv( Str $line ) { + if ( my $match = $line ~~ m!^ (\"?) (.+) $0 "," (.+) $! ) { #" Editor bug + return $match[1], $match[2]; + } + die "Lazy CSV parser didn't work on $line"; +} diff --git a/challenge-032/simon-proctor/small-test.txt b/challenge-032/simon-proctor/small-test.txt new file mode 100644 index 0000000000..00fe021a08 --- /dev/null +++ b/challenge-032/simon-proctor/small-test.txt @@ -0,0 +1,6 @@ +apple +banana +apple +cherry +cherry +apple |
