aboutsummaryrefslogtreecommitdiff
path: root/challenge-044
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-02-16 17:08:54 +0000
committerGitHub <noreply@github.com>2020-02-16 17:08:54 +0000
commitd465e98f90beccde1c0fb2b81c283c778997d437 (patch)
tree7ff106510ed5475c61e1c98bf4de119c86afef37 /challenge-044
parent055b3df1d53bab072472d58a140282ec59337977 (diff)
parent7fd8298c4b57b9b6ae57b8f27892c0153f543563 (diff)
downloadperlweeklychallenge-club-d465e98f90beccde1c0fb2b81c283c778997d437.tar.gz
perlweeklychallenge-club-d465e98f90beccde1c0fb2b81c283c778997d437.tar.bz2
perlweeklychallenge-club-d465e98f90beccde1c0fb2b81c283c778997d437.zip
Merge pull request #1256 from jaldhar/challenge-044
Challenge 44 by Jaldhar H. Vyas
Diffstat (limited to 'challenge-044')
-rw-r--r--challenge-044/jaldhar-h-vyas/blog.txt1
-rw-r--r--challenge-044/jaldhar-h-vyas/c++/ch-2.cc63
-rwxr-xr-xchallenge-044/jaldhar-h-vyas/perl/ch-1.pl39
-rwxr-xr-xchallenge-044/jaldhar-h-vyas/perl/ch-2.pl20
-rwxr-xr-xchallenge-044/jaldhar-h-vyas/raku/ch-1.p610
-rwxr-xr-xchallenge-044/jaldhar-h-vyas/raku/ch-2a.p640
-rwxr-xr-xchallenge-044/jaldhar-h-vyas/raku/ch-2b.p618
7 files changed, 191 insertions, 0 deletions
diff --git a/challenge-044/jaldhar-h-vyas/blog.txt b/challenge-044/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..f446b97edf
--- /dev/null
+++ b/challenge-044/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2020/02/perl_weekly_challenge_week_44.html
diff --git a/challenge-044/jaldhar-h-vyas/c++/ch-2.cc b/challenge-044/jaldhar-h-vyas/c++/ch-2.cc
new file mode 100644
index 0000000000..ed93f58677
--- /dev/null
+++ b/challenge-044/jaldhar-h-vyas/c++/ch-2.cc
@@ -0,0 +1,63 @@
+#include <iostream>
+#include <limits>
+#include <memory>
+#include <stack>
+#include <string>
+
+using Branch = std::stack<std::string>;
+
+struct Node {
+ explicit Node(Node* parent, int amount, const char* label) :
+ parent_{parent}, amount_{amount}, label_{label}, left_{nullptr},
+ right_{nullptr} {
+ }
+
+ Node(const Node&)=delete;
+ Node& operator=(const Node&)=delete;
+
+ Node* parent_;
+ int amount_;
+ std::string label_;
+ std::unique_ptr<Node> left_;
+ std::unique_ptr<Node> right_;
+};
+
+void traverse(Node* node, Branch& bestBranch, int& maxDepth, int depth) {
+
+ if (depth < maxDepth && node->amount_ < 200) {
+ node->left_.reset(new Node(node, node->amount_ * 2, "double"));
+ node->right_.reset(new Node(node, node->amount_ + 1, "add one"));
+ traverse(node->left_.get(), bestBranch, maxDepth, depth + 1);
+ traverse(node->right_.get(), bestBranch, maxDepth, depth + 1);
+
+ } else if (node->amount_ == 200) {
+ if (depth < maxDepth) {
+ maxDepth = depth;
+ Branch branch;
+ Node* current = node;
+
+ while (current->parent_ != nullptr) {
+ branch.push(current->label_);
+ current = current->parent_;
+ }
+ bestBranch = branch;
+ }
+ }
+}
+
+int main() {
+ Branch result;
+ int maxdepth = std::numeric_limits<int>::max();
+ std::unique_ptr<Node> root(new Node(nullptr, 1, ""));
+
+ traverse(root.get(), result, maxdepth, 0);
+
+ while(!result.empty()) {
+ std::cout << result.top();
+ result.pop();
+ if (!result.empty()) {
+ std::cout << ", ";
+ }
+ }
+ std::cout << '\n';
+} \ No newline at end of file
diff --git a/challenge-044/jaldhar-h-vyas/perl/ch-1.pl b/challenge-044/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..167bf3536a
--- /dev/null
+++ b/challenge-044/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+my @digits = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
+my @ops = (' + ', ' - ', q{});
+
+sub X {
+ my @a = @{ $_[0] };
+ my @b = @{ $_[1] };
+
+ return map {
+ my $first = $_;
+ map {
+ [ ref $first eq 'ARRAY' ? @{$first} : $first, $_ ];
+ } @b;
+ } @a;
+}
+
+sub Ztilde {
+ my @a = @{ $_[0] };
+ my @b = @{ $_[1] };
+
+ my @result;
+ for my $i (0 .. scalar @b - 1) {
+ push @result, $a[$i], $b[$i];
+ }
+ return @result;
+}
+
+my @output = @ops;
+for (1 .. 7) {
+ @output = X(\@output, \@ops);
+}
+@output = X(\@output, [q{}]);
+
+say "$_ = 100" for
+ grep { eval $_ == 100 } map { join q{}, Ztilde(\@digits, $_); } @output;
diff --git a/challenge-044/jaldhar-h-vyas/perl/ch-2.pl b/challenge-044/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..1f34a6855c
--- /dev/null
+++ b/challenge-044/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+my @result;
+my $n = 200;
+
+while ($n != 1) {
+ if ($n % 2 == 0) {
+ unshift @result, 'double';
+ $n /= 2;
+ } else {
+ unshift @result, 'add one';
+ $n--;
+ }
+}
+
+say join q{, }, @result;
+
diff --git a/challenge-044/jaldhar-h-vyas/raku/ch-1.p6 b/challenge-044/jaldhar-h-vyas/raku/ch-1.p6
new file mode 100755
index 0000000000..b041955d28
--- /dev/null
+++ b/challenge-044/jaldhar-h-vyas/raku/ch-1.p6
@@ -0,0 +1,10 @@
+#!/usr/bin/perl6
+use MONKEY-SEE-NO-EVAL;
+
+my @digits = 1 .. 9;
+my @ops = (' + ', ' - ', q{});
+
+say "$_ = 100" for
+ (([X] @ops xx 8) X q{})
+ ==> map({ (@digits Z~ @_).join; })
+ ==> grep({ EVAL($_) == 100; });
diff --git a/challenge-044/jaldhar-h-vyas/raku/ch-2a.p6 b/challenge-044/jaldhar-h-vyas/raku/ch-2a.p6
new file mode 100755
index 0000000000..501ffe20f1
--- /dev/null
+++ b/challenge-044/jaldhar-h-vyas/raku/ch-2a.p6
@@ -0,0 +1,40 @@
+#!/usr/bin/perl6
+
+class Node {
+ has Node $.parent;
+ has Node $.left is rw;
+ has Node $.right is rw;
+ has Str $.label;
+ has Int $.amount;
+}
+
+sub traverse(Node $node, @bestBranch, $maxDepth is rw, $depth) {
+ if $depth < $maxDepth && $node.amount < 200 {
+ $node.left = Node.new(parent => $node, amount => $node.amount * 2,
+ label => 'double');
+ $node.right = Node.new(parent => $node, amount => $node.amount + 1,
+ label => 'add one');
+ traverse($node.left(), @bestBranch, $maxDepth, $depth + 1);
+ traverse($node.right(), @bestBranch, $maxDepth, $depth + 1);
+
+ } elsif $node.amount == 200 && $depth < $maxDepth {
+ $maxDepth = $depth;
+ my @branch;
+ my $current = $node;
+
+ while ($current.parent()) {
+ @branch.unshift($current.label());
+ $current = $current.parent();
+ }
+ @bestBranch = @branch;
+ }
+}
+
+multi sub MAIN {
+ my @results;
+ my $root = Node.new(parent => Nil, label => q{}, amount => 1);
+ my $maxDepth = ∞;
+ traverse($root, @results, $maxDepth, 0);
+
+ @results.join(', ').say;
+} \ No newline at end of file
diff --git a/challenge-044/jaldhar-h-vyas/raku/ch-2b.p6 b/challenge-044/jaldhar-h-vyas/raku/ch-2b.p6
new file mode 100755
index 0000000000..cd7d319cc5
--- /dev/null
+++ b/challenge-044/jaldhar-h-vyas/raku/ch-2b.p6
@@ -0,0 +1,18 @@
+#!/usr/bin/perl6
+
+multi sub MAIN {
+ my @result;
+ my $n = 200;
+
+ while ($n != 1) {
+ if ($n %% 2) {
+ @result.unshift('double');
+ $n /= 2;
+ } else {
+ @result.unshift('add one');
+ $n--;
+ }
+ }
+
+ @result.join(q{, }).say;
+} \ No newline at end of file