aboutsummaryrefslogtreecommitdiff
path: root/challenge-070
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-070')
-rwxr-xr-xchallenge-070/mohammad-anwar/perl/ch-2a.pl4
-rwxr-xr-xchallenge-070/mohammad-anwar/raku/ch-1.raku49
-rwxr-xr-xchallenge-070/mohammad-anwar/raku/ch-1a.raku33
-rwxr-xr-xchallenge-070/mohammad-anwar/raku/ch-2.raku44
-rwxr-xr-xchallenge-070/mohammad-anwar/raku/ch-2a.raku48
5 files changed, 176 insertions, 2 deletions
diff --git a/challenge-070/mohammad-anwar/perl/ch-2a.pl b/challenge-070/mohammad-anwar/perl/ch-2a.pl
index dcbb7654f8..9ec6456ef2 100755
--- a/challenge-070/mohammad-anwar/perl/ch-2a.pl
+++ b/challenge-070/mohammad-anwar/perl/ch-2a.pl
@@ -16,10 +16,10 @@ use Test::Deep;
is_deeply( [ generate_gray_code_sequence(3) ],
[ 0, 1, 3, 2, 6, 7, 5, 4 ],
- 'testing 3-bit gray code sequence' );
+ 'testing 3-bit gray code sequence.' );
is_deeply( [ generate_gray_code_sequence(4) ],
[ 0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8 ],
- 'testing 4-bit gray code sequence' );
+ 'testing 4-bit gray code sequence.' );
done_testing;
diff --git a/challenge-070/mohammad-anwar/raku/ch-1.raku b/challenge-070/mohammad-anwar/raku/ch-1.raku
new file mode 100755
index 0000000000..4dc8463077
--- /dev/null
+++ b/challenge-070/mohammad-anwar/raku/ch-1.raku
@@ -0,0 +1,49 @@
+#!/usr/bin/env raku
+
+#
+# Perl Weekly Challenge - 070
+#
+# Task #1: Character Swapping
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-070
+#
+
+use v6.d;
+
+sub MAIN(Str :$S = 'perlandraku', Int :$C = 3, Int :$O = 4) {
+ ($S, swap($S, $C, $O)).join(' => ').say;
+}
+
+sub swap(Str $string, Int $count, Int $offset) {
+
+ my $length = $string.chars;
+ my @array = $string.comb;
+ for 1..$count -> $i {
+ my $a = $i % $length;
+ my $b = ($i + $offset) % $length;
+ (@array[$a], @array[$b]) = (@array[$b], @array[$a]);
+ }
+
+ return @array.join('');
+}
+
+=finish
+my $S = $ARGV[0] || 'perlandraku';
+my $C = $ARGV[1] || 3;
+my $O = $ARGV[2] || 4;
+
+print sprintf("%s => %s\n", $S, swap($S, $C, $O));
+
+sub swap {
+ my ($string, $count, $offset) = @_;
+
+ my $length = length($string);
+ my @array = split //, $string;
+ foreach my $i (1..$count) {
+ my $a = $i % $length;
+ my $b = ($i + $offset) % $length;
+ ($array[$a], $array[$b]) = ($array[$b], $array[$a]);
+ }
+
+ return join '', @array;
+}
diff --git a/challenge-070/mohammad-anwar/raku/ch-1a.raku b/challenge-070/mohammad-anwar/raku/ch-1a.raku
new file mode 100755
index 0000000000..5a50b0f603
--- /dev/null
+++ b/challenge-070/mohammad-anwar/raku/ch-1a.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+
+#
+# Perl Weekly Challenge - 070
+#
+# Task #1: Character Swapping
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-070
+#
+
+use Test;
+
+is swap('perlandraku', 3, 4),
+ 'pndraerlaku',
+ 'testing perlandraku.';
+is swap('weeklychallenge', 5, 2),
+ 'wklycheeallenge',
+ 'testing weeklychallenge.';
+
+done-testing;
+
+sub swap(Str $string, Int $count, Int $offset) {
+
+ my $length = $string.chars;
+ my @array = $string.comb;
+ for 1..$count -> $i {
+ my $a = $i % $length;
+ my $b = ($i + $offset) % $length;
+ (@array[$a], @array[$b]) = (@array[$b], @array[$a]);
+ }
+
+ return @array.join('');
+}
diff --git a/challenge-070/mohammad-anwar/raku/ch-2.raku b/challenge-070/mohammad-anwar/raku/ch-2.raku
new file mode 100755
index 0000000000..33da502653
--- /dev/null
+++ b/challenge-070/mohammad-anwar/raku/ch-2.raku
@@ -0,0 +1,44 @@
+#!/usr/bin/env raku
+
+#
+# Perl Weekly Challenge - 070
+#
+# Task #2: Gray Code Sequence
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-070
+#
+
+use v6.d;
+
+sub MAIN(Int :$N = 3) {
+ say sprintf("%d-bit Gray Code Sequence:\n[%s]",
+ $N, generate-gray-code-sequence($N).join(', '));
+}
+
+sub generate-gray-code-sequence(Int $n) {
+
+ my %S = (
+ 2 => ['00', '01', '11', '10'],
+ );
+
+ for 3 .. $n -> $i {
+ my $S1 = %S{$i - 1};
+ my $S2 = [ |$S1.reverse ];
+ $S1.map({ $_ = '0' ~ $_ });
+ $S2.map({ $_ = '1' ~ $_ });
+ %S{$i} = [ |$S1, |$S2 ];
+ }
+
+ my @gray_codes = ();
+ for %S{$n} -> $list {
+ for |$list -> $binary {
+ @gray_codes.push: to-decimal($binary);
+ }
+ }
+
+ return |@gray_codes;
+}
+
+sub to-decimal(Str $binary) {
+ return ":2<$binary>".Int;
+}
diff --git a/challenge-070/mohammad-anwar/raku/ch-2a.raku b/challenge-070/mohammad-anwar/raku/ch-2a.raku
new file mode 100755
index 0000000000..67c94c5d6c
--- /dev/null
+++ b/challenge-070/mohammad-anwar/raku/ch-2a.raku
@@ -0,0 +1,48 @@
+#!/usr/bin/env raku
+
+#
+# Perl Weekly Challenge - 070
+#
+# Task #2: Gray Code Sequence
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-070
+#
+
+use Test;
+
+is-deeply generate-gray-code-sequence(3),
+ (0, 1, 3, 2, 6, 7, 5, 4),
+ 'testing 3-bin grey code sequence.';
+is-deeply generate-gray-code-sequence(4),
+ (0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8),
+ 'testing 4-bit grey code sequence.';
+
+done-testing;
+
+sub generate-gray-code-sequence(Int $n) {
+
+ my %S = (
+ 2 => ['00', '01', '11', '10'],
+ );
+
+ for 3 .. $n -> $i {
+ my $S1 = %S{$i - 1};
+ my $S2 = [ |$S1.reverse ];
+ $S1.map({ $_ = '0' ~ $_ });
+ $S2.map({ $_ = '1' ~ $_ });
+ %S{$i} = [ |$S1, |$S2 ];
+ }
+
+ my @gray_codes = ();
+ for %S{$n} -> $list {
+ for |$list -> $binary {
+ @gray_codes.push: to-decimal($binary);
+ }
+ }
+
+ return |@gray_codes;
+}
+
+sub to-decimal(Str $binary) {
+ return ":2<$binary>".Int;
+}