aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-31 05:15:29 +0100
committerGitHub <noreply@github.com>2021-05-31 05:15:29 +0100
commit39101d6a6d2e421b9f7d597bb130e8ebdb8a1d55 (patch)
tree7811179d2b5e47797813a5387589b5b35489e735
parentaa519f5a1be2a316fe104086fc1eb52e3a819dcc (diff)
parentf7817d4f601fb7f039b610162ce4cdf7a3c2297a (diff)
downloadperlweeklychallenge-club-39101d6a6d2e421b9f7d597bb130e8ebdb8a1d55.tar.gz
perlweeklychallenge-club-39101d6a6d2e421b9f7d597bb130e8ebdb8a1d55.tar.bz2
perlweeklychallenge-club-39101d6a6d2e421b9f7d597bb130e8ebdb8a1d55.zip
Merge pull request #4170 from dms061/challenge114
Challenge114
-rw-r--r--challenge-114/dms061/perl/ch-1-example.txt8
-rw-r--r--challenge-114/dms061/perl/ch-1.pl24
-rw-r--r--challenge-114/dms061/perl/ch-2-example.txt8
-rw-r--r--challenge-114/dms061/perl/ch-2.pl35
4 files changed, 75 insertions, 0 deletions
diff --git a/challenge-114/dms061/perl/ch-1-example.txt b/challenge-114/dms061/perl/ch-1-example.txt
new file mode 100644
index 0000000000..12dc011d19
--- /dev/null
+++ b/challenge-114/dms061/perl/ch-1-example.txt
@@ -0,0 +1,8 @@
+> perl ch-1.pl 1234
+1331
+> perl ch-1.pl 999
+1001
+> perl ch-1.pl 805
+808
+> perl ch-1.pl 5962
+5995
diff --git a/challenge-114/dms061/perl/ch-1.pl b/challenge-114/dms061/perl/ch-1.pl
new file mode 100644
index 0000000000..89aaf72343
--- /dev/null
+++ b/challenge-114/dms061/perl/ch-1.pl
@@ -0,0 +1,24 @@
+=pod
+
+=head1 Question 1:
+
+Next Palindrome Number
+You are given a positive integer $N
+Write a script to find out the next Palindrome Number higher than the given integer $N
+
+=head1 Approach
+
+Going to keep this one simple, increase $N until it becomes a palidrome.
+
+=cut
+
+use strict;
+use warnings;
+
+die "Need to supply a positive integer (as a command line argument)!\n" if @ARGV == 0;
+my $n = shift;
+die "Input must be positive\n" if $n < 1;
+
+# Increase n until we have a new palindrome
+do { ++$n; } until $n eq reverse $n;
+print "$n\n";
diff --git a/challenge-114/dms061/perl/ch-2-example.txt b/challenge-114/dms061/perl/ch-2-example.txt
new file mode 100644
index 0000000000..3b98a0cc2c
--- /dev/null
+++ b/challenge-114/dms061/perl/ch-2-example.txt
@@ -0,0 +1,8 @@
+> perl ch-2.pl 3
+5
+> perl ch-2.pl 12
+17
+> perl ch-2.pl 16
+32
+> perl ch-2.pl 19
+21
diff --git a/challenge-114/dms061/perl/ch-2.pl b/challenge-114/dms061/perl/ch-2.pl
new file mode 100644
index 0000000000..42b98003a1
--- /dev/null
+++ b/challenge-114/dms061/perl/ch-2.pl
@@ -0,0 +1,35 @@
+=pod
+
+=head1 Question 2
+
+Higher Integer Set Bits
+You are given a positive integer $N.
+Write a script to find the next higher integer having the same number of 1 bits in binary representation as $N
+
+=head1 Approach
+
+Also starting with a simple approach. Count 1s in bitstring of $N and increase $N until
+we have another bitstring with the same number of 1s
+
+Should check on this approach again later to exploit the properties of binary representation to determine
+an easier way than iterating through
+
+=cut
+
+sub count_ones {
+ my $n = shift;
+ # sprintf "%b, $n" creates a bitstring for $n and
+ # tr/1// counts the number of 1s in the string
+ return sprintf("%b", $n) =~ tr/1//;
+}
+
+die "Need to enter a positive integer!\n" if @ARGV == 0;
+
+my $n = shift;
+die "Integer needs to be positive!\n" if $n < 1;
+
+my $count = count_ones $n;
+# Iterate until we have a match
+do { ++$n; } until count_ones ($n) == $count;
+print "$n\n";
+