diff options
| author | Packy Anderson <packy@cpan.org> | 2023-10-17 23:33:07 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2023-10-17 23:33:07 -0400 |
| commit | 5d2a46a17421e69008359e30e4deb90a1a3da51b (patch) | |
| tree | 1cb19f4d39fc949d3842a5ff29ac4fa93e32867c /challenge-239 | |
| parent | afac53ab966abe7e14039640d054f82eb323097c (diff) | |
| download | perlweeklychallenge-club-5d2a46a17421e69008359e30e4deb90a1a3da51b.tar.gz perlweeklychallenge-club-5d2a46a17421e69008359e30e4deb90a1a3da51b.tar.bz2 perlweeklychallenge-club-5d2a46a17421e69008359e30e4deb90a1a3da51b.zip | |
Challenge 239 solutions by Packy Anderson
* Raku
* Perl
* Python
1 Blog post
Diffstat (limited to 'challenge-239')
| -rw-r--r-- | challenge-239/packy-anderson/README.md | 72 | ||||
| -rw-r--r-- | challenge-239/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-239/packy-anderson/perl/ch-1.pl | 38 | ||||
| -rwxr-xr-x | challenge-239/packy-anderson/perl/ch-2.pl | 29 | ||||
| -rwxr-xr-x | challenge-239/packy-anderson/python/ch-1.py | 34 | ||||
| -rwxr-xr-x | challenge-239/packy-anderson/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-239/packy-anderson/raku/ch-1.raku | 33 | ||||
| -rwxr-xr-x | challenge-239/packy-anderson/raku/ch-2.raku | 27 |
8 files changed, 231 insertions, 30 deletions
diff --git a/challenge-239/packy-anderson/README.md b/challenge-239/packy-anderson/README.md index dc1fa1c2b2..5e38fa1145 100644 --- a/challenge-239/packy-anderson/README.md +++ b/challenge-239/packy-anderson/README.md @@ -8,40 +8,35 @@ Sample output ``` $ perl/ch-1.pl Example 1: -Input: @int = (1, 2, 3, 4, 5) -Output: (1, 3, 6, 10, 15) +Input: @arr1 = ("ab", "c") + @arr2 = ("a", "bc") +Output: true + +Using @arr1, word1 => "ab" . "c" => "abc" +Using @arr2, word2 => "a" . "bc" => "abc" Example 2: -Input: @int = (1, 1, 1, 1, 1) -Output: (1, 2, 3, 4, 5) +Input: @arr1 = ("ab", "c") + @arr2 = ("ac", "b") +Output: false + +Using @arr1, word1 => "ab" . "c" => "abc" +Using @arr2, word2 => "ac" . "b" => "acb" Example 3: -Input: @int = (0, -1, 1, 2) -Output: (0, -1, 0, 2) +Input: @arr1 = ("ab", "cd", "e") + @arr2 = ("abcde") +Output: true + +Using @arr1, word1 => "ab" . "cd" . "e" => "abcde" +Using @arr2, word2 => "abcde" => "abcde" ``` * [Task 2](perl/ch-2.pl) Sample output ``` -$ perl/ch-2.pl -Example 1: -Input: @int = (15, 99, 1, 34) -Output: (1, 15, 34, 99) - -15 => 1 x 5 => 5 (1 step) -99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps) -1 (0 steps) -34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps) - -Example 2: -Input: @int = (50, 25, 33, 22) -Output: (22, 33, 50, 25) -50 => 5 x 0 => 0 (1 step) -25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps) -33 => 3 x 3 => 9 (1 step) -22 => 2 x 2 => 4 (1 step) ``` ## Raku @@ -52,22 +47,35 @@ Sample output ``` $ raku/ch-1.raku Example 1: -Input: @int = (1, 2, 3, 4, 5) -Output: (1, 3, 6, 10, 15) +Input: @arr1 = ("ab", "c") + @arr2 = ("a", "bc") +Output: true + +Using @arr1, word1 => "ab" . "c" => "abc" +Using @arr2, word2 => "a" . "bc" => "abc" Example 2: -Input: @int = (1, 1, 1, 1, 1) -Output: (1, 2, 3, 4, 5) +Input: @arr1 = ("ab", "c") + @arr2 = ("ac", "b") +Output: false + +Using @arr1, word1 => "ab" . "c" => "abc" +Using @arr2, word2 => "ac" . "b" => "acb" Example 3: -Input: @int = (0, -1, 1, 2) -Output: (0, -1, 0, 2) +Input: @arr1 = ("ab", "cd", "e") + @arr2 = ("abcde") +Output: true + +Using @arr1, word1 => "ab" . "cd" . "e" => "abcde" +Using @arr2, word2 => "abcde" => "abcde" ``` * [Task 2](raku/ch-2.raku) Sample output ``` + ``` ## Guest Language: Python @@ -78,6 +86,10 @@ Sample output To be completed later... +## Guest Language: Elixir + +To be completed later... + ## Blog Post -[Perl Weekly Challenge: Be Runnin' Up That Sum, Be Persisten' Up That Sort](https://packy.dardan.com/2023/10/09/be-runnin-up-that-sum-be-persisten-up-that-sort/) +[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/) diff --git a/challenge-239/packy-anderson/blog.txt b/challenge-239/packy-anderson/blog.txt new file mode 100644 index 0000000000..cddc521587 --- /dev/null +++ b/challenge-239/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/2023/10/16/perl-weekly-challenge-now-its-the-same-old-string-but-with-consistency-since-youve-been-gone/
\ No newline at end of file diff --git a/challenge-239/packy-anderson/perl/ch-1.pl b/challenge-239/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..f8138a9ff7 --- /dev/null +++ b/challenge-239/packy-anderson/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( reduce ); + +sub concatString { + my($num, $arr) = @_; + my $word = reduce { $a . $b } @$arr; + my $words = "\nUsing \@arr$num, word$num => \"" + . join('" . "', @$arr) + . '" => "' . $word . '"'; + return ($word, $words); +} + +sub sameString { + my ($arr1, $arr2) = @_; + my ($word1, $words1) = concatString(1, $arr1); + my ($word2, $words2) = concatString(2, $arr2); + return($word1 eq $word2, $words1 . $words2); +} + +sub solution { + my ($arr1, $arr2) = @_; + say 'Input: @arr1 = ("' . join('", "', @$arr1) . '")'; + say ' @arr2 = ("' . join('", "', @$arr2) . '")'; + my ($same, $words) = sameString($arr1, $arr2); + say 'Output: ' . ($same ? 'true' : 'false'); + say $words; +} + +say "Example 1:"; +solution(["ab", "c"], ["a", "bc"]); + +say "\nExample 2:"; +solution(["ab", "c"], ["ac", "b"]); + +say "\nExample 3:"; +solution(["ab", "cd", "e"], ["abcde"]);
\ No newline at end of file diff --git a/challenge-239/packy-anderson/perl/ch-2.pl b/challenge-239/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..a96326b8f3 --- /dev/null +++ b/challenge-239/packy-anderson/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use v5.38; + +sub consistentCount { + my($allowed, $str) = @_; + my $regex = '^[' . $allowed . ']+$'; + my $count = 0; + foreach my $s ( @$str ) { + $count++ if $s =~ /$regex/; + } + return $count; +} + +sub solution { + my($allowed, $str) = @_; + say 'Input: @str = ("' . join('", "', @$str) . '")'; + say ' $allowed = "' . $allowed . '"'; + my $output = consistentCount($allowed, $str); + say 'Output: ' . $output; +} + +say "Example 1:"; +solution("ab", ["ad", "bd", "aaab", "baa", "badab"]); + +say "\nExample 2:"; +solution("abc", ["a", "b", "c", "ab", "ac", "bc", "abc"]); + +say "\nExample 3:"; +solution("cad", ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"]);
\ No newline at end of file diff --git a/challenge-239/packy-anderson/python/ch-1.py b/challenge-239/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..15924b4037 --- /dev/null +++ b/challenge-239/packy-anderson/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +from functools import reduce + +def concatString(num, arr): + word = reduce(lambda a, b: a + b, arr) + words = ( + f'\nUsing @arr{num}, word{num} => "' + + "' . '".join(arr) + + f'" => "{word}"' + ) + return word, words + +def sameString(arr1, arr2): + word1, words1 = concatString(1, arr1) + word2, words2 = concatString(2, arr2) + return(word1 == word2, words1 + words2) + +def solution(arr1, arr2): + as_list = '"' + '", "'.join(arr1) + '"' + print(f'Input: @arr1 = ({as_list})') + as_list = '"' + '", "'.join(arr1) + '"' + print(f' @arr2 = ({as_list})') + same, words = sameString(arr1, arr2) + print(f'Output: {same}\n{words}') + +print('Example 1:') +solution(["ab", "c"], ["a", "bc"]) + +print('\nExample 2:') +solution(["ab", "c"], ["ac", "b"]) + +print('\nExample 3:') +solution(["ab", "cd", "e"], ["abcde"])
\ No newline at end of file diff --git a/challenge-239/packy-anderson/python/ch-2.py b/challenge-239/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..8a451a6fe8 --- /dev/null +++ b/challenge-239/packy-anderson/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +import re + +def consistentCount(allowed, str): + regex = re.compile('^[' + allowed + ']+$') + count = 0 + for s in str: + if regex.match(s): + count += 1 + return count + +def solution(allowed, str): + as_list = '"' + '", "'.join(str) + '"' + print(f'Input: @str = ({as_list})') + print(f' $allowed = "{allowed}"') + output = consistentCount(allowed, str) + print(f'Output: {output}') + +print('Example 1:') +solution("ab", ["ad", "bd", "aaab", "baa", "badab"]) + +print('\nExample 2:') +solution("abc", ["a", "b", "c", "ab", "ac", "bc", "abc"]) + +print('\nExample 3:') +solution("cad", ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"])
\ No newline at end of file diff --git a/challenge-239/packy-anderson/raku/ch-1.raku b/challenge-239/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..7c4a7f4dbc --- /dev/null +++ b/challenge-239/packy-anderson/raku/ch-1.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku +use v6; + +sub concatString($num, @arr) { + my $word = [~] @arr; + my $words = "\nUsing \@arr$num, word$num => \"" + ~ @arr.join('" . "') + ~ '" => "' ~ $word ~ '"'; + return ($word, $words); +} + +sub sameString(@arr1, @arr2) { + my ($word1, $words1) = concatString(1, @arr1); + my ($word2, $words2) = concatString(2, @arr2); + return($word1 eq $word2, $words1 ~ $words2); +} + +sub solution(@arr1, @arr2) { + say 'Input: @arr1 = ("' ~ @arr1.join('", "') ~ '")'; + say ' @arr2 = ("' ~ @arr2.join('", "') ~ '")'; + my ($same, $words) = sameString(@arr1, @arr2); + say 'Output: ' ~ ($same ?? 'true' !! 'false'); + say $words; +} + +say "Example 1:"; +solution(["ab", "c"], ["a", "bc"]); + +say "\nExample 2:"; +solution(["ab", "c"], ["ac", "b"]); + +say "\nExample 3:"; +solution(["ab", "cd", "e"], ["abcde"]);
\ No newline at end of file diff --git a/challenge-239/packy-anderson/raku/ch-2.raku b/challenge-239/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..1d5ba6b465 --- /dev/null +++ b/challenge-239/packy-anderson/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/env raku +use v6; + +sub consistentCount($allowed, @str) { + my $regex = '^ <[' ~ $allowed ~ ']>+ $'; + my $count = 0; + for @str -> $s { + $count++ if $s.match: / <$regex> /; + } + return $count; +} + +sub solution($allowed, @str) { + say 'Input: @str = ("' ~ @str.join('", "') ~ '")'; + say ' $allowed = "' ~ $allowed ~ '"'; + my $output = consistentCount($allowed, @str); + say 'Output: ' ~ $output; +} + +say "Example 1:"; +solution("ab", ["ad", "bd", "aaab", "baa", "badab"]); + +say "\nExample 2:"; +solution("abc", ["a", "b", "c", "ab", "ac", "bc", "abc"]); + +say "\nExample 3:"; +solution("cad", ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"]);
\ No newline at end of file |
