From d0d0ee686a11878ec3a7615ba6822e5758db8159 Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Mon, 28 Oct 2019 16:56:50 +0000 Subject: Remove debugging --- challenge-032/dave-cross/perl5/ch-2.pl | 1 - 1 file changed, 1 deletion(-) (limited to 'challenge-032') diff --git a/challenge-032/dave-cross/perl5/ch-2.pl b/challenge-032/dave-cross/perl5/ch-2.pl index 37fbda8759..5c786b2721 100644 --- a/challenge-032/dave-cross/perl5/ch-2.pl +++ b/challenge-032/dave-cross/perl5/ch-2.pl @@ -27,7 +27,6 @@ sub text_bar { } my ($width) = GetTerminalSize; - say $width; my $keylen = max map { length } keys %$data; my $maxval = max values %$data; -- cgit From 7f57699a91617338e14a979c3bba80688682ee8e Mon Sep 17 00:00:00 2001 From: holli-holzer Date: Mon, 28 Oct 2019 19:24:45 +0100 Subject: 2 in 1 Solution Markus Holzer --- challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 | 26 ++++++++++++++++++++++++ challenge-032/markus-holzer/perl6/simpsons.txt | 10 +++++++++ 2 files changed, 36 insertions(+) create mode 100644 challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 create mode 100644 challenge-032/markus-holzer/perl6/simpsons.txt (limited to 'challenge-032') diff --git a/challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 b/challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 new file mode 100644 index 0000000000..0941012c55 --- /dev/null +++ b/challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 @@ -0,0 +1,26 @@ +my %*SUB-MAIN-OPTS = :named-anywhere; + +multi sub MAIN( *@files, Bool :$csv, Bool :$graph, Bool :$sort-by-label ) +{ + CATCH { return .message.say } + + my @words = @files + ?? @files.map( |*.IO.lines ) + !! |$*ARGFILES.lines; + + my $weights = Bag.new( @words ); + my $lngst = max $weights.keys.map( *.chars ); + + my $format = $csv ?? "%s, %s" !! + $graph ?? "%{$lngst}s | %s " !! + "%-{$lngst}s %s " ; + + my &sorter = $sort-by-label + ?? { $^a.key cmp $^b.key } + !! { $^b.value <=> $^a.value }; + + .say for $weights + .sort( &sorter ) + .map({ .key => $graph ?? "#" x .value !! .value }) + .map({ sprintf $format, .key, .value }); +} \ No newline at end of file diff --git a/challenge-032/markus-holzer/perl6/simpsons.txt b/challenge-032/markus-holzer/perl6/simpsons.txt new file mode 100644 index 0000000000..bb3179d47e --- /dev/null +++ b/challenge-032/markus-holzer/perl6/simpsons.txt @@ -0,0 +1,10 @@ +Marge +Homer +Bart +Maggie +Homer +Bart +Homer +Marge +Homer +Bart \ No newline at end of file -- cgit From ddc3f01b4574cc77300e6efae902e76bab6b1f59 Mon Sep 17 00:00:00 2001 From: holli-holzer Date: Mon, 28 Oct 2019 19:24:45 +0100 Subject: 2 in 1 Solution Markus Holzer --- challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 | 26 ++++++++++++++++++++++++ challenge-032/markus-holzer/perl6/simpsons.txt | 10 +++++++++ challenge-032/markus-holzer/perl6/test.bat | 6 ++++++ 3 files changed, 42 insertions(+) create mode 100644 challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 create mode 100644 challenge-032/markus-holzer/perl6/simpsons.txt create mode 100644 challenge-032/markus-holzer/perl6/test.bat (limited to 'challenge-032') diff --git a/challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 b/challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 new file mode 100644 index 0000000000..0941012c55 --- /dev/null +++ b/challenge-032/markus-holzer/perl6/ch-1-and-2.pl6 @@ -0,0 +1,26 @@ +my %*SUB-MAIN-OPTS = :named-anywhere; + +multi sub MAIN( *@files, Bool :$csv, Bool :$graph, Bool :$sort-by-label ) +{ + CATCH { return .message.say } + + my @words = @files + ?? @files.map( |*.IO.lines ) + !! |$*ARGFILES.lines; + + my $weights = Bag.new( @words ); + my $lngst = max $weights.keys.map( *.chars ); + + my $format = $csv ?? "%s, %s" !! + $graph ?? "%{$lngst}s | %s " !! + "%-{$lngst}s %s " ; + + my &sorter = $sort-by-label + ?? { $^a.key cmp $^b.key } + !! { $^b.value <=> $^a.value }; + + .say for $weights + .sort( &sorter ) + .map({ .key => $graph ?? "#" x .value !! .value }) + .map({ sprintf $format, .key, .value }); +} \ No newline at end of file diff --git a/challenge-032/markus-holzer/perl6/simpsons.txt b/challenge-032/markus-holzer/perl6/simpsons.txt new file mode 100644 index 0000000000..bb3179d47e --- /dev/null +++ b/challenge-032/markus-holzer/perl6/simpsons.txt @@ -0,0 +1,10 @@ +Marge +Homer +Bart +Maggie +Homer +Bart +Homer +Marge +Homer +Bart \ No newline at end of file diff --git a/challenge-032/markus-holzer/perl6/test.bat b/challenge-032/markus-holzer/perl6/test.bat new file mode 100644 index 0000000000..8da2aff480 --- /dev/null +++ b/challenge-032/markus-holzer/perl6/test.bat @@ -0,0 +1,6 @@ +@echo off +type simpsons.txt|call perl6 ch-1-and-2.pl6 +type simpsons.txt|call perl6 ch-1-and-2.pl6 --csv +call perl6 ch-1-and-2.pl6 simpsons.txt simpsons.txt +call perl6 ch-1-and-2.pl6 simpsons.txt --graph +call perl6 ch-1-and-2.pl6 --csv simpsons.txt --sort-by-label -- cgit From 9387b63ffdb79b3858cfea0df34c9d935ae146fd Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 28 Oct 2019 20:30:11 +0000 Subject: add solution to week 32 task 1 --- challenge-032/steven-wilson/perl5/ch-1.pl | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 challenge-032/steven-wilson/perl5/ch-1.pl (limited to 'challenge-032') diff --git a/challenge-032/steven-wilson/perl5/ch-1.pl b/challenge-032/steven-wilson/perl5/ch-1.pl new file mode 100644 index 0000000000..99b1e44d31 --- /dev/null +++ b/challenge-032/steven-wilson/perl5/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl +# Author: Steven Wilson +# Date: 2019-10-28 +# Week: 032 + +# Task #1 + +# Count instances +# Create a script that either reads standard input or one or more files +# specified on the command-line. Count the number of times and then +# print a summary, sorted by the count of each entry. +# So with the following input in file example.txt +# apple +# banana +# apple +# cherry +# cherry +# apple +# the script would display something like: +# apple 3 +# cherry 2 +# banana 1 +# For extra credit, add a -csv option to your script, which would generate: +# apple,3 +# cherry,2 +# banana,1 + +use strict; +use warnings; +use feature qw/ say /; + +my %word_count; +my $delimiter = "\t"; +if ( $ARGV[0] eq "-csv" ) { + shift @ARGV; + $delimiter = ","; +} + +for my $file (@ARGV) { + open my $fh, '<', $file or die "Can't open < $file: $!"; + while ( !eof $fh ) { + my $word = readline $fh; + chomp $word; + $word_count{$word} += 1; + } +} + +my @word_count_desc + = reverse sort { $word_count{$a} <=> $word_count{$b} } keys %word_count; + +for my $word (@word_count_desc) { + say "$word$delimiter$word_count{$word}"; +} -- cgit From c483ea58ab8d7ae3a445429e0b86921713a9d69e Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 28 Oct 2019 21:42:27 +0000 Subject: - Added solutions by Markus Holzer. --- challenge-032/markus-holzer/perl6/ch-1.p6 | 26 ++++++++++++++++++++++++++ challenge-032/markus-holzer/perl6/ch-2.p6 | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 challenge-032/markus-holzer/perl6/ch-1.p6 create mode 100644 challenge-032/markus-holzer/perl6/ch-2.p6 (limited to 'challenge-032') diff --git a/challenge-032/markus-holzer/perl6/ch-1.p6 b/challenge-032/markus-holzer/perl6/ch-1.p6 new file mode 100644 index 0000000000..f1eb4d25dc --- /dev/null +++ b/challenge-032/markus-holzer/perl6/ch-1.p6 @@ -0,0 +1,26 @@ +my %*SUB-MAIN-OPTS = :named-anywhere; + +multi sub MAIN( *@files, Bool :$csv, Bool :$graph, Bool :$sort-by-label ) +{ + CATCH { return .message.say } + + my @words = @files + ?? @files.map( |*.IO.lines ) + !! |$*ARGFILES.lines; + + my $weights = Bag.new( @words ); + my $lngst = max $weights.keys.map( *.chars ); + + my $format = $csv ?? "%s, %s" !! + $graph ?? "%{$lngst}s | %s " !! + "%-{$lngst}s %s " ; + + my &sorter = $sort-by-label + ?? { $^a.key cmp $^b.key } + !! { $^b.value <=> $^a.value }; + + .say for $weights + .sort( &sorter ) + .map({ .key => $graph ?? "#" x .value !! .value }) + .map({ sprintf $format, .key, .value }); +} \ No newline at end of file diff --git a/challenge-032/markus-holzer/perl6/ch-2.p6 b/challenge-032/markus-holzer/perl6/ch-2.p6 new file mode 100644 index 0000000000..f1eb4d25dc --- /dev/null +++ b/challenge-032/markus-holzer/perl6/ch-2.p6 @@ -0,0 +1,26 @@ +my %*SUB-MAIN-OPTS = :named-anywhere; + +multi sub MAIN( *@files, Bool :$csv, Bool :$graph, Bool :$sort-by-label ) +{ + CATCH { return .message.say } + + my @words = @files + ?? @files.map( |*.IO.lines ) + !! |$*ARGFILES.lines; + + my $weights = Bag.new( @words ); + my $lngst = max $weights.keys.map( *.chars ); + + my $format = $csv ?? "%s, %s" !! + $graph ?? "%{$lngst}s | %s " !! + "%-{$lngst}s %s " ; + + my &sorter = $sort-by-label + ?? { $^a.key cmp $^b.key } + !! { $^b.value <=> $^a.value }; + + .say for $weights + .sort( &sorter ) + .map({ .key => $graph ?? "#" x .value !! .value }) + .map({ sprintf $format, .key, .value }); +} \ No newline at end of file -- cgit From a099703bbdadb9578e4703b86d76ec4d1fda4811 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 28 Oct 2019 22:54:48 +0100 Subject: Solve 032 (ASCII Bar chart + Count instances) Plus a standard, many years old bash script to count instances. --- challenge-032/e-choroba/perl5/ch-1.pl | 26 ++++++++++++++ challenge-032/e-choroba/perl5/ch-1.sh | 2 ++ challenge-032/e-choroba/perl5/ch-2.pl | 64 +++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100755 challenge-032/e-choroba/perl5/ch-1.pl create mode 100755 challenge-032/e-choroba/perl5/ch-1.sh create mode 100755 challenge-032/e-choroba/perl5/ch-2.pl (limited to 'challenge-032') diff --git a/challenge-032/e-choroba/perl5/ch-1.pl b/challenge-032/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..4fbaef2346 --- /dev/null +++ b/challenge-032/e-choroba/perl5/ch-1.pl @@ -0,0 +1,26 @@ +#! /usr/bin/perl +use warnings; +use strict; + +use Getopt::Long; +use Text::CSV_XS qw{ csv }; +use List::Util qw{ max }; + +use open IO => ':encoding(UTF-8)', ':std'; + +GetOptions(csv => \ my $csv_output); + +my %count; +chomp, ++$count{$_} while <>; + +if ($csv_output) { + csv(in => [ + map [$_, $count{$_} ], + sort { $count{$b} <=> $count{$a} } + keys %count]); + +} else { + my $max_length = max(map length, keys %count); + printf "%${max_length}s %d\n", $_, $count{$_} + for sort { $count{$a} <=> $count{$b} } keys %count; +} diff --git a/challenge-032/e-choroba/perl5/ch-1.sh b/challenge-032/e-choroba/perl5/ch-1.sh new file mode 100755 index 0000000000..2f88949be9 --- /dev/null +++ b/challenge-032/e-choroba/perl5/ch-1.sh @@ -0,0 +1,2 @@ +#! /bin/bash +cat "$@" | sort | uniq -c | sort -nr diff --git a/challenge-032/e-choroba/perl5/ch-2.pl b/challenge-032/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..752d3dc050 --- /dev/null +++ b/challenge-032/e-choroba/perl5/ch-2.pl @@ -0,0 +1,64 @@ +#! /usr/bin/perl +{ package My::BarChart; + use Moo; + + use Function::Parameters; + use Types::Standard qw{ HashRef Num Str Enum }; + use List::Util qw{ max }; + + sub SortBy { Enum[qw[ labels values labels_desc values_desc ]] } + + use namespace::clean; + + has width => (is => 'lazy', isa => Num); + has data => (is => 'ro', isa => HashRef[Num], required => 1); + has separator => (is => 'ro', isa => Str, default => ' | '); + + has _bar_width => (is => 'lazy', isa => Num); + has _max_length => (is => 'lazy', isa => Num); + + method generate (SortBy $sort_by = 'keys') { + my $data = $self->data; + my $max = max(values %$data); + + my $sort = {labels => \&_by_key, + values => \&_by_value, + labels_desc => \&_by_key_desc, + values_desc => \&_by_value_desc}->{$sort_by}; + + for my $key (sort { $self->$sort } keys %$data) { + printf '%' . $self->_max_length . "s%s%s\n", + $key, + $self->separator, + '#' x ($self->_bar_width / $max * $data->{$key}); + } + } + + method BUILD ($) { + die "Chart is too wide.\n" if $self->_bar_width <= 0; + } + + method _build_width () { $ENV{COLUMNS} || qx{tput cols} || 80 } + + method _build__max_length () { max(map length, keys %{ $self->data }) } + + method _build__bar_width () { + $self->width - $self->_max_length - length $self->separator + } + + method _by_key () { $a cmp $b } + method _by_key_desc () { $b cmp $a } + method _by_value () { $self->data->{$a} <=> $self->data->{$b} } + method _by_value_desc () { $self->data->{$b} <=> $self->data->{$a} } +} + +use warnings; +use strict; +use feature qw{ say }; + +my $data = { apple => 3, cherry => 2, banana => .5 }; +my $chart = 'My::BarChart'->new(data => $data); + +say $chart->generate('labels'); +say $chart->generate('values'); +say $chart->generate('values_desc'); -- cgit