aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-11-28 03:27:49 +0000
committerGitHub <noreply@github.com>2022-11-28 03:27:49 +0000
commite7e4278515ce62015dae9db447a5a8904bb1f646 (patch)
tree18df55cbe4271e4c45b72830f4233a9049ad5d96
parent654cb062ea99e7fa585405bc5887f4adbde8a4ea (diff)
parent6df754efaf06b4858e29b0962314312a371b29ba (diff)
downloadperlweeklychallenge-club-e7e4278515ce62015dae9db447a5a8904bb1f646.tar.gz
perlweeklychallenge-club-e7e4278515ce62015dae9db447a5a8904bb1f646.tar.bz2
perlweeklychallenge-club-e7e4278515ce62015dae9db447a5a8904bb1f646.zip
Merge pull request #7169 from adamcrussell/challenge-192
initial commit
-rw-r--r--challenge-192/adam-russell/blog.txt1
-rw-r--r--challenge-192/adam-russell/blog1.txt1
-rw-r--r--challenge-192/adam-russell/perl/ch-1.pl28
-rw-r--r--challenge-192/adam-russell/perl/ch-2.pl32
-rw-r--r--challenge-192/adam-russell/prolog/ch-1.p16
-rw-r--r--challenge-192/adam-russell/prolog/ch-2.p63
6 files changed, 141 insertions, 0 deletions
diff --git a/challenge-192/adam-russell/blog.txt b/challenge-192/adam-russell/blog.txt
new file mode 100644
index 0000000000..ad5d08ed10
--- /dev/null
+++ b/challenge-192/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/11/27 \ No newline at end of file
diff --git a/challenge-192/adam-russell/blog1.txt b/challenge-192/adam-russell/blog1.txt
new file mode 100644
index 0000000000..5ae156da3d
--- /dev/null
+++ b/challenge-192/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/11/27 \ No newline at end of file
diff --git a/challenge-192/adam-russell/perl/ch-1.pl b/challenge-192/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..8b8603e9b3
--- /dev/null
+++ b/challenge-192/adam-russell/perl/ch-1.pl
@@ -0,0 +1,28 @@
+use v5.36;
+##
+# You are given a positive integer, $n.
+# Write a script to find the binary flip.
+##
+sub int2bits{
+ my($n) = @_;
+ my @bits;
+ while($n){
+ my $b = $n & 1;
+ unshift @bits, $b;
+ $n = $n >> 1;
+ }
+ return @bits
+}
+
+sub binary_flip{
+ my($n) = @_;
+ my @bits = int2bits($n);
+ @bits = map {$_^ 1} @bits;
+ return oct(q/0b/ . join(q//, @bits));
+}
+
+MAIN:{
+ say binary_flip(5);
+ say binary_flip(4);
+ say binary_flip(6);
+} \ No newline at end of file
diff --git a/challenge-192/adam-russell/perl/ch-2.pl b/challenge-192/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..6c038d8cf6
--- /dev/null
+++ b/challenge-192/adam-russell/perl/ch-2.pl
@@ -0,0 +1,32 @@
+use v5.36;
+##
+# You are given a list of integers greater than or equal to zero, @list.
+# Write a script to distribute the number so that each members are same.
+# If you succeed then print the total moves otherwise print -1.
+# Please follow these rules:
+# 1) You can only move a value of '1' per move
+# 2) You are only allowed to move a value of '1' to a direct neighbor/adjacent cell
+##
+use POSIX;
+
+sub equal_distribution{
+ my(@integers) = @_;
+ my $moves;
+ my $average = unpack("%32I*", pack("I*", @integers)) / @integers;
+ return -1 unless floor($average) == ceil($average);
+ {
+ map{
+ my $i = $_;
+ if($integers[$i] > $average && $integers[$i] > $integers[$i+1]){$integers[$i]--; $integers[$i+1]++; $moves++}
+ if($integers[$i] < $average && $integers[$i] < $integers[$i+1]){$integers[$i]++; $integers[$i+1]--; $moves++}
+ } 0 .. @integers - 2;
+ redo unless 0 == grep {$average != $_} @integers;
+ }
+ return $moves;
+}
+
+MAIN:{
+ say equal_distribution(1, 0, 5);
+ say equal_distribution(0, 2, 0);
+ say equal_distribution(0, 3, 0);
+} \ No newline at end of file
diff --git a/challenge-192/adam-russell/prolog/ch-1.p b/challenge-192/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..c93214b4fb
--- /dev/null
+++ b/challenge-192/adam-russell/prolog/ch-1.p
@@ -0,0 +1,16 @@
+bits(N, Bits):-
+ bits(N, [], Bits).
+bits(0, Bits, Bits):-!.
+bits(N, BitsAccum, Bits):-
+ B is N /\ 1,
+ N0 is N >> 1,
+ bits(N0, [B|BitsAccum], Bits).
+
+bits_reverse(B, RB):-
+ Flipped is xor(B, 1),
+ number_chars(Flipped, [RB]).
+
+binary_flip(N, NBitsReversed):-
+ bits(N, Bits),
+ maplist(bits_reverse, Bits, BitsReversed),
+ number_chars(NBitsReversed, ['0','b'|BitsReversed]). \ No newline at end of file
diff --git a/challenge-192/adam-russell/prolog/ch-2.p b/challenge-192/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..767cb42c8c
--- /dev/null
+++ b/challenge-192/adam-russell/prolog/ch-2.p
@@ -0,0 +1,63 @@
+:-dynamic(moves/1).
+moves(0).
+
+equal_distribution(ListIntegers, _):-
+ length(ListIntegers, L),
+ sum_list(ListIntegers, S),
+ Average is S / L,
+ F is floor(Average),
+ C is ceiling(Average),
+ F \== C,
+ fail.
+equal_distribution(ListIntegers, ListEqualDistribution):-
+ length(ListIntegers, L),
+ sum_list(ListIntegers, S),
+ Average is S / L,
+ F is floor(Average),
+ C is ceiling(Average),
+ F == C,
+ length(ListEqualDistribution, L),
+ equal_distribution(ListIntegers, F, ListEqual),
+ delete(ListEqual, F, ListEqualAverageDeleted),
+ length(ListEqualAverageDeleted, ListEqualAverageDeletedLength),
+ ((ListEqualAverageDeletedLength == 0,
+ ListEqualDistribution = ListEqual);
+ equal_distribution(ListEqual, ListEqualDistribution)), !.
+
+distribute(Average, [X, Y], [S, T]):-
+ X < Average,
+ X < Y,
+ S is X + 1,
+ T is Y - 1,
+ moves(Moves),
+ succ(Moves, M),
+ retract(moves(Moves)),
+ asserta(moves(M)).
+distribute(Average, [X, Y], [S, T]):-
+ X > Average,
+ X > Y,
+ S is X - 1,
+ T is Y + 1,
+ moves(Moves),
+ succ(Moves, M),
+ retract(moves(Moves)),
+ asserta(moves(M)).
+distribute(Average, [X, Y], [S, T]):-
+ ((X == Average; X == Y);
+ (X < Average, X > Y)
+ ),
+ S = X,
+ T = Y.
+
+equal_distribution([A, B|[]], Average, [X, Y|[]]):-
+ maplist(distribute(Average),[[A, B]], [[X, Y]]).
+equal_distribution(ListIntegers, Average, [X|T]):-
+ append([A, B], RestIntegers, ListIntegers),
+ maplist(distribute(Average),[[A, B]], [[X, Y]]),
+ equal_distribution([Y|RestIntegers], Average, T).
+
+main(ListIntegers, Moves):-
+ retract(moves(_)),
+ asserta(moves(0)),
+ (equal_distribution(ListIntegers, _), moves(Moves), !);
+ Moves = -1. \ No newline at end of file