aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-145/adam-russell/cxx/ch-1.cxx25
-rw-r--r--challenge-145/adam-russell/perl/ch-1.pl20
-rw-r--r--challenge-145/adam-russell/prolog/ch-1.p10
3 files changed, 55 insertions, 0 deletions
diff --git a/challenge-145/adam-russell/cxx/ch-1.cxx b/challenge-145/adam-russell/cxx/ch-1.cxx
new file mode 100644
index 0000000000..d059e70c28
--- /dev/null
+++ b/challenge-145/adam-russell/cxx/ch-1.cxx
@@ -0,0 +1,25 @@
+#include<vector>
+#include<iostream>
+
+class DotProduct{
+ public:
+ int dot_product(std::vector<int>, std::vector<int>);
+};
+
+int DotProduct::dot_product(std::vector<int> a, std::vector<int> b){
+ if(a.empty() && b.empty()){
+ return 0;
+ }
+ int s = a.back();
+ a.pop_back();
+ int t = b.back();
+ b.pop_back();
+ return s * t + this->dot_product(a, b);
+}
+
+int main(int argc, char** argv){
+ DotProduct dp;
+ std::vector<int> a = {1, 2, 3};
+ std::vector<int> b = {4, 5, 6};
+ std::cout << dp.dot_product(a, b) << std::endl;
+} \ No newline at end of file
diff --git a/challenge-145/adam-russell/perl/ch-1.pl b/challenge-145/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..167bcd6cc2
--- /dev/null
+++ b/challenge-145/adam-russell/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+##
+# You are given 2 arrays of the same size, @a and @b.
+# Write a script to implement the Dot Product.
+##
+sub dot_product{
+ my($s, $t) = @_;
+ return $s->[0] * $t->[0] if(@{$s} == 1 && @{$t} == 1);
+ my $u = pop @{$s};
+ my $v = pop @{$t};
+ return $u * $v + dot_product($s, $t);
+}
+
+MAIN:{
+ my(@a, @b);
+ @a = (1, 2, 3);
+ @b = (4, 5, 6);
+ print dot_product(\@a, \@b) . "\n";
+} \ No newline at end of file
diff --git a/challenge-145/adam-russell/prolog/ch-1.p b/challenge-145/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..1f4936c535
--- /dev/null
+++ b/challenge-145/adam-russell/prolog/ch-1.p
@@ -0,0 +1,10 @@
+dot_product([], [], DotProduct):-
+ DotProduct = 0.
+
+dot_product([A|T0], [B|T1], DotProduct):-
+ dot_product(T0, T1, DP),
+ DotProduct is (A * B) + DP.
+
+main:-
+ dot_product([1, 2, 3], [4, 5, 6], DotProduct),
+ write(DotProduct), nl. \ No newline at end of file