diff options
| author | wanderdoc <wanderdoc@googlemail.com> | 2020-07-21 19:54:59 +0200 |
|---|---|---|
| committer | wanderdoc <wanderdoc@googlemail.com> | 2020-07-21 19:54:59 +0200 |
| commit | 760536a04a3cfcf7eb8e0982e011791b55aeef9c (patch) | |
| tree | e4d9a423c1d0205b8b32454eee7ad636e48d1dae /challenge-070 | |
| parent | 5b33e58cd8dbd0e9af73146f9169b568c139b851 (diff) | |
| download | perlweeklychallenge-club-760536a04a3cfcf7eb8e0982e011791b55aeef9c.tar.gz perlweeklychallenge-club-760536a04a3cfcf7eb8e0982e011791b55aeef9c.tar.bz2 perlweeklychallenge-club-760536a04a3cfcf7eb8e0982e011791b55aeef9c.zip | |
Solutions to challenge-070.
Diffstat (limited to 'challenge-070')
| -rw-r--r-- | challenge-070/wanderdoc/perl/ch-1.pl | 63 | ||||
| -rw-r--r-- | challenge-070/wanderdoc/perl/ch-2.pl | 57 |
2 files changed, 120 insertions, 0 deletions
diff --git a/challenge-070/wanderdoc/perl/ch-1.pl b/challenge-070/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..99d1b8d25a --- /dev/null +++ b/challenge-070/wanderdoc/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string $S of size $N. You are also given swap count $C and offset $O such that $C >= 1, $O >= 1 and $C + $O <= $N. + +Write a script to perform character swapping like below: +$S[ 1 % $N ] <=> $S[ (1 + $O) % $N ] +$S[ 2 % $N ] <=> $S[ (2 + $O) % $N ] +$S[ 3 % $N ] <=> $S[ (3 + $O) % $N ] ... +$S[ $C % $N ] <=> $S[ ($C + $O) % $N ] + +Example 1 +Input: $S = 'perlandraku' $C = 3 $O = 4 + +Character Swapping: + swap 1: e <=> n = pnrlaedraku + swap 2: r <=> d = pndlaerraku + swap 3: l <=> r = pndraerlaku + +Output: pndraerlaku + +Example 2 +Input: $S = 'weeklychallenge' $C = 5 $O = 2 + +Character Swapping: + swap 1: e <=> k = wkeelychallenge + swap 2: e <=> l = wkleeychallenge + swap 3: e <=> y = wklyeechallenge + swap 4: e <=> c = wklyceehallenge + swap 5: e <=> h = wklycheeallenge + +Output: wklycheeallenge +=cut + +use Test::More; + + + +sub swapping +{ + my $string = $_[0]; + + my $count = $_[1]; + my $offset = $_[2]; + my $len = length($string); + + + my $i = 1; + + while ( $i <= $count ) + { + (substr($string, $i % $len, 1), substr($string, ($i + $offset) % $len, 1)) = + (substr($string, ($i + $offset) % $len, 1), substr($string, $i % $len, 1)); + $i++; + } + + return $string; +} +is(swapping('perlandraku', 3, 4), 'pndraerlaku', 'Example 1'); +is(swapping('weeklychallenge', 5, 2), 'wklycheeallenge', 'Example 2'); +done_testing();
\ No newline at end of file diff --git a/challenge-070/wanderdoc/perl/ch-2.pl b/challenge-070/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..a579b1c9ce --- /dev/null +++ b/challenge-070/wanderdoc/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an integer 2 <= $N <= 5. + +Write a script to generate $N-bit gray code sequence. +To generate the 3-bit Gray code sequence from the 2-bit Gray code sequence, follow the step below: + +2-bit Gray Code sequence [0, 1, 3, 2] + + +Binary form of the sequence a) S1 = [00, 01, 11, 10] +Reverse of S1 b) S2 = [10, 11, 01, 00] +Prefix all entries of S1 with '0' c) S1 = [000, 001, 011, 010] +Prefix all entries of S2 with '1' d) S2 = [110, 111, 101, 100] +Concatenate S1 and S2 gives 3-bit Gray Code sequence e) [000, 001, 011, 010, 110, 111, 101, 100] +3-bit Gray Code sequence [0, 1, 3, 2, 6, 7, 5, 4] + +Example +Input: $N = 4 +Output: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8] + +=cut + +use Test::More; +sub gray_code +{ + my @seq = @{$_[0]}; + my $cnt = $_[1]; + + my $bit = $_[2]; + + while ( $cnt < $bit ) + { + @seq = map sprintf("%0${cnt}b",$_), @seq; + my @seq_2 = reverse @seq; + + + @seq = map '0' . $_, @seq; + @seq_2 = map '1' . $_, @seq_2; + push @seq, @seq_2; + + @seq = map oct("0b".$_), @seq; + $cnt++; + } + return @seq; + +} + +is_deeply([gray_code([0, 1, 3, 2], 2, 3)], ([0, 1, 3, 2, 6, 7, 5, 4]), 'Example 1'); +is_deeply([gray_code([0, 1, 3, 2], 2, 4)], + ([0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8]), 'Example 2'); +is_deeply([gray_code([0, 1, 3, 2, 6, 7, 5, 4], 3, 4)], + ([0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8]), 'Variation'); +done_testing();
\ No newline at end of file |
