From 0949e4f44fa8b435ae4b54c6bfb0fee3813b9481 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 17 Jul 2021 12:43:34 +0200 Subject: Challenge 121 Task 1 LK Perl Python --- challenge-121/lubos-kolouch/python/ch-1.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 challenge-121/lubos-kolouch/python/ch-1.py (limited to 'challenge-121/lubos-kolouch/python/ch-1.py') 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 -- cgit