aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-06-30 21:49:58 +0100
committerGitHub <noreply@github.com>2021-06-30 21:49:58 +0100
commit5b55fc4b1702ed43d1c79321353fda35e41a0b0b (patch)
tree808b386d7c3c7f6962577bf911478a6af1aac54b
parentadc400641834aec193b880b0cc4b4b9cc41d9472 (diff)
parent276f97dbb85d39a447d62612acebce83d7147209 (diff)
downloadperlweeklychallenge-club-5b55fc4b1702ed43d1c79321353fda35e41a0b0b.tar.gz
perlweeklychallenge-club-5b55fc4b1702ed43d1c79321353fda35e41a0b0b.tar.bz2
perlweeklychallenge-club-5b55fc4b1702ed43d1c79321353fda35e41a0b0b.zip
Merge pull request #4387 from adherzog/challenge-119
Add perl solutions for #119.
-rwxr-xr-xchallenge-119/adherzog/perl/ch1.pl64
-rwxr-xr-xchallenge-119/adherzog/perl/ch2.pl78
2 files changed, 142 insertions, 0 deletions
diff --git a/challenge-119/adherzog/perl/ch1.pl b/challenge-119/adherzog/perl/ch1.pl
new file mode 100755
index 0000000000..01420abd81
--- /dev/null
+++ b/challenge-119/adherzog/perl/ch1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/env perl
+
+##############################################################################
+# Perl Weekly Challenge #119
+##############################################################################
+#
+# Task #1 - Swap Nibbles
+#
+# 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.
+#
+# > A nibble is a four-bit aggregation, or half an octet.
+#
+##############################################################################
+
+use strict;
+use warnings;
+use v5.10;
+
+use Test::More;
+
+if (@ARGV) {
+ say swap_nibbles(@ARGV);
+}
+else {
+ my @tests = (
+ { input => 101, output => 86, },
+ { input => 18, output => 33, },
+
+ { input => 255, output => 255, },
+ );
+
+ for my $test (@tests) {
+ is( swap_nibbles( $test->{'input'} ), $test->{'output'}, );
+ }
+
+ done_testing();
+}
+
+exit;
+
+##############################################################################
+# We could convert the number into a binary string, and use substr or a regex
+# to extract the nibbles, concatanate them together and then convert back into
+# decimal.
+#
+# Instead, we'll use bit manipulation.
+#
+# & is bitwise AND. Using $N & 0xF0 gives us the high nibble. $N & 0x0F gives
+# us the low nibble.
+#
+# << and >> are bit shift operators. Shifting by 4 can convert the high nibble
+# into the low, and vice versa.
+#
+# Then recombine with a bitwise OR (|).
+#
+##############################################################################
+
+sub swap_nibbles {
+ my $N = shift;
+ return ( ( $N & 0xF0 ) >> 4 ) | ( ( $N & 0x0F ) << 4 );
+}
diff --git a/challenge-119/adherzog/perl/ch2.pl b/challenge-119/adherzog/perl/ch2.pl
new file mode 100755
index 0000000000..9e1135ad8b
--- /dev/null
+++ b/challenge-119/adherzog/perl/ch2.pl
@@ -0,0 +1,78 @@
+#!/usr/bin/env perl
+
+##############################################################################
+# Perl Weekly Challenge #119
+##############################################################################
+#
+# Task #2 - Sequence without 1-on-1
+#
+# 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 v5.10;
+
+use Test::More;
+
+if (@ARGV) {
+ say get_nth_term_in_sequence(@ARGV);
+}
+else {
+ my @tests = (
+ { input => 1, output => 1, },
+ { input => 2, output => 2, },
+ { input => 3, output => 3, },
+ { input => 4, output => 12, },
+ { input => 5, output => 13, },
+ { input => 6, output => 21, },
+ { input => 7, output => 22, },
+ { input => 8, output => 23, },
+ { input => 9, output => 31, },
+ { input => 10, output => 32, },
+ { input => 11, output => 33, },
+ { input => 12, output => 121, },
+ { input => 13, output => 122, },
+ { input => 14, output => 123, },
+ { input => 15, output => 131, },
+ { input => 16, output => 132, },
+ { input => 17, output => 133, },
+ { input => 18, output => 212, },
+ { input => 60, output => 2223, },
+ { input => 200, output => 31221, },
+ { input => 1000, output => 1332223, },
+ );
+
+ for my $test (@tests) {
+ my $output = get_nth_term_in_sequence( $test->{'input'} );
+ is( $output, $test->{'output'} );
+ }
+
+ done_testing();
+}
+
+exit;
+
+##############################################################################
+#
+# This is just a basic bruteforce implementation, but it works.
+#
+##############################################################################
+
+sub get_nth_term_in_sequence {
+ my $N = shift;
+
+ my $term = 0;
+ while ( $N > 0 ) {
+ $term++;
+ $N-- if ( $term =~ /^[123]+$/ && $term !~ /11/ );
+ }
+
+ return $term;
+}