Skip to content

Instantly share code, notes, and snippets.

@adamcrussell

adamcrussell/ch-1.cxx

Last active Nov 10, 2019
Embed
What would you like to do?
Perl Weekly Challenge 033
/**
* Create a script that accepts one or more files
* specified on the command-line and count the
* number of times letters appeared in the files.
**/
#include <map>
#include <cctype>
#include <vector>
#include <iostream>
int main(int argc, char** argv){
std::string input;
std::vector<std::pair<char, int>> v;
std::map<char, int> letter_counts;
do{
getline(std::cin, input);
if(!input.empty()){
for(char letter: input){
letter = tolower(letter);
if(letter != ' ' && letter != '\t' && letter != '\n' && letter != '.'){
if(letter_counts.find(letter) != letter_counts.end()){
letter_counts[letter] += 1;
}
else{
letter_counts[letter] = 1;
}
}
}
}
}while(!input.empty());
std::copy(letter_counts.begin(), letter_counts.end(), std::back_inserter<std::vector<std::pair<char, int>>>(v));
std::vector<std::pair<char, int>>::iterator iter = v.begin();
while(iter != v.end()){
std::cout << iter->first << ": " << iter->second << std::endl;
iter++;
}
}
##
# Create a script that accepts one or more files
# specified on the command-line and count the
# number of times letters appeared in the files.
##
sub MAIN {
my %letter_count;
for $*IN.lines() -> $line {
my @characters = $line.split("");
for @characters -> $c {
%letter_count{$c}++ if $c~~m/<alpha>/;
}
}
for sort keys %letter_count -> $key {
print "$key: %letter_count{$key}\n";
}
}
use strict;
use warnings;
##
# Create a script that accepts one or more files
# specified on the command-line and count the
# number of times letters appeared in the files.
##
MAIN:{
my %letter_count;
while(<>){
chomp;
my @characters = split(//, $_);
for my $c (@characters){
$letter_count{$c}++ if $c=~m/[[:alpha:]]/;
}
}
for my $key (sort keys %letter_count){
print "$key: $letter_count{$key}\n";
}
}
/**
* Write a script to print 11x11 multiplication
* table, only the top half triangle.
**/
#include <iostream>
int main(int argc, char **argv){
std::cout << " x| 1 2 3 4 5 6 7 8 9 10 11" << std::endl;
std::cout << " ---+------------------------------------------------" << std::endl;
for(int x = 1; x <= 11; x++){
std::cout.width(6); std::cout << x << "|";
for(int y = 1; y <= 11; y++){
if(y < x){
std::cout.width(4); std::cout << std::right << " ";
}
else{
if(y < 10){
std::cout.width(4); std::cout << std::right << x*y;
}
else{
std::cout.width(6); std::cout << std::right << x*y;
}
}
}
std::cout << std::endl;
}
}
##
# Write a script to print 11x11 multiplication
# table, only the top half triangle.
##
use Form;
sub print_table11 {
my ($x,$x1,$x2,$x3,$x4,$x5,$x6,$x7,$x8,$x9,$x10,$x11);
my $header = form
' x| 1 2 3 4 5 6 7 8 9 10 11',
' ---+----------------------------------------------';
print $header;
for 1 .. 11 -> $x {
my @a;
@a = (0) xx ($x -1) if $x > 1;
@a.append($x .. 11);
my @b = map({$_ == 0 ?? "" !! $_}, map({ $x * $_ }, @a));
print sprintf '%5s|', $x;
my $s = sprintf '%4s%4s%4s%4s%4s%4s%4s%4s%4s%5s%5s', @b;
say $s;
}
}
sub MAIN {
print_table11;
}
use strict;
use warnings;
##
# Write a script to print 11x11 multiplication
# table, only the top half triangle.
##
use Perl6::Form;
sub print_table11{
my($x,$x1,$x2,$x3,$x4,$x5,$x6,$x7,$x8,$x9,$x10,$x11);
my $header = form
' x| 1 2 3 4 5 6 7 8 9 10 11',
' ---+----------------------------------------------';
print "$header";
for $x (1 .. 11){
my @a;
@a = (0) x ($x -1) if $x > 1;
push @a, ($x .. 11);
($x1,$x2,$x3,$x4,$x5,$x6,$x7,$x8,$x9,$x10,$x11) = map {$_ == 0 ? "" : $_} map { $x * $_ } @a;
my $row = form
' {>>}|{>>}{>>}{>>}{>>}{>>}{>>}{>>}{>>}{>>}{>>>}{>>>}',
$x,$x1,$x2,$x3,$x4,$x5,$x6,$x7,$x8,$x9,$x10,$x11;
print "$row";
}
}
MAIN:{
print_table11;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.