aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-23 21:28:09 +0100
committerGitHub <noreply@github.com>2021-05-23 21:28:09 +0100
commit4782f92eb901e6c30d5dab4b415369924c7ff821 (patch)
tree07d46ca46f4c6ec34e5690e9caf5629c2d5fe6c6
parentae46850ffe2e7a6825893a6c5f78a21fdddbbb65 (diff)
parent8d7aa571a4ad953fe67692ed3beec61c406eed21 (diff)
downloadperlweeklychallenge-club-4782f92eb901e6c30d5dab4b415369924c7ff821.tar.gz
perlweeklychallenge-club-4782f92eb901e6c30d5dab4b415369924c7ff821.tar.bz2
perlweeklychallenge-club-4782f92eb901e6c30d5dab4b415369924c7ff821.zip
Merge pull request #4125 from adamcrussell/challenge-113
Challenge 113
-rw-r--r--challenge-113/adam-russell/blog.txt1
-rw-r--r--challenge-113/adam-russell/blog1.txt1
-rw-r--r--challenge-113/adam-russell/perl/ch-1.pl18
-rw-r--r--challenge-113/adam-russell/perl/ch-2.pl59
-rw-r--r--challenge-113/adam-russell/prolog/ch-1.p24
-rw-r--r--challenge-113/adam-russell/prolog/ch-2.p46
6 files changed, 149 insertions, 0 deletions
diff --git a/challenge-113/adam-russell/blog.txt b/challenge-113/adam-russell/blog.txt
new file mode 100644
index 0000000000..99a2dcc848
--- /dev/null
+++ b/challenge-113/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/05/23
diff --git a/challenge-113/adam-russell/blog1.txt b/challenge-113/adam-russell/blog1.txt
new file mode 100644
index 0000000000..991f23e0f6
--- /dev/null
+++ b/challenge-113/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2021/05/23
diff --git a/challenge-113/adam-russell/perl/ch-1.pl b/challenge-113/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..ca77eb766d
--- /dev/null
+++ b/challenge-113/adam-russell/perl/ch-1.pl
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+##
+# You are given a positive integer $N and a digit $D.
+# Write a script to check if $N can be represented as
+# a sum of positive integers having $D at least once.
+# If the check passes print 1 otherwise 0.
+##
+sub is_represented{
+ my($n, $d) = @_;
+ my @contains = grep { grep { $_ == $d } split(//) } (1 .. $n);
+ return $n == unpack("%32C*", pack("C*", @contains));
+}
+
+MAIN:{
+ print is_represented(25, 7) + 0 . "\n";
+ print is_represented(24, 7) + 0 . "\n";
+}
diff --git a/challenge-113/adam-russell/perl/ch-2.pl b/challenge-113/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..1b9f0bafd8
--- /dev/null
+++ b/challenge-113/adam-russell/perl/ch-2.pl
@@ -0,0 +1,59 @@
+use strict;
+use warnings;
+##
+# You are given a Binary Tree.
+# Write a script to replace each node of the tree with
+# the sum of all the remaining nodes.
+##
+use Graph;
+use Graph::Easy::Parser;
+
+sub dfs_update{
+ my($graph, $vertex, $graph_updated, $previous) = @_;
+ my @successors = $graph->successors($vertex);
+ for my $successor (@successors){
+ my $sum_remaining = sum_remaining($graph, $vertex);
+ $graph_updated->add_edge($previous, $sum_remaining) if $previous;
+ dfs_update($graph, $successor, $graph_updated, $sum_remaining);
+ }
+ $graph_updated->add_edge($previous, sum_remaining($graph, $vertex)) if !@successors;
+}
+
+sub sum_remaining{
+ my($graph, $visited) = @_;
+ my $sum = 0;
+ for my $vertex ($graph->vertices()){
+ $sum += $vertex if $vertex != $visited;
+ }
+ return $sum;
+}
+
+sub display_graph{
+ my($graph) = @_;
+ my $s = $graph->stringify();
+ my @s = split(/,/, $s);
+ my @lines;
+ for my $n (@s){
+ my @a = split(/-/, $n);
+ push @lines, "[ $a[0] ] => [ $a[1] ]";
+ }
+ my $parser = new Graph::Easy::Parser();
+ my $graph_viz = $parser->from_text(join("", @lines));
+ print $graph_viz->as_ascii();
+}
+
+MAIN:{
+ my $graph = new Graph();
+ my $graph_updated = new Graph();
+ my $root = 1;
+ $graph->add_edge($root, 2);
+ $graph->add_edge($root, 3);
+ $graph->add_edge(2, 4);
+ $graph->add_edge(4, 7);
+ $graph->add_edge(3, 5);
+ $graph->add_edge(3, 6);
+ dfs_update($graph, $root, $graph_updated);
+ display_graph($graph);
+ display_graph($graph_updated);
+}
+
diff --git a/challenge-113/adam-russell/prolog/ch-1.p b/challenge-113/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..0031401999
--- /dev/null
+++ b/challenge-113/adam-russell/prolog/ch-1.p
@@ -0,0 +1,24 @@
+:-initialization(main).
+
+contains([], _, []).
+contains([H|T], Digit, [N|R]):-
+ number_chars(H, C),
+ number_chars(Digit, [D]),
+ member(D, C),
+ N = H,
+ contains(T, Digit, R).
+contains([H|T], Digit, Contains):-
+ number_chars(H, C),
+ number_chars(Digit, [D]),
+ \+ member(D, C),
+ contains(T, Digit, Contains).
+
+represented(N, D):-
+ findall(X, between(1, N, X), Numbers),
+ contains(Numbers, D, Contains),
+ sum_list(Contains, N).
+
+main:-
+ (((represented(25, 7), write(1)); write(0)), nl),
+ (((represented(24, 7), write(1)); write(0)), nl),
+ halt.
diff --git a/challenge-113/adam-russell/prolog/ch-2.p b/challenge-113/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..78d655568e
--- /dev/null
+++ b/challenge-113/adam-russell/prolog/ch-2.p
@@ -0,0 +1,46 @@
+:-dynamic(edge/3).
+
+:-initialization(main).
+
+root(1).
+edge(old, 1, 2).
+edge(old, 2, 4).
+edge(old, 4, 7).
+edge(old, 1, 3).
+edge(old, 3, 5).
+edge(old, 3, 6).
+
+dfs_replace(GraphOld, GraphNew, Vertex):-
+ dfs_replace(GraphOld, GraphNew, Vertex, _).
+dfs_replace(GraphOld, GraphNew, Vertex, VertexPrevious):-
+ (var(VertexPrevious),
+ edge(GraphOld, Vertex, VertexNext),
+ sum_remaining(GraphOld, Vertex, SumRemaining),
+ dfs_replace(GraphOld, GraphNew, VertexNext, SumRemaining));
+ sum_remaining(GraphOld, Vertex, SumRemaining),
+ assertz(edge(GraphNew, VertexPrevious, SumRemaining)),
+ findall(V, edge(GraphOld, _, V), VerticesOld),
+ findall(V, edge(GraphNew, _, V), VerticesNew),
+ length(VerticesOld, VOL),
+ length(VerticesNew, VNL),
+ VOL \== VNL,
+ edge(GraphOld, Vertex, VertexNext),
+ dfs_replace(GraphOld, GraphNew, VertexNext, SumRemaining).
+dfs_replace(GraphOld, GraphNew, _, _):-
+ findall(V, edge(GraphOld, _, V), VerticesOld),
+ findall(V, edge(GraphNew, _, V), VerticesNew),
+ length(VerticesOld, VOL),
+ length(VerticesNew, VNL),
+ VOL == VNL.
+
+sum_remaining(GraphOld, Vertex, SumRemaining):-
+ findall(V, edge(GraphOld, _, V), Vertices),
+ root(Root),
+ delete([Root|Vertices], Vertex, RemainingVertices),
+ sum_list(RemainingVertices, SumRemaining).
+
+main:-
+ root(Root),
+ dfs_replace(old, new, Root),
+ listing(edge/3),
+ halt.