aboutsummaryrefslogtreecommitdiff
path: root/challenge-121
diff options
context:
space:
mode:
authorPerlMonk-Athanasius <PerlMonk.Athanasius@gmail.com>2021-07-18 21:39:32 +1000
committerPerlMonk-Athanasius <PerlMonk.Athanasius@gmail.com>2021-07-18 21:39:32 +1000
commit1d7f59ab036e632f99bef53ede1ba254d9238fe0 (patch)
treec3bdf1615647830175b0d7b15ea0cc154d009e39 /challenge-121
parent540074d986e970c6c396d17f2889efb528a9f0bf (diff)
downloadperlweeklychallenge-club-1d7f59ab036e632f99bef53ede1ba254d9238fe0.tar.gz
perlweeklychallenge-club-1d7f59ab036e632f99bef53ede1ba254d9238fe0.tar.bz2
perlweeklychallenge-club-1d7f59ab036e632f99bef53ede1ba254d9238fe0.zip
Perl & Raku solutions to Task 1 of the Perl Weekly Challenge #121
Diffstat (limited to 'challenge-121')
-rw-r--r--challenge-121/athanasius/perl/ch-1.pl127
-rw-r--r--challenge-121/athanasius/raku/ch-1.raku93
2 files changed, 220 insertions, 0 deletions
diff --git a/challenge-121/athanasius/perl/ch-1.pl b/challenge-121/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..edf78499b6
--- /dev/null
+++ b/challenge-121/athanasius/perl/ch-1.pl
@@ -0,0 +1,127 @@
+#!perl
+
+###############################################################################
+=comment
+
+Perl Weekly Challenge 121
+=========================
+
+TASK #1
+-------
+*Invert Bit*
+
+Submitted by: Mohammad S Anwar
+
+You are given integers 0 <= $m <= 255 and 1 <= $n <= 8.
+
+Write a script to invert $n bit from the end of the binary representation of $m
+and print the decimal representation of the new binary number.
+
+Example
+
+ Input: $m = 12, $n = 3
+ Output: 8
+
+ Binary representation of $m = 00001100
+ Invert 3rd bit from the end = 00001000
+ Decimal equivalent of 00001000 = 8
+
+ Input $m = 18, $n = 4
+ Output: 26
+
+ Binary representation of $m = 00010010
+ Invert 4th bit from the end = 00011010
+ Decimal equivalent of 00011010 = 26
+
+=cut
+###############################################################################
+
+#--------------------------------------#
+# Copyright © 2021 PerlMonk Athanasius #
+#--------------------------------------#
+
+use strict;
+use warnings;
+use Const::Fast;
+use Regexp::Common qw( number );
+
+const my $VERBOSE => 1;
+const my $USAGE =>
+"Usage:
+ perl $0 <m> <n>
+
+ <m> Decimal: an integer between 0 and 255
+ <n> Bit count: an integer between 1 and 8\n";
+
+#------------------------------------------------------------------------------
+BEGIN
+#------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 121, Task #1: Invert Bit (Perl)\n\n";
+}
+
+#==============================================================================
+MAIN:
+#==============================================================================
+{
+ my ($m, $n) = parse_command_line();
+
+ print "Input: \$m = $m, \$n = $n\n";
+
+ my $m_bin = sprintf '%08b', $m;
+ my @bin = split '', $m_bin;
+ my $idx = 8 - $n;
+
+ $bin[ $idx ] = $bin[ $idx ] == 1 ? 0 : 1;
+
+ my $s_bin = join '', @bin;
+ my $s_dec = oct "0b$s_bin";
+
+ print "Output: $s_dec\n";
+
+ if ($VERBOSE)
+ {
+ printf "\nBinary representation of \$m = %s\n" .
+ "Invert %d%s bit from the end = %s\n" .
+ "And the decimal equivalent of %s = %d\n",
+ $m_bin, $n, ($n == 1 ? 'st' :
+ $n == 2 ? 'nd' :
+ $n == 3 ? 'rd' : 'th'), $s_bin, $s_bin, $s_dec;
+ }
+}
+
+#------------------------------------------------------------------------------
+sub parse_command_line
+#------------------------------------------------------------------------------
+{
+ my $args = scalar @ARGV;
+ $args == 2 or error( "Expected 2 command line arguments, found $args" );
+
+ my ($m, $n) = @ARGV;
+
+ $m =~ / ^ $RE{num}{int} $ /x
+ or error( qq[\$m: "$m" is not a valid integer] );
+ $m += 0; # Normalize: e.g., 010 --> 10
+ $m >= 0 or error( "\$m: $m is less than 0" );
+ $m <= 255 or error( "\$m: $m is greater than 255" );
+
+ $n =~ / ^ $RE{num}{int} $ /x
+ or error( qq[\$n: "$n" is not a valid integer] );
+ $n += 0; # Normalize: e.g., 08 --> 8
+ $n >= 1 or error( "\$n: $n is less than 1" );
+ $n <= 8 or error( "\$n: $n is greater than 8" );
+
+ return ($m, $n);
+}
+
+#------------------------------------------------------------------------------
+sub error
+#------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+###############################################################################
diff --git a/challenge-121/athanasius/raku/ch-1.raku b/challenge-121/athanasius/raku/ch-1.raku
new file mode 100644
index 0000000000..25b40f483b
--- /dev/null
+++ b/challenge-121/athanasius/raku/ch-1.raku
@@ -0,0 +1,93 @@
+use v6d;
+
+###############################################################################
+=begin comment
+
+Perl Weekly Challenge 121
+=========================
+
+TASK #1
+-------
+*Invert Bit*
+
+Submitted by: Mohammad S Anwar
+
+You are given integers 0 <= $m <= 255 and 1 <= $n <= 8.
+
+Write a script to invert $n bit from the end of the binary representation of $m
+and print the decimal representation of the new binary number.
+
+Example
+
+ Input: $m = 12, $n = 3
+ Output: 8
+
+ Binary representation of $m = 00001100
+ Invert 3rd bit from the end = 00001000
+ Decimal equivalent of 00001000 = 8
+
+ Input $m = 18, $n = 4
+ Output: 26
+
+ Binary representation of $m = 00010010
+ Invert 4th bit from the end = 00011010
+ Decimal equivalent of 00011010 = 26
+
+=end comment
+###############################################################################
+
+#--------------------------------------#
+# Copyright © 2021 PerlMonk Athanasius #
+#--------------------------------------#
+
+my Bool constant $VERBOSE = True;
+
+#------------------------------------------------------------------------------
+BEGIN
+#------------------------------------------------------------------------------
+{
+ "\nChallenge 121, Task #1: Invert Bit (Raku)\n".put;
+}
+
+#==============================================================================
+sub MAIN
+(
+ Int:D $m where { 0 <= $m <= 255 }, #= Decimal: an integer between 0 and 255
+ Int:D $n where { 1 <= $n <= 8 } #= Bit count: an integer between 1 and 8
+)
+#==============================================================================
+{
+ "Input: \$m = %d, \$n = %d\n".printf: $m + 0, $n + 0; # Normalize
+
+ my Str $m-bin = '%08b'.sprintf: $m;
+ my UInt @bin = $m-bin.split( '', :skip-empty ).map: { .Int };
+ my UInt $idx = 8 - $n;
+ @bin[ $idx ] = @bin[ $idx ] == 1 ?? 0 !! 1;
+ my Str $s-bin = @bin.join;
+ my UInt $s-dec = :2( $s-bin );
+
+ "Output: $s-dec".put;
+
+ if $VERBOSE
+ {
+ ("\nBinary representation of \$m = %s\n" ~
+ "Invert %d%s bit from the end = %s\n" ~
+ "And the decimal equivalent of %s = %d\n").printf:
+ $m-bin, $n, ($n == 1 ?? 'st' !!
+ $n == 2 ?? 'nd' !!
+ $n == 3 ?? 'rd' !! 'th'), $s-bin, $s-bin, $s-dec;
+ }
+}
+
+#------------------------------------------------------------------------------
+sub USAGE()
+#------------------------------------------------------------------------------
+{
+ my Str $usage = $*USAGE;
+
+ $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/;
+
+ $usage.put;
+}
+
+##############################################################################