aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2023-10-25 01:48:44 -0400
committerPacky Anderson <packy@cpan.org>2023-10-25 01:48:44 -0400
commitf008267ad67a177cc8a10c16b0994ee948264a29 (patch)
tree6385a4a25a5730c207cc642273091700e9b3e37a
parent9a29a498d91ce334562ece670f6774de775a04fe (diff)
downloadperlweeklychallenge-club-f008267ad67a177cc8a10c16b0994ee948264a29.tar.gz
perlweeklychallenge-club-f008267ad67a177cc8a10c16b0994ee948264a29.tar.bz2
perlweeklychallenge-club-f008267ad67a177cc8a10c16b0994ee948264a29.zip
Challenge 240 solutions by Packy Anderson
* Raku * Perl * Python 1 Blog post
-rw-r--r--challenge-240/packy-anderson/README.md81
-rw-r--r--challenge-240/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-240/packy-anderson/perl/ch-1.pl27
-rwxr-xr-xchallenge-240/packy-anderson/perl/ch-2.pl23
-rwxr-xr-xchallenge-240/packy-anderson/python/ch-1.py26
-rwxr-xr-xchallenge-240/packy-anderson/python/ch-2.py20
-rwxr-xr-xchallenge-240/packy-anderson/raku/ch-1.raku25
-rwxr-xr-xchallenge-240/packy-anderson/raku/ch-2.raku22
8 files changed, 179 insertions, 46 deletions
diff --git a/challenge-240/packy-anderson/README.md b/challenge-240/packy-anderson/README.md
index 5e38fa1145..ca5343fc14 100644
--- a/challenge-240/packy-anderson/README.md
+++ b/challenge-240/packy-anderson/README.md
@@ -1,95 +1,84 @@
# Solutions by Packy Anderson
-## Perl
+## Raku
-* [Task 1](perl/ch-1.pl)
+* [Task 1](raku/ch-1.raku)
Sample output
```
-$ perl/ch-1.pl
+$ raku/ch-1.raku
Example 1:
-Input: @arr1 = ("ab", "c")
- @arr2 = ("a", "bc")
+Input: @str = ("Perl", "Python", "Pascal")
+ $chk = ("ppp")
Output: true
-Using @arr1, word1 => "ab" . "c" => "abc"
-Using @arr2, word2 => "a" . "bc" => "abc"
-
Example 2:
-Input: @arr1 = ("ab", "c")
- @arr2 = ("ac", "b")
+Input: @str = ("Perl", "Raku")
+ $chk = ("rp")
Output: false
-Using @arr1, word1 => "ab" . "c" => "abc"
-Using @arr2, word2 => "ac" . "b" => "acb"
-
Example 3:
-Input: @arr1 = ("ab", "cd", "e")
- @arr2 = ("abcde")
+Input: @str = ("Oracle", "Awk", "C")
+ $chk = ("oac")
Output: true
-
-Using @arr1, word1 => "ab" . "cd" . "e" => "abcde"
-Using @arr2, word2 => "abcde" => "abcde"
```
-* [Task 2](perl/ch-2.pl)
+* [Task 2](raku/ch-2.raku)
Sample output
```
+$ raku/ch-2.raku
+Example 1:
+Input: @int = (0, 2, 1, 5, 3, 4)
+Output: (0, 1, 2, 4, 5, 3)
+Example 2:
+Input: @int = (5, 0, 1, 2, 3, 4)
+Output: (4, 5, 0, 1, 2, 3)
```
-## Raku
+## Perl
-* [Task 1](raku/ch-1.raku)
+* [Task 1](perl/ch-1.pl)
Sample output
```
-$ raku/ch-1.raku
+$ perl/ch-1.pl
Example 1:
-Input: @arr1 = ("ab", "c")
- @arr2 = ("a", "bc")
+Input: @str = ("Perl", "Python", "Pascal")
+ $chk = ("ppp")
Output: true
-Using @arr1, word1 => "ab" . "c" => "abc"
-Using @arr2, word2 => "a" . "bc" => "abc"
-
Example 2:
-Input: @arr1 = ("ab", "c")
- @arr2 = ("ac", "b")
+Input: @str = ("Perl", "Raku")
+ $chk = ("rp")
Output: false
-Using @arr1, word1 => "ab" . "c" => "abc"
-Using @arr2, word2 => "ac" . "b" => "acb"
-
Example 3:
-Input: @arr1 = ("ab", "cd", "e")
- @arr2 = ("abcde")
+Input: @str = ("Oracle", "Awk", "C")
+ $chk = ("oac")
Output: true
-
-Using @arr1, word1 => "ab" . "cd" . "e" => "abcde"
-Using @arr2, word2 => "abcde" => "abcde"
```
-* [Task 2](raku/ch-2.raku)
+* [Task 2](perl/ch-2.pl)
Sample output
```
+$ perl/ch-2.pl
+Example 1:
+Input: @int = (0, 2, 1, 5, 3, 4)
+Output: (0, 1, 2, 4, 5, 3)
+Example 2:
+Input: @int = (5, 0, 1, 2, 3, 4)
+Output: (4, 5, 0, 1, 2, 3)
```
## Guest Language: Python
* [Task 1](python/ch-1.py)
* [Task 2](python/ch-2.py)
-## Guest Language: Java
-
-To be completed later...
-
-## Guest Language: Elixir
-
-To be completed later...
## Blog Post
-[Perl Weekly Challenge: Now it’s the same old string, but with consistency since you’ve been gone…](https://packy.dardan.com/2023/10/16/perl-weekly-challenge-now-its-the-same-old-string-but-with-consistency-since-youve-been-gone/)
+[Perl Weekly Challenge: Building Acronym Arrays](https://packy.dardan.com/perl-weekly-challenge-building-acronym-arrays/)
diff --git a/challenge-240/packy-anderson/blog.txt b/challenge-240/packy-anderson/blog.txt
new file mode 100644
index 0000000000..4b630f6b05
--- /dev/null
+++ b/challenge-240/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/perl-weekly-challenge-building-acronym-arrays/ \ No newline at end of file
diff --git a/challenge-240/packy-anderson/perl/ch-1.pl b/challenge-240/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..a6e4fe3670
--- /dev/null
+++ b/challenge-240/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+use v5.38;
+
+use List::Util qw( reduce );
+
+sub makeAcronym {
+ my $str = shift;
+ my $acronym = reduce { $a . substr($b, 0, 1) } '', @$str;
+ return lc($acronym);
+}
+
+sub solution($chk, $str) {
+ say 'Input: @str = ("' . join('", "', @$str). '")';
+ say ' $chk = ("' . $chk . '")';
+ my ($acronym) = makeAcronym($str);
+ say 'Output: ' . ($acronym eq $chk ? 'true' : 'false');
+}
+
+say "Example 1:";
+solution("ppp", ["Perl", "Python", "Pascal"]);
+
+say "\nExample 2:";
+solution("rp", ["Perl", "Raku"]);
+
+say "\nExample 3:";
+
+solution("oac", ["Oracle", "Awk", "C"]); \ No newline at end of file
diff --git a/challenge-240/packy-anderson/perl/ch-2.pl b/challenge-240/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..c4e9485868
--- /dev/null
+++ b/challenge-240/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+use v5.38;
+
+sub buildArray(@old) {
+ my @new;
+ foreach my $i (0 .. $#old) {
+ push @new, $old[$old[$i]];
+ }
+ return @new;
+}
+
+sub solution {
+ my @int = @_;
+ say 'Input: @int = (' . join(', ', @int) . ')';
+ my @output = buildArray(@int);
+ say 'Output: (' . join(', ', @output) . ')';
+}
+
+say "Example 1:";
+solution(0, 2, 1, 5, 3, 4);
+
+say "\nExample 2:";
+solution(5, 0, 1, 2, 3, 4); \ No newline at end of file
diff --git a/challenge-240/packy-anderson/python/ch-1.py b/challenge-240/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..6eb01f2285
--- /dev/null
+++ b/challenge-240/packy-anderson/python/ch-1.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+
+from functools import reduce
+
+def makeAcronym(str_list):
+ # add empty string to beginning of list
+ str_list = [''] + str_list
+ acronym = reduce(lambda a, b: a + b[0], str_list)
+ return acronym.lower()
+
+def solution(chk, str_list):
+ as_list = '"' + '", "'.join(str_list) + '"'
+ print(f'Input: @str = ({as_list})')
+ print(f' $chk = "{chk}"')
+ acronym = makeAcronym(str_list)
+ same = 'true' if acronym == chk else 'false'
+ print(f'Output: {same}')
+
+print('Example 1:')
+solution("ppp", ["Perl", "Python", "Pascal"])
+
+print('\nExample 2:')
+solution("rp", ["Perl", "Raku"])
+
+print('\nExample 3:')
+solution("oac", ["Oracle", "Awk", "C"]) \ No newline at end of file
diff --git a/challenge-240/packy-anderson/python/ch-2.py b/challenge-240/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..555d8c77a4
--- /dev/null
+++ b/challenge-240/packy-anderson/python/ch-2.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+
+def buildArray(old):
+ new = []
+ for i in range(0, len(old)):
+ new.append(old[old[i]])
+ return new
+
+def solution(ints):
+ as_list = ', '.join(map(lambda i: str(i), ints))
+ print(f'Input: @int = ({as_list})')
+ output = buildArray(ints)
+ as_list = ', '.join(map(lambda i: str(i), output))
+ print(f'Output: ({as_list})')
+
+print('Example 1:')
+solution([0, 2, 1, 5, 3, 4])
+
+print('\nExample 2:')
+solution([5, 0, 1, 2, 3, 4]) \ No newline at end of file
diff --git a/challenge-240/packy-anderson/raku/ch-1.raku b/challenge-240/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..8370826d2b
--- /dev/null
+++ b/challenge-240/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,25 @@
+#!/usr/bin/env raku
+use v6;
+
+sub firstOfSecond { $^a ~ substr($^b, 0, 1) };
+
+sub makeAcronym(@str) {
+ my $acronym = [[&firstOfSecond]] ('', |@str);
+ return $acronym.lc;
+}
+
+sub solution($chk, @str) {
+ say 'Input: @str = ("' ~ @str.join('", "') ~ '")';
+ say ' $chk = ("' ~ $chk ~ '")';
+ my ($acronym) = makeAcronym(@str);
+ say 'Output: ' ~ ($acronym eq $chk ?? 'true' !! 'false');
+}
+
+say "Example 1:";
+solution("ppp", ["Perl", "Python", "Pascal"]);
+
+say "\nExample 2:";
+solution("rp", ["Perl", "Raku"]);
+
+say "\nExample 3:";
+solution("oac", ["Oracle", "Awk", "C"]); \ No newline at end of file
diff --git a/challenge-240/packy-anderson/raku/ch-2.raku b/challenge-240/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..40b40da644
--- /dev/null
+++ b/challenge-240/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,22 @@
+#!/usr/bin/env raku
+use v6;
+
+sub buildArray(@old) {
+ my @new;
+ for 0 .. @old.elems - 1 -> $i {
+ @new.push(@old[@old[$i]]);
+ }
+ return @new;
+}
+
+sub solution(*@int) {
+ say 'Input: @int = (' ~ @int.join(', ') ~ ')';
+ my @output = buildArray(@int);
+ say 'Output: (' ~ @output.join(', ') ~ ')';
+}
+
+say "Example 1:";
+solution(0, 2, 1, 5, 3, 4);
+
+say "\nExample 2:";
+solution(5, 0, 1, 2, 3, 4); \ No newline at end of file