diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-03 11:35:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-03 11:35:28 +0100 |
| commit | 5e1be3ff660e14623bcef436f8e6dcb2ac6426a2 (patch) | |
| tree | b2eabeea018155d1f21009cca076350b1deacaf0 /challenge-319 | |
| parent | d120136fd53e8e39879afde192e7780c9b226d90 (diff) | |
| parent | b8b391a9d93731394edb2a2c38a437e5352ba079 (diff) | |
| download | perlweeklychallenge-club-5e1be3ff660e14623bcef436f8e6dcb2ac6426a2.tar.gz perlweeklychallenge-club-5e1be3ff660e14623bcef436f8e6dcb2ac6426a2.tar.bz2 perlweeklychallenge-club-5e1be3ff660e14623bcef436f8e6dcb2ac6426a2.zip | |
Merge pull request #11961 from packy/master
Challenge 319 solutions by Packy Anderson
Diffstat (limited to 'challenge-319')
| -rw-r--r-- | challenge-319/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-319/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-319/packy-anderson/elixir/ch-1.exs | 37 | ||||
| -rwxr-xr-x | challenge-319/packy-anderson/elixir/ch-2.exs | 28 | ||||
| -rwxr-xr-x | challenge-319/packy-anderson/perl/ch-1.pl | 33 | ||||
| -rwxr-xr-x | challenge-319/packy-anderson/perl/ch-2.pl | 30 | ||||
| -rwxr-xr-x | challenge-319/packy-anderson/python/ch-1.py | 28 | ||||
| -rwxr-xr-x | challenge-319/packy-anderson/python/ch-2.py | 26 | ||||
| -rwxr-xr-x | challenge-319/packy-anderson/raku/ch-1.raku | 32 | ||||
| -rwxr-xr-x | challenge-319/packy-anderson/raku/ch-2.raku | 27 |
10 files changed, 243 insertions, 1 deletions
diff --git a/challenge-319/packy-anderson/README.md b/challenge-319/packy-anderson/README.md index c2f55fde69..ca6c64697e 100644 --- a/challenge-319/packy-anderson/README.md +++ b/challenge-319/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Group the Reverse Position](https://packy.dardan.com/b/Tp) +[Perl Weekly Challenge: Count the Minimum Common Word](https://packy.dardan.com/b/UD) diff --git a/challenge-319/packy-anderson/blog.txt b/challenge-319/packy-anderson/blog.txt new file mode 100644 index 0000000000..bb4a8edb9e --- /dev/null +++ b/challenge-319/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/UD
\ No newline at end of file diff --git a/challenge-319/packy-anderson/elixir/ch-1.exs b/challenge-319/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..bfc85c404a --- /dev/null +++ b/challenge-319/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,37 @@ +#!/usr/bin/env elixir + +defmodule PWC do + @vowels "[aeiou]" + + @starts_or_ends_with_vowel Regex.compile!( + "\\b#{@vowels}|#{@vowels}\\b" + ) + + def wordCount(words) do + {_, matched} = Enum.map_reduce(words, [], fn word, list -> + list = if Regex.match?(@starts_or_ends_with_vowel, word) do + [ word | list ] + else + list + end + # always have to return the both + # the value and the accumulator + {word, list} + end) + length(matched) + end + + def solution(words) do + IO.puts("Input: @ints = (" <> Enum.join(words, ", ") <> ")") + IO.puts("Output: " <> to_string(wordCount(words)) ) + end +end + +IO.puts("Example 1:") +PWC.solution(["unicode", "xml", "raku", "perl"]) + +IO.puts("\nExample 2:") +PWC.solution(["the", "weekly", "challenge"]) + +IO.puts("\nExample 3:") +PWC.solution(["perl", "python", "postgres"]) diff --git a/challenge-319/packy-anderson/elixir/ch-2.exs b/challenge-319/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..cdd6dda68f --- /dev/null +++ b/challenge-319/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,28 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def minCommon(arr1, arr2) do + set1 = MapSet.new(arr1) + set2 = MapSet.new(arr2) + common = MapSet.intersection(set1, set2) + cond do + MapSet.size(common) == 0 -> -1 + true -> common |> MapSet.to_list |> Enum.min + end + end + + def solution(arr1, arr2) do + IO.puts("Input: @array_1 = (" <> Enum.join(arr1, ", ") <> ")") + IO.puts(" @array_2 = (" <> Enum.join(arr2, ", ") <> ")") + IO.puts("Output: " <> to_string( minCommon(arr1, arr2) ) ) + end +end + +IO.puts("Example 1:") +PWC.solution([1, 2, 3, 4], [3, 4, 5, 6]) + +IO.puts("\nExample 2:") +PWC.solution([1, 2, 3], [2, 4]) + +IO.puts("\nExample 3:") +PWC.solution([1, 2, 3, 4], [5, 6, 7, 8]) diff --git a/challenge-319/packy-anderson/perl/ch-1.pl b/challenge-319/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..c3d651ebc3 --- /dev/null +++ b/challenge-319/packy-anderson/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.40; + +my $vowels = qr{[aeiou]}; + +my $starts_or_ends_with_vowel = qr{ + \b $vowels # word boundry followed by a vowel + | # or + $vowels \b # a vowel followed by a word boundry +}x; + +sub wordCount(@list) { + my @matched; + foreach my $word (@list) { + push @matched, $word + if $word =~ /$starts_or_ends_with_vowel/; + } + return scalar(@matched); +} + +sub solution($list) { + say 'Input: @list = ("' . join('", "', @$list) . '")'; + say 'Output: ' . wordCount(@$list); +} + +say "Example 1:"; +solution(["unicode", "xml", "raku", "perl"]); + +say "\nExample 2:"; +solution(["the", "weekly", "challenge"]); + +say "\nExample 3:"; +solution(["perl", "python", "postgres"]); diff --git a/challenge-319/packy-anderson/perl/ch-2.pl b/challenge-319/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..6cb108b389 --- /dev/null +++ b/challenge-319/packy-anderson/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +use v5.40; + +use Set::Scalar; +use List::AllUtils qw( min ); + +sub minCommon($arr1, $arr2) { + my $set1 = Set::Scalar->new(@$arr1); + my $set2 = Set::Scalar->new(@$arr2); + my $common = $set1 * $set2; + if ($common) { + return min($common->elements); + } + return -1; +} + +sub solution($arr1, $arr2) { + say 'Input: @array_1 = (' . join(', ', @$arr1) . ')'; + say ' @array_2 = (' . join(', ', @$arr2) . ')'; + say 'Output: ' . minCommon($arr1, $arr2); +} + +say "Example 1:"; +solution([1, 2, 3, 4], [3, 4, 5, 6]); + +say "\nExample 2:"; +solution([1, 2, 3], [2, 4]); + +say "\nExample 3:"; +solution([1, 2, 3, 4], [5, 6, 7, 8]);
\ No newline at end of file diff --git a/challenge-319/packy-anderson/python/ch-1.py b/challenge-319/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..9354c8674c --- /dev/null +++ b/challenge-319/packy-anderson/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +import re + +vowels = '[aeiou]' + +starts_or_ends_with_vowel = rf'\b{vowels}|{vowels}\b' + +def wordCount(word_list): + matched = [] + for word in word_list: + if re.search(starts_or_ends_with_vowel, word): + matched.append(word) + return len(matched) + +def solution(word_list): + joined = '"' + '", "'.join(word_list) + '"' + print(f'Input: @list = ({joined})') + print(f'Output: {wordCount(word_list)}') + +print('Example 1:') +solution(["unicode", "xml", "raku", "perl"]) + +print('\nExample 2:') +solution(["the", "weekly", "challenge"]) + +print('\nExample 3:') +solution(["perl", "python", "postgres"]) diff --git a/challenge-319/packy-anderson/python/ch-2.py b/challenge-319/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..b739236ec7 --- /dev/null +++ b/challenge-319/packy-anderson/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +def minCommon(arr1, arr2): + set1 = set(arr1) + set2 = set(arr2) + common = set1.intersection(set2) + if common: + return min(list(common)) + return -1 + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(arr1, arr2): + print(f'Input: @array_1 = ({comma_join(arr1)})') + print(f' @array_2 = ({comma_join(arr2)})') + print(f'Output: { minCommon(arr1, arr2 )}') + +print('Example 1:') +solution([1, 2, 3, 4], [3, 4, 5, 6]) + +print('\nExample 2:') +solution([1, 2, 3], [2, 4]) + +print('\nExample 3:') +solution([1, 2, 3, 4], [5, 6, 7, 8]) diff --git a/challenge-319/packy-anderson/raku/ch-1.raku b/challenge-319/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..9538aa1307 --- /dev/null +++ b/challenge-319/packy-anderson/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku +use v6; + +my regex vowels { <[aeiou]> }; + +my regex starts_or_ends_with_vowel { + « <{&vowels}> # left word boundry followed by a vowel + || # or + <{&vowels}> » # a vowel followed by the right word boundry +}; + +sub wordCount(@list) { + my @matched; + for @list -> $word { + @matched.push($word) if $word ~~ &starts_or_ends_with_vowel; + } + return @matched.elems; +} + +sub solution(@list) { + say 'Input: @list = ("' ~ @list.join('", "') ~ '")'; + say 'Output: ' ~ wordCount(@list); +} + +say "Example 1:"; +solution(["unicode", "xml", "raku", "perl"]); + +say "\nExample 2:"; +solution(["the", "weekly", "challenge"]); + +say "\nExample 3:"; +solution(["perl", "python", "postgres"]); diff --git a/challenge-319/packy-anderson/raku/ch-2.raku b/challenge-319/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..a65245a1d6 --- /dev/null +++ b/challenge-319/packy-anderson/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/env raku +use v6; + +sub minCommon(@arr1, @arr2) { + my $set1 = Set.new(@arr1); + my $set2 = Set.new(@arr2); + my $common = $set1 ∩ $set2; + if ($common) { + return min($common.keys); + } + return -1; +} + +sub solution(@arr1, @arr2) { + say 'Input: @array_1 = (' ~ @arr1.join(', ') ~ ')'; + say ' @array_2 = (' ~ @arr2.join(', ') ~ ')'; + say 'Output: ' ~ minCommon(@arr1, @arr2); +} + +say "Example 1:"; +solution([1, 2, 3, 4], [3, 4, 5, 6]); + +say "\nExample 2:"; +solution([1, 2, 3], [2, 4]); + +say "\nExample 3:"; +solution([1, 2, 3, 4], [5, 6, 7, 8]); |
