diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-02 09:52:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-02 09:52:11 +0100 |
| commit | 1d4f26465d71bbf664717f1dcdbc4dc03e8a92fa (patch) | |
| tree | 65d4f82a408dae2a8ec2783f2bbc9fd91e9be3c4 /challenge-280 | |
| parent | e712b5491ed008a9e318935fc5d0f11b07b16f54 (diff) | |
| parent | b863d69689ab5c27820954f0534d8b47a2c7bd86 (diff) | |
| download | perlweeklychallenge-club-1d4f26465d71bbf664717f1dcdbc4dc03e8a92fa.tar.gz perlweeklychallenge-club-1d4f26465d71bbf664717f1dcdbc4dc03e8a92fa.tar.bz2 perlweeklychallenge-club-1d4f26465d71bbf664717f1dcdbc4dc03e8a92fa.zip | |
Merge pull request #10530 from packy/master
Challenge 280 solutions by Packy Anderson
Diffstat (limited to 'challenge-280')
| -rw-r--r-- | challenge-280/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-280/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-280/packy-anderson/elixir/ch-1.exs | 36 | ||||
| -rwxr-xr-x | challenge-280/packy-anderson/elixir/ch-2.exs | 24 | ||||
| -rwxr-xr-x | challenge-280/packy-anderson/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-280/packy-anderson/perl/ch-2.pl | 25 | ||||
| -rwxr-xr-x | challenge-280/packy-anderson/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-280/packy-anderson/python/ch-2.py | 21 | ||||
| -rwxr-xr-x | challenge-280/packy-anderson/raku/ch-1.raku | 30 | ||||
| -rwxr-xr-x | challenge-280/packy-anderson/raku/ch-2.raku | 24 |
10 files changed, 219 insertions, 1 deletions
diff --git a/challenge-280/packy-anderson/README.md b/challenge-280/packy-anderson/README.md index 4143afe86f..7974906632 100644 --- a/challenge-280/packy-anderson/README.md +++ b/challenge-280/packy-anderson/README.md @@ -22,4 +22,4 @@ ## Blog Post -[Perl Weekly Challenge: Split 'em away!](https://packy.dardan.com/b/PP) +[Perl Weekly Challenge: Appear Twice, Count Once](https://packy.dardan.com/b/P_) diff --git a/challenge-280/packy-anderson/blog.txt b/challenge-280/packy-anderson/blog.txt new file mode 100644 index 0000000000..c25b82b1e2 --- /dev/null +++ b/challenge-280/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/P_
\ No newline at end of file diff --git a/challenge-280/packy-anderson/elixir/ch-1.exs b/challenge-280/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..76449b86b3 --- /dev/null +++ b/challenge-280/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,36 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def twiceAppearance([], _), do: "␀" # fallback + + def twiceAppearance([next | rest], count) do + if Map.has_key?(count, next) do + next + else + twiceAppearance(rest, Map.put(count, next, 1)) + end + end + + def twiceAppearance(str) do + # split the string into characters and + # reprocess with an empty Map + twiceAppearance(String.graphemes(str), %{}) + end + + def solution(str) do + IO.puts("Input: $str = \"#{str}\"") + IO.puts("Output: \"#{twiceAppearance(str)}\"") + end +end + +IO.puts("Example 1:") +PWC.solution("acbddbca") + +IO.puts("\nExample 2:") +PWC.solution("abccd") + +IO.puts("\nExample 3:") +PWC.solution("abcdabbb") + +IO.puts("\nExample 4:") +PWC.solution("abcdefg") diff --git a/challenge-280/packy-anderson/elixir/ch-2.exs b/challenge-280/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..0bfbcfaff3 --- /dev/null +++ b/challenge-280/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,24 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def countAsterisks(str) do + str + |> String.replace(~r/\|[^|]+\|/, "") + |> String.replace(~r/[^*]+/, "") + |> String.length + end + + def solution(str) do + IO.puts("Input: $str = \"#{str}\"") + IO.puts("Output: " <> to_string(countAsterisks(str)) ) + end +end + +IO.puts("Example 1:") +PWC.solution("p|*e*rl|w**e|*ekly|") + +IO.puts("\nExample 2:") +PWC.solution("perl") + +IO.puts("\nExample 3:") +PWC.solution("th|ewe|e**|k|l***ych|alleng|e") diff --git a/challenge-280/packy-anderson/perl/ch-1.pl b/challenge-280/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..6d2f9b04f8 --- /dev/null +++ b/challenge-280/packy-anderson/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +use v5.40; + +sub twiceAppearance($str) { + my @chars = split //, $str; + my %count; + foreach my $char ( @chars ) { + $count{$char}++; + return $char if $count{$char} > 1; + } + return "␀"; # fallback +} + +sub solution($str) { + say qq/Input: \$str = "$str"/; + my $char = twiceAppearance($str); + say qq/Output: "$char"/; +} + +say "Example 1:"; +solution("acbddbca"); + +say "\nExample 2:"; +solution("abccd"); + +say "\nExample 3:"; +solution("abcdabbb"); + +say "\nExample 4:"; +solution("abcdefg"); diff --git a/challenge-280/packy-anderson/perl/ch-2.pl b/challenge-280/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..312fe1810e --- /dev/null +++ b/challenge-280/packy-anderson/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use v5.40; + +sub countAsterisks($str) { + (my $excluded = $str) =~ s/\|[^|]*\|//g; + my %count; + map { $count{$_}++ } split //, $excluded; + return 0 unless exists $count{"*"}; + return $count{"*"}; +} + +sub solution($str) { + say qq/Input: \$str = "$str"/; + my $count = countAsterisks($str); + say qq/Output: $count/; +} + +say "Example 1:"; +solution("p|*e*rl|w**e|*ekly|"); + +say "\nExample 2:"; +solution("perl"); + +say "\nExample 3:"; +solution("th|ewe|e**|k|l***ych|alleng|e"); diff --git a/challenge-280/packy-anderson/python/ch-1.py b/challenge-280/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..def9b93832 --- /dev/null +++ b/challenge-280/packy-anderson/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +from collections import Counter + +def twiceAppearance(str): + count = Counter() + for char in str: + count[char] += 1 + if count[char] > 1: + return char + return "␀" # fallback + +def solution(str): + print(f'Input: $str = "{str}"') + print(f'Output: "{twiceAppearance(str)}"') + +print('Example 1:') +solution("acbddbca") + +print('\nExample 2:') +solution("abccd") + +print('\nExample 3:') +solution("abcdabbb") + +print('\nExample 4:') +solution("abcdefg") diff --git a/challenge-280/packy-anderson/python/ch-2.py b/challenge-280/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..bdbf0cbfc0 --- /dev/null +++ b/challenge-280/packy-anderson/python/ch-2.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +from collections import Counter +import re + +def countAsterisks(str): + count = Counter(re.sub(r'\|[^|]+\|', '', str)) + return count["*"] + +def solution(str): + print(f'Input: $str = "{str}"') + print(f'Output: {countAsterisks(str)}') + +print('Example 1:') +solution("p|*e*rl|w**e|*ekly|") + +print('\nExample 2:') +solution("perl") + +print('\nExample 3:') +solution("th|ewe|e**|k|l***ych|alleng|e") diff --git a/challenge-280/packy-anderson/raku/ch-1.raku b/challenge-280/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..1c772e66e1 --- /dev/null +++ b/challenge-280/packy-anderson/raku/ch-1.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env raku +use v6; + +sub twiceAppearance($str) { + my @chars = $str.comb; + my %count; + for @chars -> $char { + %count{$char}++; + return $char if %count{$char} > 1; + } + return "␀"; # fallback +} + +sub solution($str) { + say qq/Input: \$str = "$str"/; + my $char = twiceAppearance($str); + say qq/Output: "$char"/; +} + +say "Example 1:"; +solution("acbddbca"); + +say "\nExample 2:"; +solution("abccd"); + +say "\nExample 3:"; +solution("abcdabbb"); + +say "\nExample 4:"; +solution("abcdefg"); diff --git a/challenge-280/packy-anderson/raku/ch-2.raku b/challenge-280/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..6aafda37f8 --- /dev/null +++ b/challenge-280/packy-anderson/raku/ch-2.raku @@ -0,0 +1,24 @@ +#!/usr/bin/env raku +use v6; + +sub countAsterisks($str) { + (my $excluded = $str) ~~ s:global/\| <-[ | ]> * \|//; + my %count = $excluded.comb.Bag; + return 0 unless %count{"*"}:exists; + return %count{"*"}; +} + +sub solution($str) { + say qq/Input: \$str = "$str"/; + my $count = countAsterisks($str); + say qq/Output: $count/; +} + +say "Example 1:"; +solution("p|*e*rl|w**e|*ekly|"); + +say "\nExample 2:"; +solution("perl"); + +say "\nExample 3:"; +solution("th|ewe|e**|k|l***ych|alleng|e"); |
