aboutsummaryrefslogtreecommitdiff
path: root/challenge-192
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-28 16:10:09 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-28 16:10:09 +0100
commitbd33c5f39dd23529c700deff9d6a6891e3721d0b (patch)
treeeeccb17d8439bd53ab22eadce211282729685aee /challenge-192
parentc62ba86c52ad00d866516834828dd7731c63d551 (diff)
downloadperlweeklychallenge-club-bd33c5f39dd23529c700deff9d6a6891e3721d0b.tar.gz
perlweeklychallenge-club-bd33c5f39dd23529c700deff9d6a6891e3721d0b.tar.bz2
perlweeklychallenge-club-bd33c5f39dd23529c700deff9d6a6891e3721d0b.zip
Add Perl solution
Diffstat (limited to 'challenge-192')
-rw-r--r--challenge-192/paulo-custodio/Makefile2
-rw-r--r--challenge-192/paulo-custodio/perl/ch-1.pl49
-rw-r--r--challenge-192/paulo-custodio/perl/ch-2.pl100
-rw-r--r--challenge-192/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-192/paulo-custodio/t/test-2.yaml20
5 files changed, 186 insertions, 0 deletions
diff --git a/challenge-192/paulo-custodio/Makefile b/challenge-192/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-192/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-192/paulo-custodio/perl/ch-1.pl b/challenge-192/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..ed35bddc81
--- /dev/null
+++ b/challenge-192/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+# Challenge 192
+#
+# Task 1: Binary Flip
+# Submitted by: Mohammad S Anwar
+#
+# You are given a positive integer, $n.
+#
+# Write a script to find the binary flip.
+# Example 1
+#
+# Input: $n = 5
+# Output: 2
+#
+# First find the binary equivalent of the given integer, 101.
+# Then flip the binary digits 0 -> 1 and 1 -> 0 and we get 010.
+# So Binary 010 => Decimal 2.
+#
+# Example 2
+#
+# Input: $n = 4
+# Output: 3
+#
+# Decimal 4 = Binary 100
+# Flip 0 -> 1 and 1 -> 0, we get 011.
+# Binary 011 = Decimal 3
+#
+# Example 3
+#
+# Input: $n = 6
+# Output: 1
+#
+# Decimal 6 = Binary 110
+# Flip 0 -> 1 and 1 -> 0, we get 001.
+# Binary 001 = Decimal 1
+
+use Modern::Perl;
+
+sub flip {
+ my($n)=@_;
+ my $len=length(sprintf("%b",$n));
+ my $mask=2**$len-1;
+ return (~$n)&$mask;
+}
+
+@ARGV==1 or die "usage: ch-2.pl n\n";
+my $n=shift||0;
+say flip($n);
diff --git a/challenge-192/paulo-custodio/perl/ch-2.pl b/challenge-192/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..761463954a
--- /dev/null
+++ b/challenge-192/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,100 @@
+#!/usr/bin/perl
+
+# Challenge 192
+#
+# Task 2: Equal Distribution
+# Submitted by: Mohammad S Anwar
+#
+# 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 the rules (as suggested by Neils van Dijke [2022-11-21 13:00]
+#
+# 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
+#
+#
+# Example 1:
+#
+# Input: @list = (1, 0, 5)
+# Output: 4
+#
+# Move #1: 1, 1, 4
+# (2nd cell gets 1 from the 3rd cell)
+#
+# Move #2: 1, 2, 3
+# (2nd cell gets 1 from the 3rd cell)
+#
+# Move #3: 2, 1, 3
+# (1st cell get 1 from the 2nd cell)
+#
+# Move #4: 2, 2, 2
+# (2nd cell gets 1 from the 3rd cell)
+#
+# Example 2:
+#
+# Input: @list = (0, 2, 0)
+# Output: -1
+#
+# It is not possible to make each same.
+#
+# Example 3:
+#
+# Input: @list = (0, 3, 0)
+# Output: 2
+#
+# Move #1: 1, 2, 0
+# (1st cell gets 1 from the 2nd cell)
+#
+# Move #2: 1, 1, 1
+# (3rd cell gets 1 from the 2nd cell)
+
+use Modern::Perl;
+use List::Util 'sum';
+
+sub equalize_ {
+ my($num_moves, @n)=@_;
+ return $num_moves if @n<2;
+
+ while (1) {
+ return $num_moves if scalar(grep {$n[0]==$n[$_]} 0..$#n)==@n;
+
+ # find biggest difference
+ my $max_diff=0; my $max_from=0; my $max_to=0;
+ for my $i (0..$#n-1) {
+ for my $j ($i+1..$#n) {
+ my $diff=abs($n[$i]-$n[$j]);
+ if ($diff>$max_diff) {
+ $max_diff=$diff;
+ if ($n[$i]>$n[$j]) {
+ ($max_from,$max_to)=($i,$j);
+ }
+ else {
+ ($max_from,$max_to)=($j,$i);
+ }
+ }
+ }
+ }
+ return -1 if $max_diff==0;
+
+ # move
+ my $dir=$max_to>$max_from ? 1 : -1;
+ $n[$max_from]--; $n[$max_from+$dir]++; $num_moves++;
+ }
+}
+
+sub equalize {
+ my(@n)=@_;
+ my $avg=sum(@n)/@n;
+ if (int($avg)==$avg) {
+ return equalize_(0, @n);
+ }
+ else {
+ return -1;
+ }
+}
+
+say equalize(@ARGV);
diff --git a/challenge-192/paulo-custodio/t/test-1.yaml b/challenge-192/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..ff69712d64
--- /dev/null
+++ b/challenge-192/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 5
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: 3
+- setup:
+ cleanup:
+ args: 6
+ input:
+ output: 1
diff --git a/challenge-192/paulo-custodio/t/test-2.yaml b/challenge-192/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..4575b0eb32
--- /dev/null
+++ b/challenge-192/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 3 3 3
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 1 0 5
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: 0 2 0
+ input:
+ output: -1
+- setup:
+ cleanup:
+ args: 0 3 0
+ input:
+ output: 2