aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-20 11:19:58 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-20 11:19:58 +0100
commit163376010349fcd0b943bead35e8cc7487bc9a54 (patch)
tree9f78903fccaaac38a1b4cef3037cbb917794eaac
parent4dd91b52dbff8e64423be8ce608d73bab64ad83e (diff)
parent48817147fca9e8b15e02473eb24f75ed633319c5 (diff)
downloadperlweeklychallenge-club-163376010349fcd0b943bead35e8cc7487bc9a54.tar.gz
perlweeklychallenge-club-163376010349fcd0b943bead35e8cc7487bc9a54.tar.bz2
perlweeklychallenge-club-163376010349fcd0b943bead35e8cc7487bc9a54.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rw-r--r--challenge-070/javier-luque/blog.txt1
-rw-r--r--challenge-070/javier-luque/perl/ch-1.pl19
-rw-r--r--challenge-070/javier-luque/perl/ch-2.pl29
-rw-r--r--challenge-070/javier-luque/raku/ch-1.p617
-rw-r--r--challenge-070/javier-luque/raku/ch-2.p627
5 files changed, 93 insertions, 0 deletions
diff --git a/challenge-070/javier-luque/blog.txt b/challenge-070/javier-luque/blog.txt
new file mode 100644
index 0000000000..0cabf620ee
--- /dev/null
+++ b/challenge-070/javier-luque/blog.txt
@@ -0,0 +1 @@
+https://perlchallenges.wordpress.com/2020/07/20/perl-weekly-challenge-070/
diff --git a/challenge-070/javier-luque/perl/ch-1.pl b/challenge-070/javier-luque/perl/ch-1.pl
new file mode 100644
index 0000000000..31ced7e1ce
--- /dev/null
+++ b/challenge-070/javier-luque/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/perl
+# Test: ./ch-1.pl
+use Modern::Perl;
+
+say swap_chars('perlandraku', 3, 4);
+
+sub swap_chars {
+ my ($S, $C, $O) = @_;
+ my @s = split('', $S);
+ my $N = scalar(@s) - 1;
+
+ for (my $i = 1; $i <= $C; $i++) {
+ my $temp = $s[$i % $N];
+ $s[$i % $N] = $s[($i + $O) % $N];
+ $s[($i + $O) % $N] = $temp;
+ }
+
+ return join '', @s;
+}
diff --git a/challenge-070/javier-luque/perl/ch-2.pl b/challenge-070/javier-luque/perl/ch-2.pl
new file mode 100644
index 0000000000..2adf21f0c7
--- /dev/null
+++ b/challenge-070/javier-luque/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+# Test: ./ch-2.pl
+use Modern::Perl;
+
+say gray_code(4);
+
+sub gray_code {
+ my $N = shift;
+ my @S1 = ('00','01','11','10');
+
+ while ($N > 2) {
+ # Flip the array
+ my @S2 = reverse @S1;
+
+ # Prefix
+ @S1 = map { '0' . $_ } @S1;
+ @S2 = map { '1' . $_ } @S2;
+
+ # Concatenate
+ push @S1, @S2;
+
+ $N--;
+ }
+
+ # Convert to decimal
+ @S1 = map { oct("0b" . $_) } @S1;
+
+ return '[' . (join ', ', @S1) . ']';
+}
diff --git a/challenge-070/javier-luque/raku/ch-1.p6 b/challenge-070/javier-luque/raku/ch-1.p6
new file mode 100644
index 0000000000..41086439c0
--- /dev/null
+++ b/challenge-070/javier-luque/raku/ch-1.p6
@@ -0,0 +1,17 @@
+# Test: perl6 ch-1.p6
+sub MAIN() {
+ say swap-chars('perlandraku', 3, 4);
+}
+
+sub swap-chars( Str $S, Int $C, Int $O) {
+ my $N = $S.chars;
+ my @s = $S.split('', :skip-empty);
+
+ loop (my $i = 1; $i <= $C; $i++) {
+ my $temp = @s[$i % $N];
+ @s[$i % $N] = @s[($i + $O) % $N];
+ @s[($i + $O) % $N] = $temp;
+ }
+
+ return @s.join('');
+}
diff --git a/challenge-070/javier-luque/raku/ch-2.p6 b/challenge-070/javier-luque/raku/ch-2.p6
new file mode 100644
index 0000000000..745f2f2f8c
--- /dev/null
+++ b/challenge-070/javier-luque/raku/ch-2.p6
@@ -0,0 +1,27 @@
+# Test: perl6 ch-2.p6
+sub MAIN() {
+ say gray-code(4);
+}
+
+sub gray-code(Int $N is copy where $N >= 2 && $N <=5 ) {
+ my @S1 = ('00','01','11','10');
+
+ while ($N > 2) {
+ # Flip the array
+ my @S2 = @S1.reverse;
+
+ # Prefix
+ @S1 = @S1.map({ '0' ~ $_ });
+ @S2 = @S2.map({ '1' ~ $_ });
+
+ # Concatenate
+ @S1 = flat @S1, @S2;
+
+ $N--;
+ }
+
+ # Convert to decimal
+ @S1 = @S1.map({ "0b$_".Int });
+
+ return @S1.perl;
+}