aboutsummaryrefslogtreecommitdiff
path: root/challenge-119
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-07 15:42:21 +0100
committerGitHub <noreply@github.com>2021-07-07 15:42:21 +0100
commitd2d2812e9077bb1e4295480d789370fb169e95c0 (patch)
tree04bcf15845f82683a295b812c19840aa8b21965b /challenge-119
parent3c76f4064ce5e5014ea94ad0ee314ac54557e090 (diff)
parent66cea3b9b11f47e8ddad959ccabbf0b1018b5dde (diff)
downloadperlweeklychallenge-club-d2d2812e9077bb1e4295480d789370fb169e95c0.tar.gz
perlweeklychallenge-club-d2d2812e9077bb1e4295480d789370fb169e95c0.tar.bz2
perlweeklychallenge-club-d2d2812e9077bb1e4295480d789370fb169e95c0.zip
Merge pull request #4454 from corvettes13/master
Pull Request Challenge 119
Diffstat (limited to 'challenge-119')
-rw-r--r--challenge-119/paul-fajman/perl/ch-1.pl55
-rw-r--r--challenge-119/paul-fajman/perl/ch-2.pl58
2 files changed, 113 insertions, 0 deletions
diff --git a/challenge-119/paul-fajman/perl/ch-1.pl b/challenge-119/paul-fajman/perl/ch-1.pl
new file mode 100644
index 0000000000..cd2b2f0165
--- /dev/null
+++ b/challenge-119/paul-fajman/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#/usr/bin/perl
+
+# Weekly Challenge 119 Task #1
+# You are given a positive integer $N.
+# Write a script to swap the two nibbles of the binary representation of the
+# given number and print the decimal number of the new binary representation.
+#
+# Input: $N = 101
+# Output: 86
+
+# Binary representation of decimal 101 is 1100101 or as 2 nibbles (0110)(0101).
+# The swapped nibbles would be (0101)(0110) same as decimal 86.
+
+# Input: $N = 18
+# Output: 33
+
+# Binary representation of decimal 18 is 10010 or as 2 nibbles (0001)(0010).
+# The swapped nibbles would be (0010)(0001) same as decimal 33.
+##############################
+
+use strict;
+use warnings;
+
+use POSIX;
+
+my $int = $ARGV[0];
+my $quo = $int;
+my ($i, $rem);
+my $total = 0;
+my @nibbles=();
+
+# Calculate binary number
+while($quo ne 0) {
+ $rem = floor($quo % 2);
+ unshift @nibbles, $rem;
+ $quo = floor($quo/2);
+}
+
+# Check that final number is 8 digits
+while ($#nibbles+1 lt 8) {
+ unshift @nibbles, 0;
+}
+
+# Swap the nibbles
+my @final = splice(@nibbles, 4);
+@final = (@final, @nibbles);
+
+# Calcuate the new decimal number
+for ($i=7; $i>-1; $i--) {
+ $total+= $final[$i]*(2**(7-$i));
+}
+
+print "Input: \$N = $int\n";
+print "Output: $total\n";
+
diff --git a/challenge-119/paul-fajman/perl/ch-2.pl b/challenge-119/paul-fajman/perl/ch-2.pl
new file mode 100644
index 0000000000..f764ea153a
--- /dev/null
+++ b/challenge-119/paul-fajman/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#/usr/bin/perl
+
+# Weekly Challenge 119 Task #2
+# Write a script to generate sequence starting at 1. Consider the
+# increasing sequence of integers which contain only 1’s, 2’s and
+# 3’s, and do not have any doublets of 1’s like below. Please
+# accept a positive integer $N and print the $Nth term in the
+# generated sequence.
+
+# 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131
+
+use strict;
+use warnings;
+
+use POSIX;
+
+my $input = $ARGV[0];
+my @numbers;
+
+my ($quo, $rem, $i);
+my $check=0;
+my ($oneone, $output);
+
+for ($i=1;$i<$input+1;$i++) {
+ $quo = $i;
+ # Calculate base 4 number
+ # Later throw out any values with 0
+ while ($quo ne 0) {
+ $rem = floor($quo % 4);
+ unshift @numbers, $rem;
+ $quo = floor($quo/4);
+ }
+ # Check for any digit with a 0
+ foreach (@numbers) {
+ if ($_ eq 0) {
+ $check=1;
+ next;
+ }
+ $oneone.=$_;
+ }
+
+ # Check if any digits are consecutive ones or if one was a 0;
+ if ($oneone =~ m/11/ || $check eq 1) {
+ undef($oneone);
+ undef(@numbers);
+ $check = 0;
+ $input++;
+ next;
+ }
+
+ $output = $oneone;
+ undef(@numbers);
+ undef($oneone);
+}
+
+print "INPUT: \$N = $ARGV[0]\n";
+print "OUTPUT: $output\n";
+