diff options
| author | Cris-HD <crisn7@hotmail.com> | 2021-07-16 17:52:58 +0200 |
|---|---|---|
| committer | Cris-HD <crisn7@hotmail.com> | 2021-07-16 17:52:58 +0200 |
| commit | de1b69f6eecaa2a9fe8691507f752318139743f7 (patch) | |
| tree | 242682b2967d7e999c71ebaf69db24f196ab5e10 | |
| parent | 94183600370c02d2c23aaa3fd540c32a6f1838e4 (diff) | |
| download | perlweeklychallenge-club-de1b69f6eecaa2a9fe8691507f752318139743f7.tar.gz perlweeklychallenge-club-de1b69f6eecaa2a9fe8691507f752318139743f7.tar.bz2 perlweeklychallenge-club-de1b69f6eecaa2a9fe8691507f752318139743f7.zip | |
Added challenge 121 solution
| -rwxr-xr-x | challenge-121/cristian-heredia/perl/ch_1.pl | 54 | ||||
| -rwxr-xr-x | challenge-121/cristian-heredia/python/ch_1.py | 39 |
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-121/cristian-heredia/perl/ch_1.pl b/challenge-121/cristian-heredia/perl/ch_1.pl new file mode 100755 index 0000000000..f6661bf008 --- /dev/null +++ b/challenge-121/cristian-heredia/perl/ch_1.pl @@ -0,0 +1,54 @@ +=begin + 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 +=cut + +use strict; +use warnings; +use Data::Dumper; + +my $m = 18; +my $n = 4; + +# Convert to binary +my @bits = split(//, sprintf ("%b", $m)); + +#Complete with 0 +my $counter = 8-@bits; +while ($counter!=0){ + unshift @bits, '0'; + $counter--; +} + +# invert $n bit from the end of the binary +if($bits[8-$n]==0){ + $bits[8-$n]='1'; +} +else{ + $bits[8-$n]='0'; +} +# Convert to string +my $newNumber = join( '', @bits ); +# Convert to decimal +my $result = eval("0b$newNumber"); + +print("Output: $result"); diff --git a/challenge-121/cristian-heredia/python/ch_1.py b/challenge-121/cristian-heredia/python/ch_1.py new file mode 100755 index 0000000000..d8d7cfcaf5 --- /dev/null +++ b/challenge-121/cristian-heredia/python/ch_1.py @@ -0,0 +1,39 @@ +''' + 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 +''' + +m = 12 +n = 3 + +# Convert to binary with leading 0 +bits = list("{0:08b}".format(m)) + +# invert $n bit from the end of the binary +if bits[-n] == '0': + bits[-n] = '1' +else: + bits[-n] = '0' + +# Convert to decimal string +result = int(''.join(bits), 2) +print(f"Output: {result}") + |
