diff options
| author | Adam Russell <ac.russell@live.com> | 2019-11-17 16:24:56 -0500 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2019-11-17 16:24:56 -0500 |
| commit | ec748bdaa40bcbe8e335fa6993812455ee922455 (patch) | |
| tree | 3735e87964d55430d75b9e4145510807d27532e0 /challenge-034 | |
| parent | 1cb9654dd6e3a40a12c031c3d9b7f9e610f30d70 (diff) | |
| download | perlweeklychallenge-club-ec748bdaa40bcbe8e335fa6993812455ee922455.tar.gz perlweeklychallenge-club-ec748bdaa40bcbe8e335fa6993812455ee922455.tar.bz2 perlweeklychallenge-club-ec748bdaa40bcbe8e335fa6993812455ee922455.zip | |
solutions for challenge 034
Diffstat (limited to 'challenge-034')
| -rw-r--r-- | challenge-034/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-034/adam-russell/cxx/ch-1.cxx | 28 | ||||
| -rw-r--r-- | challenge-034/adam-russell/cxx/ch-2.cxx | 16 | ||||
| -rw-r--r-- | challenge-034/adam-russell/perl5/ch-1.pl | 13 | ||||
| -rw-r--r-- | challenge-034/adam-russell/perl5/ch-2.pl | 11 |
5 files changed, 69 insertions, 0 deletions
diff --git a/challenge-034/adam-russell/blog.txt b/challenge-034/adam-russell/blog.txt new file mode 100644 index 0000000000..840b342947 --- /dev/null +++ b/challenge-034/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/11703.html diff --git a/challenge-034/adam-russell/cxx/ch-1.cxx b/challenge-034/adam-russell/cxx/ch-1.cxx new file mode 100644 index 0000000000..c4760d5ddb --- /dev/null +++ b/challenge-034/adam-russell/cxx/ch-1.cxx @@ -0,0 +1,28 @@ +/** +* Write a program that demonstrates using hash slices and/or array slices. +**/ +#include <map> +#include <vector> +#include <string> +#include <iostream> +#include <algorithm> +#include <iterator> + +void print_element(std::string s){ + if(s != "") + std::cout << s << std::endl; +} + +int main(int argc, char** argv){ + std::vector<int> v {2, 4}; + std::map<int, std::string> mapExp = { {1, "first"}, {2, "second"}, {3, "third"}, {4,"fourth"} }; + std::vector<std::string> vValue; + std::transform(mapExp.begin(), mapExp.end(), std::back_inserter(vValue), + [=](const std::pair<int, std::string> &mapItem){ + std::vector<int>::const_iterator iter = std::find(v.begin(), v.end(), mapItem.first); + if(iter != v.end()) + return mapItem.second; + return std::string(""); + }); + std::for_each(vValue.begin(), vValue.end(), print_element); +} diff --git a/challenge-034/adam-russell/cxx/ch-2.cxx b/challenge-034/adam-russell/cxx/ch-2.cxx new file mode 100644 index 0000000000..7b29f55ca0 --- /dev/null +++ b/challenge-034/adam-russell/cxx/ch-2.cxx @@ -0,0 +1,16 @@ +/** +* Write a program that demonstrates a dispatch table. +**/ +#include <map> +#include <string> +#include <iostream> +#include <functional> + +int main(){ + std::map< const std::string , std::function<void(void)> > dispTable{ + {"hello",[](){ std::cout << "Hello!" << std::endl; } }, + {"goodbye",[](){ std::cout << "Goodbye!" << std::endl; } } + }; + dispTable["hello"](); + dispTable["goodbye"](); +}; diff --git a/challenge-034/adam-russell/perl5/ch-1.pl b/challenge-034/adam-russell/perl5/ch-1.pl new file mode 100644 index 0000000000..6735816714 --- /dev/null +++ b/challenge-034/adam-russell/perl5/ch-1.pl @@ -0,0 +1,13 @@ +use strict; +use warnings; +## +# Write a program that demonstrates using hash slices and/or array slices. +## +my %h = ( + 1 => "first", + 2 => "second", + 3 => "third", + 4 => "fourth" +); +my @a = @h{2, 4}; +print join("\n", @a) . "\n"; diff --git a/challenge-034/adam-russell/perl5/ch-2.pl b/challenge-034/adam-russell/perl5/ch-2.pl new file mode 100644 index 0000000000..f44f0f09db --- /dev/null +++ b/challenge-034/adam-russell/perl5/ch-2.pl @@ -0,0 +1,11 @@ +use strict; +use warnings; +## +# Write a program that demonstrates a dispatch table. +## +my %dispatch_table = ( + hello => sub { print "Hello!\n"; }, + goodbye => sub { print "Goodbye!\n"; } +); +$dispatch_table{hello}(); +$dispatch_table{goodbye}(); |
