diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-07-12 14:18:08 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-07-16 16:34:53 +0200 |
| commit | 7c09c15318feae0b8b67cda3b6fc8cc768ffbd9f (patch) | |
| tree | 242f655ad718f434ce1c099f9aee6c2923f81eca | |
| parent | 93e61f41948cb7e7bf309a45cb4001e036e8dc91 (diff) | |
| download | perlweeklychallenge-club-7c09c15318feae0b8b67cda3b6fc8cc768ffbd9f.tar.gz perlweeklychallenge-club-7c09c15318feae0b8b67cda3b6fc8cc768ffbd9f.tar.bz2 perlweeklychallenge-club-7c09c15318feae0b8b67cda3b6fc8cc768ffbd9f.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-121/jo-37/perl/ch-1.pl | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/challenge-121/jo-37/perl/ch-1.pl b/challenge-121/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..be854bb6df --- /dev/null +++ b/challenge-121/jo-37/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [-tests] [M N] + +-examples + run the examples from the challenge + +-tests + run some tests + +M + number to be processed + +N + bit position + +EOS + + +### Input and Output + +say invert_nth_bit($ARGV[0], $ARGV[1]); + + +### Implementation + +sub invert_nth_bit ($m, $n) { + $m ^ 1 << ($n - 1); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is invert_nth_bit(12, 3), 8, 'example 1'; + is invert_nth_bit(18, 4), 26, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is invert_nth_bit(0, 32), 2 ** 31, 'not limited to N <= 8'; + is invert_nth_bit(2 ** 31, 32), 0, 'not limited to M < 256'; + } + + done_testing; + exit; +} |
