diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-07-04 21:51:12 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-07-04 21:51:12 +0100 |
| commit | 66aec5febcbf5210d80ee08b15ea4f7754c38816 (patch) | |
| tree | 77f77fe8b3fd3a427849fdd4fdb80883efe7ddb0 /challenge-119 | |
| parent | 8cab52b28709e2e730a973ed9d27068d01651a8e (diff) | |
| download | perlweeklychallenge-club-66aec5febcbf5210d80ee08b15ea4f7754c38816.tar.gz perlweeklychallenge-club-66aec5febcbf5210d80ee08b15ea4f7754c38816.tar.bz2 perlweeklychallenge-club-66aec5febcbf5210d80ee08b15ea4f7754c38816.zip | |
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-119')
| -rw-r--r-- | challenge-119/colin-crain/perl/ch-1.pl | 51 | ||||
| -rw-r--r-- | challenge-119/colin-crain/perl/ch-2.pl | 75 | ||||
| -rw-r--r-- | challenge-119/colin-crain/raku/ch-1.raku | 54 | ||||
| -rw-r--r-- | challenge-119/colin-crain/raku/ch-2.raku | 43 |
4 files changed, 223 insertions, 0 deletions
diff --git a/challenge-119/colin-crain/perl/ch-1.pl b/challenge-119/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..0b08147664 --- /dev/null +++ b/challenge-119/colin-crain/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# nibble-n-swap.pl
+#
+# Swap Nibbles
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+# To keep the task simple, we only allow integer less than or equal to
+# 255.
+#
+# Example
+# 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.
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+use 5.32.0;
+
+use warnings;
+use strict;
+use utf8;
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+my $num = shift @ARGV // 3;
+
+0 < $num < 256 or die "number must be in range 1 to 255 inclusive";
+
+my $bin = sprintf "%08b", $num;
+substr( $bin, 0, 4 ) = substr( $bin, 4, 4, substr( $bin, 0, 4));
+say oct "0b$bin";
+
diff --git a/challenge-119/colin-crain/perl/ch-2.pl b/challenge-119/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..6b58a95e74 --- /dev/null +++ b/challenge-119/colin-crain/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# chaperone-required.pl
+#
+# Sequence without 1-on-1
+# Submitted by: Cheok-Yin Fung
+# 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, …
+#
+# Example
+# Input: $N = 5
+# Output: 13
+#
+# Input: $N = 10
+# Output: 32
+#
+# Input: $N = 60
+# Output: 2223
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+use 5.32.0;
+
+use warnings;
+use strict;
+use utf8;
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+my $ele = shift // 60;
+
+say make_sequence( $ele )->[$ele-1];
+
+
+## faster, base-4
+sub make_sequence ($quan, $i = 0) {
+ my @seq;
+ while (@seq < $quan) {
+ my ($num, $rem) = (++$i, '');
+ my $val = '';
+ while ( $num > 0 ) {
+ ($num, $rem) = (int( $num/4 ), $num % 4);
+ $val = $rem . $val;
+ }
+ next if $val =~ /0|11/;
+ push @seq, $val;
+ }
+ return \@seq;
+}
+
+## simpler, but more chaff, less wheat
+sub make_sequence10 ( $quan, $i = 0 ) {
+ my @seq;
+ while ( ++$i ) {
+ next if $i =~ /[^123]|11/;
+ push @seq, $i;
+ last if @seq == $quan;
+ }
+ return \@seq;
+}
+
+# use Benchmark qw( cmpthese );
+#
+# cmpthese(0, {
+# 'four' => sub { make_sequence(50) },
+# 'ten' => sub { make_sequence10(50) } } );
+
+oct
diff --git a/challenge-119/colin-crain/raku/ch-1.raku b/challenge-119/colin-crain/raku/ch-1.raku new file mode 100644 index 0000000000..4e8ef74c1f --- /dev/null +++ b/challenge-119/colin-crain/raku/ch-1.raku @@ -0,0 +1,54 @@ +#!/usr/bin/env perl6 +# +# +# 119-1-nibble-n-swap.raku +# +# Swap Nibbles +# Submitted by: Mohammad S Anwar +# 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. +# +# To keep the task simple, we only allow integer less than or equal to +# 255. +# +# Example +# 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. +# +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN ( Int $num where 256 > $num > 0 = 3) ; + +$num.fmt("%08b") + .comb(4) + .reverse + .join + .parse-base(2) + .say; + + + + + + + + + diff --git a/challenge-119/colin-crain/raku/ch-2.raku b/challenge-119/colin-crain/raku/ch-2.raku new file mode 100644 index 0000000000..d9296fa622 --- /dev/null +++ b/challenge-119/colin-crain/raku/ch-2.raku @@ -0,0 +1,43 @@ +#!/usr/bin/env perl6 +# +# +# chaperone-required.raku +# +# Sequence without 1-on-1 +# Submitted by: Cheok-Yin Fung +# 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, … +# +# Example +# Input: $N = 5 +# Output: 13 +# +# Input: $N = 10 +# Output: 32 +# +# Input: $N = 60 +# Output: 2223 +# +# method: +# In Raku we can create an infinite sequence and access that. The raw +# numbers are first expresed in base 4, then the elements are filtered +# to only those that do not contain any didgit other than 1,2, or 3 and +# do not contain the "11" formation. Now we can take this item as a +# first-class citizen and access it positionally, reifying the +# `$ele-1`-th element and print it. We can do all of this anonymously, +# without even instatating a container to hold it. +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN ( Int $ele where $ele > 0 = 60 ) ; + +( (1 … ∞).map(*.base(4)) + .grep({! / 0 | 11 /}) )[$ele-1].say ; + |
