diff options
| author | Adam Russell <ac.russell@live.com> | 2019-11-03 01:26:32 -0400 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2019-11-03 01:26:32 -0400 |
| commit | b944e70b71acd2ba6d0d98af95d456b2eb3af784 (patch) | |
| tree | 550d2e4821431630b677b3f69ba7fe3904ac9f0c /challenge-032 | |
| parent | 20f13bc3f77dcc532de961cbd0bb470fb8625d66 (diff) | |
| download | perlweeklychallenge-club-b944e70b71acd2ba6d0d98af95d456b2eb3af784.tar.gz perlweeklychallenge-club-b944e70b71acd2ba6d0d98af95d456b2eb3af784.tar.bz2 perlweeklychallenge-club-b944e70b71acd2ba6d0d98af95d456b2eb3af784.zip | |
solutions for challenge 032
Diffstat (limited to 'challenge-032')
| -rw-r--r-- | challenge-032/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-032/adam-russell/cxx/ch-1.cxx | 38 | ||||
| -rw-r--r-- | challenge-032/adam-russell/cxx/ch-2.cxx | 44 | ||||
| -rw-r--r-- | challenge-032/adam-russell/perl5/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-032/adam-russell/perl5/ch-2.pl | 24 | ||||
| -rw-r--r-- | challenge-032/adam-russell/raku/ch-1.p6 | 16 | ||||
| -rw-r--r-- | challenge-032/adam-russell/raku/ch-2.p6 | 19 |
7 files changed, 159 insertions, 0 deletions
diff --git a/challenge-032/adam-russell/blog.txt b/challenge-032/adam-russell/blog.txt new file mode 100644 index 0000000000..522fd09028 --- /dev/null +++ b/challenge-032/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/10802.html diff --git a/challenge-032/adam-russell/cxx/ch-1.cxx b/challenge-032/adam-russell/cxx/ch-1.cxx new file mode 100644 index 0000000000..99fca73635 --- /dev/null +++ b/challenge-032/adam-russell/cxx/ch-1.cxx @@ -0,0 +1,38 @@ +/** +* 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. +**/ +#include <map> +#include <vector> +#include <string> +#include <iostream> +#include <algorithm> +int main(int argc, char** argv){ + std::string input; + std::vector<std::pair<std::string, int>> v; + std::map<std::string, int> word_counts; + do{ + getline(std::cin, input); + if(word_counts.find(input) != word_counts.end()){ + word_counts[input] += 1; + } + else{ + if(!input.empty()) + word_counts[input] = 1; + } + }while(!input.empty()); + std::copy(word_counts.begin(), word_counts.end(), std::back_inserter<std::vector<std::pair<std::string, int>>>(v)); + std::sort(v.begin(), v.end(),[](const std::pair<std::string, int> &l, const std::pair<std::string, int> &r){ + if(l.second != r.second) + return l.second > r.second; + return l.first > r.first; + }); + + std::vector<std::pair<std::string, int>>::iterator iter = v.begin(); + while(iter != v.end()){ + std::cout << iter->first << "\t" << iter->second << std::endl; + iter++; + } +} diff --git a/challenge-032/adam-russell/cxx/ch-2.cxx b/challenge-032/adam-russell/cxx/ch-2.cxx new file mode 100644 index 0000000000..b54904c0bb --- /dev/null +++ b/challenge-032/adam-russell/cxx/ch-2.cxx @@ -0,0 +1,44 @@ +/** +* Write a function that takes a hashref where the keys +* are labels and the values are integer or floating +* point values. Generate a bar graph of the data and +* display it to stdout. +**/ +#include <map> +#include <vector> +#include <iostream> +#include "boost/foreach.hpp" +#include "boost/property_tree/json_parser.hpp" + +#define MAX_LENGTH 10 + +int main(int argc, char** argv){ + int max, min; + std::map<std::string, int> counts; + std::stringstream s; + std::string input(argv[1]); + std::vector<std::pair<std::string, int>> v; + s << input; + boost::property_tree::ptree json_tree; + boost::property_tree::read_json(s, json_tree); + BOOST_FOREACH(boost::property_tree::ptree::value_type &v, json_tree){ + counts[v.first.data()] = stoi(v.second.data()); + } + std::copy(counts.begin(), counts.end(), std::back_inserter<std::vector<std::pair<std::string, int>>>(v)); + std::sort(v.begin(), v.end(),[](const std::pair<std::string, int> &l, const std::pair<std::string, int> &r){ + if(l.second != r.second) + return l.second > r.second; + return l.first > r.first; + }); + std::map<std::string,int>::iterator find_max = std::max_element(counts.begin(),counts.end(),[] (const std::pair<std::string,int>& a, const std::pair<std::string,int>& b)->bool{ return a.second < b.second; } ); + std::map<std::string,int>::iterator find_min = std::min_element(counts.begin(),counts.end(),[] (const std::pair<std::string,int>& a, const std::pair<std::string,int>& b)->bool{ return a.second < b.second; } ); + max = find_max->second; + min = find_min->second; + std::vector<std::pair<std::string, int>>::iterator iter = v.begin(); + while(iter != v.end()){ + float scaled = (iter->second - min + 1.0) / (max - min); + std::string bar ((int)(scaled*MAX_LENGTH),'#'); + std::cout << iter->first << "\t|" << bar << std::endl; + iter++; + } +} diff --git a/challenge-032/adam-russell/perl5/ch-1.pl b/challenge-032/adam-russell/perl5/ch-1.pl new file mode 100644 index 0000000000..082a8144c2 --- /dev/null +++ b/challenge-032/adam-russell/perl5/ch-1.pl @@ -0,0 +1,17 @@ +## +# 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. +## +MAIN:{ + my %word_counts; + while(<>){ + chomp; + $word_counts{$_}+=1; + } + my @sorted_keys = sort {$word_counts{$b} <=> $word_counts{$a}} keys %word_counts; + for my $key (@sorted_keys){ + print "$key\t$word_counts{$key}\n"; + } +} diff --git a/challenge-032/adam-russell/perl5/ch-2.pl b/challenge-032/adam-russell/perl5/ch-2.pl new file mode 100644 index 0000000000..1a976ec56d --- /dev/null +++ b/challenge-032/adam-russell/perl5/ch-2.pl @@ -0,0 +1,24 @@ +use strict; +use warnings; +## +# Write a function that takes a hashref where the keys +# are labels and the values are integer or floating +# point values. Generate a bar graph of the data and +# display it to stdout. +## +use JSON::PP; +use constant MAX_LENGTH => 10; + +MAIN:{ + my $input = $ARGV[0]; + my $data = decode_json($input); + my @sorted_keys = sort {$data->{$b} <=> $data->{$a}} keys %{$data}; + my @sorted_values = sort {$a <=> $b} values %{$data}; + my $min = $sorted_values[0]; + my $max = $sorted_values[@sorted_values - 1]; + for my $key (@sorted_keys){ + my $scaled = ($data->{$key} - $min + 1)/($max - $min); + print "$key\t| "; + print "#" x int(MAX_LENGTH * $scaled) . "\n"; + } +} diff --git a/challenge-032/adam-russell/raku/ch-1.p6 b/challenge-032/adam-russell/raku/ch-1.p6 new file mode 100644 index 0000000000..e69661271c --- /dev/null +++ b/challenge-032/adam-russell/raku/ch-1.p6 @@ -0,0 +1,16 @@ +## +# 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. +## +sub MAIN{ + my %word_counts; + for $*IN.lines -> $line { + $line.chomp; + %word_counts{$line}+=1; + } + for %word_counts.sort(*.value).reverse -> $pair { + say $pair.key ~ "\t" ~ $pair.value; + } +} diff --git a/challenge-032/adam-russell/raku/ch-2.p6 b/challenge-032/adam-russell/raku/ch-2.p6 new file mode 100644 index 0000000000..6c7c17bcfc --- /dev/null +++ b/challenge-032/adam-russell/raku/ch-2.p6 @@ -0,0 +1,19 @@ +use JSON::Fast; +## +# Write a function that takes a hashref where the keys +# are labels and the values are integer or floating +# point values. Generate a bar graph of the data and +# display it to stdout. +## +sub term:<MAX-LENGTH> { 10 }; + +sub MAIN($input) { + my %data = from-json $input; + my @sorted = %data.sort(*.value); + my $min = @sorted[0].value; + my $max = @sorted[@sorted.end].value; + for %data.sort(*.value).reverse -> $pair { + print $pair.key ~ "\t| "; + say "#" x ($pair.value - $min + 1) / ($max - $min) * MAX-LENGTH; + } +} |
