diff options
| author | Packy Anderson <packy@cpan.org> | 2024-02-16 01:14:32 -0500 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-02-16 01:14:32 -0500 |
| commit | d23fbdd681ca2a9b02c5cb2234caa61d5ec32cef (patch) | |
| tree | 5a60957ab7b6f8f3907682e982d9ce5560ee29e8 | |
| parent | c9792b8a02f80c49e9bb4f0a1056c98d41e693a4 (diff) | |
| download | perlweeklychallenge-club-d23fbdd681ca2a9b02c5cb2234caa61d5ec32cef.tar.gz perlweeklychallenge-club-d23fbdd681ca2a9b02c5cb2234caa61d5ec32cef.tar.bz2 perlweeklychallenge-club-d23fbdd681ca2a9b02c5cb2234caa61d5ec32cef.zip | |
Challenge 256 solutions by Packy Anderson
* Raku
* Perl
* Python
1 Blog post
| -rw-r--r-- | challenge-256/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-256/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-256/packy-anderson/perl/ch-1.pl | 38 | ||||
| -rwxr-xr-x | challenge-256/packy-anderson/perl/ch-2.pl | 25 | ||||
| -rwxr-xr-x | challenge-256/packy-anderson/python/ch-1.py | 36 | ||||
| -rwxr-xr-x | challenge-256/packy-anderson/python/ch-2.py | 23 | ||||
| -rwxr-xr-x | challenge-256/packy-anderson/raku/ch-1.raku | 38 | ||||
| -rwxr-xr-x | challenge-256/packy-anderson/raku/ch-2.raku | 25 |
8 files changed, 187 insertions, 1 deletions
diff --git a/challenge-256/packy-anderson/README.md b/challenge-256/packy-anderson/README.md index 5509e1d05e..c576612b58 100644 --- a/challenge-256/packy-anderson/README.md +++ b/challenge-256/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[Odd Char Seems to be the Most Frequent Word](https://packy.dardan.com/b/HM) +[Merge the Maximum String Pairs](https://packy.dardan.com/b/Hg) diff --git a/challenge-256/packy-anderson/blog.txt b/challenge-256/packy-anderson/blog.txt new file mode 100644 index 0000000000..a8eb5c3690 --- /dev/null +++ b/challenge-256/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Hg
\ No newline at end of file diff --git a/challenge-256/packy-anderson/perl/ch-1.pl b/challenge-256/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..774efe549e --- /dev/null +++ b/challenge-256/packy-anderson/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +use v5.38; + +sub maximumPairs(@words) { + my $count = 0; + while (@words) { + # the the first word off the list + my $first = shift @words; + # now compare to the rest of the words in the list + for my $i (0 .. $#words) { + my $second = $words[$i]; + if ($first eq reverse $second) { + # we found a pair + $count++; + # remove @words[$i] from the list + splice(@words, $i, 1); + # we don't need to compare any more words to $first + last; + } + } + } + return $count; +} + +sub solution(@words) { + say 'Input: @words = ("' . join('", "', @words) . '")'; + my $count = maximumPairs(@words); + say 'Output: ' . $count; +} + +say "Example 1:"; +solution("ab", "de", "ed", "bc"); + +say "\nExample 2:"; +solution("aa", "ba", "cd", "ed"); + +say "\nExample 3:"; +solution("uv", "qp", "st", "vu", "mn", "pq");
\ No newline at end of file diff --git a/challenge-256/packy-anderson/perl/ch-2.pl b/challenge-256/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..627beafa65 --- /dev/null +++ b/challenge-256/packy-anderson/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use v5.38; + +sub mergeStrings($str1, $str2) { + my @chars1 = split(//, $str1); + my @chars2 = split(//, $str2); + my $result; + while (@chars1 || @chars2) { + $result .= shift(@chars1) if @chars1; + $result .= shift(@chars2) if @chars2; + } + return $result; +} + +sub solution($str1, $str2) { + say qq{Input: \$str1 = "$str1", \$str2 = "$str2"}; + my $output = mergeStrings($str1, $str2); + say qq{Output: "$output"}; +} + +say "Example 1:"; +solution("abcd", "1234"); + +say "\nExample 2:"; +solution("abc", "12345");
\ No newline at end of file diff --git a/challenge-256/packy-anderson/python/ch-1.py b/challenge-256/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..d5aab629f4 --- /dev/null +++ b/challenge-256/packy-anderson/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +def maximumPairs(words): + count = 0 + while words: + # the the first word off the list + first = words.pop(0) + # now compare to the rest of the words in the list + for i in range(len(words)): + second = words[i] + if first == second[::-1]: + # we found a pair + count += 1 + # remove words[i] from the list + words.pop(i) + # we don't need to compare + # any more words to first + break + return count + +def comma_join(arr): + return '", "'.join(arr) + +def solution(words): + print(f'Input: @words = ({words})') + count = maximumPairs(words) + print(f'Output: {count}') + +print('Example 1:') +solution(["ab", "de", "ed", "bc"]) + +print('\nExample 2:') +solution(["aa", "ba", "cd", "ed"]) + +print('\nExample 3:') +solution(["uv", "qp", "st", "vu", "mn", "pq"]) diff --git a/challenge-256/packy-anderson/python/ch-2.py b/challenge-256/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..a178ffab9e --- /dev/null +++ b/challenge-256/packy-anderson/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +def mergeStrings(str1, str2): + chars1 = list(str1) + chars2 = list(str2) + result = '' + while chars1 or chars2: + if chars1: + result += chars1.pop(0) + if chars2: + result += chars2.pop(0) + return result + +def solution(str1, str2): + print(f'Input: $str1 = "{str1}", $str2 = "{str2}"') + output = mergeStrings(str1, str2) + print(f'Output: {output}') + +print('Example 1:') +solution("abcd", "1234") + +print('\nExample 2:') +solution("abc", "12345")
\ No newline at end of file diff --git a/challenge-256/packy-anderson/raku/ch-1.raku b/challenge-256/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..97d7625ce4 --- /dev/null +++ b/challenge-256/packy-anderson/raku/ch-1.raku @@ -0,0 +1,38 @@ +#!/usr/bin/env raku +use v6; + +sub maximumPairs(@words) { + my $count = 0; + while (@words) { + # the the first word off the list + my $first = @words.shift; + # now compare to the rest of the words in the list + for 0 .. @words.end -> $i { + my $second = @words[$i]; + if ($first eq $second.flip) { + # we found a pair + $count++; + # remove @words[$i] from the list + @words.splice($i, 1); + # we don't need to compare any more words to $first + last; + } + } + } + return $count; +} + +sub solution(@words) { + say 'Input: @words = ("' ~ @words.join('", "') ~ '")'; + my $count = maximumPairs(@words); + say 'Output: ' ~ $count; +} + +say "Example 1:"; +solution(["ab", "de", "ed", "bc"]); + +say "\nExample 2:"; +solution(["aa", "ba", "cd", "ed"]); + +say "\nExample 3:"; +solution(["uv", "qp", "st", "vu", "mn", "pq"]);
\ No newline at end of file diff --git a/challenge-256/packy-anderson/raku/ch-2.raku b/challenge-256/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..809a4ecbd1 --- /dev/null +++ b/challenge-256/packy-anderson/raku/ch-2.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku +use v6; + +sub mergeStrings($str1, $str2) { + my @chars1 = $str1.split('', :skip-empty); + my @chars2 = $str2.split('', :skip-empty); + my $result; + while (@chars1 || @chars2) { + $result ~= @chars1.shift if @chars1; + $result ~= @chars2.shift if @chars2; + } + return $result; +} + +sub solution($str1, $str2) { + say qq{Input: \$str1 = "$str1", \$str2 = "$str2"}; + my $output = mergeStrings($str1, $str2); + say qq{Output: "$output"}; +} + +say "Example 1:"; +solution("abcd", "1234"); + +say "\nExample 2:"; +solution("abc", "12345");
\ No newline at end of file |
