Created
November 3, 2019 05:44
-
-
Save adamcrussell/406eeab9136d994d99ea14fc15333ca2 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 032
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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::Fast; | |
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment