aboutsummaryrefslogtreecommitdiff
path: root/challenge-034
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-11-17 22:04:01 +0000
committerGitHub <noreply@github.com>2019-11-17 22:04:01 +0000
commit9ffa9e135d18fa689304a7264cbfdea7f8fbe444 (patch)
tree2914ecc466d72231004a23f54079351f2a48a5df /challenge-034
parentb273bc4d4272ada418aa1f93e25eb2a5bca1446c (diff)
parentccf97ba1ecd9b37895a1355c60932b4a658a9c6b (diff)
downloadperlweeklychallenge-club-9ffa9e135d18fa689304a7264cbfdea7f8fbe444.tar.gz
perlweeklychallenge-club-9ffa9e135d18fa689304a7264cbfdea7f8fbe444.tar.bz2
perlweeklychallenge-club-9ffa9e135d18fa689304a7264cbfdea7f8fbe444.zip
Merge pull request #940 from adamcrussell/challenge-034
Challenge 034
Diffstat (limited to 'challenge-034')
-rw-r--r--challenge-034/adam-russell/blog.txt1
-rw-r--r--challenge-034/adam-russell/cxx/ch-1.cxx28
-rw-r--r--challenge-034/adam-russell/cxx/ch-2.cxx16
-rw-r--r--challenge-034/adam-russell/perl5/ch-1.pl13
-rw-r--r--challenge-034/adam-russell/perl5/ch-2.pl11
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..9ea156c0ae
--- /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(int argc, char** argv){
+ 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}();