aboutsummaryrefslogtreecommitdiff
path: root/challenge-121
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-17 12:07:07 +0100
committerGitHub <noreply@github.com>2021-07-17 12:07:07 +0100
commita9b6fc117801abf586e4bcc34d8f7a92a6bf0e70 (patch)
treeb11a66b15a844a02d2e8e6039e794abb99f63634 /challenge-121
parentdf7641e55490b9f9253a1452033cd89f5fd64cc9 (diff)
parent0949e4f44fa8b435ae4b54c6bfb0fee3813b9481 (diff)
downloadperlweeklychallenge-club-a9b6fc117801abf586e4bcc34d8f7a92a6bf0e70.tar.gz
perlweeklychallenge-club-a9b6fc117801abf586e4bcc34d8f7a92a6bf0e70.tar.bz2
perlweeklychallenge-club-a9b6fc117801abf586e4bcc34d8f7a92a6bf0e70.zip
Merge pull request #4538 from LubosKolouch/master
Challenge 121 Task 1 LK Perl Python
Diffstat (limited to 'challenge-121')
-rw-r--r--challenge-121/lubos-kolouch/perl/ch-1.pl34
-rw-r--r--challenge-121/lubos-kolouch/python/ch-1.py20
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-121/lubos-kolouch/perl/ch-1.pl b/challenge-121/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..b7c4d9a014
--- /dev/null
+++ b/challenge-121/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+#===============================================================================
+# DESCRIPTION: Perl Weekly Challenge #121
+# Task 1 -Invert Bit
+#
+# AUTHOR: Lubos Kolouch
+# CREATED: 20210717 04:44:33 PM
+#===============================================================================
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+sub bin2dec {
+ # shamelessly copied from The Perl Cookbook
+ return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
+}
+
+sub invert_bit{
+ my ($what, $n) = @_;
+
+ my $binary_what = sprintf ("%b",$what);
+ my @binary_arr = split //, $binary_what;
+ $binary_arr[-$n] = $binary_arr[-$n] ? 0 : 1;
+ return bin2dec(join "", @binary_arr);
+}
+
+use Test::More;
+
+is(invert_bit((12, 3)), 8);
+is(invert_bit((18, 4)), 26);
+
+done_testing;
+
diff --git a/challenge-121/lubos-kolouch/python/ch-1.py b/challenge-121/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..0e41d3a501
--- /dev/null
+++ b/challenge-121/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,20 @@
+# ===============================================================================
+# DESCRIPTION: Perl Weekly Challenge #121
+# Task 1 - Invert bit
+#
+# AUTHOR: Lubos Kolouch
+# CREATED: 20210710 04:44:33 PM
+# ===============================================================================
+
+
+def invert_bit(what: int, n: int):
+
+ binary_what = str(bin(what)[2:])
+
+ binary_arr = list(binary_what)
+ binary_arr[-n] = '0' if binary_arr[-n] == '1' else '1'
+ return int("".join(binary_arr), 2)
+
+
+assert invert_bit(12, 3) == 8
+assert invert_bit(18, 4) == 26