diff options
| author | Packy Anderson <packy@cpan.org> | 2024-06-11 22:09:02 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-06-11 22:09:02 -0400 |
| commit | f14af13216d224f815798ceaa5563b8068199306 (patch) | |
| tree | b095e1f8cb532e2e7d216563d66e3b07621419dd | |
| parent | ce654cb8a5a89ad19b858b600e63e606aa8db195 (diff) | |
| download | perlweeklychallenge-club-f14af13216d224f815798ceaa5563b8068199306.tar.gz perlweeklychallenge-club-f14af13216d224f815798ceaa5563b8068199306.tar.bz2 perlweeklychallenge-club-f14af13216d224f815798ceaa5563b8068199306.zip | |
Challenge 273 solutions by Packy Anderson
* Raku that maybe looks like Raku
* Perl
* Python that definitely looks like Perl
* Elixir that kinda looks like Elixir
1 Blog post
| -rw-r--r-- | challenge-273/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-273/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-273/packy-anderson/elixir/ch-1.exs | 35 | ||||
| -rwxr-xr-x | challenge-273/packy-anderson/elixir/ch-2.exs | 34 | ||||
| -rwxr-xr-x | challenge-273/packy-anderson/perl/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-273/packy-anderson/perl/ch-2.pl | 36 | ||||
| -rwxr-xr-x | challenge-273/packy-anderson/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-273/packy-anderson/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-273/packy-anderson/raku/ch-1.raku | 29 | ||||
| -rwxr-xr-x | challenge-273/packy-anderson/raku/ch-2.raku | 34 |
10 files changed, 256 insertions, 1 deletions
diff --git a/challenge-273/packy-anderson/README.md b/challenge-273/packy-anderson/README.md index 917fbbcdcf..5daf97b693 100644 --- a/challenge-273/packy-anderson/README.md +++ b/challenge-273/packy-anderson/README.md @@ -22,4 +22,4 @@ ## Blog Post -[Defanged Addresses & String Scores](https://packy.dardan.com/b/MS) +[Time to count B chars](https://packy.dardan.com/b/Md) diff --git a/challenge-273/packy-anderson/blog.txt b/challenge-273/packy-anderson/blog.txt new file mode 100644 index 0000000000..cbc787f4c9 --- /dev/null +++ b/challenge-273/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Md
\ No newline at end of file diff --git a/challenge-273/packy-anderson/elixir/ch-1.exs b/challenge-273/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..6f4cbbe17a --- /dev/null +++ b/challenge-273/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,35 @@ +#!/usr/bin/env elixir + +defmodule PWC do + + def charPercent(strVal, charVal) do + char_cnt = strVal + |> String.graphemes + |> Enum.filter(fn c -> c == charVal end) + |> length + trunc(round( ( char_cnt / String.length(strVal) ) * 100 )) + end + + def solution(strVal, charVal) do + IO.puts("Input: $str = \"#{strVal}\", $char = \"#{charVal}\"") + IO.puts("Output: " <> to_string(charPercent(strVal, charVal) )) + end +end + +IO.puts("Example 1:") +PWC.solution("perl", "e") + +IO.puts("\nExample 2:") +PWC.solution("java", "a") + +IO.puts("\nExample 3:") +PWC.solution("python", "m") + +IO.puts("\nExample 4:") +PWC.solution("ada", "a") + +IO.puts("\nExample 5:") +PWC.solution("ballerina", "l") + +IO.puts("\nExample 6:") +PWC.solution("analitik", "k") diff --git a/challenge-273/packy-anderson/elixir/ch-2.exs b/challenge-273/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..edc99a6ccf --- /dev/null +++ b/challenge-273/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,34 @@ +#!/usr/bin/env elixir + +defmodule PWC do + + def bAfterA([], seen_b), do: seen_b + + def bAfterA([c | rest], seen_b) do + cond do + seen_b && c == "a" -> false + true -> bAfterA(rest, seen_b || c == "b") + end + end + + def bAfterA(strVal) when is_binary(strVal) do + bAfterA(String.codepoints(strVal), false) + end + + def solution(strVal) do + IO.puts("Input: $str = \"#{strVal}\"") + IO.puts("Output: " <> to_string(bAfterA(strVal)) ) + end +end + +IO.puts("Example 1:") +PWC.solution("aabb") + +IO.puts("\nExample 2:") +PWC.solution("abab") + +IO.puts("\nExample 3:") +PWC.solution("aaa") + +IO.puts("\nExample 4:") +PWC.solution("bbb") diff --git a/challenge-273/packy-anderson/perl/ch-1.pl b/challenge-273/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..ab1c78c7a5 --- /dev/null +++ b/challenge-273/packy-anderson/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use v5.40; + +use POSIX qw(round); + +sub charPercent($str, $char) { + my $char_cnt = scalar( grep { $_ eq $char } split //, $str ); + return round( ($char_cnt / length($str)) * 100 ); +} + +sub solution($str, $char) { + say qq{Input: \$str = "$str", \$char = "$char"}; + say 'Output: ' . charPercent($str, $char); +} + +say "Example 1:"; +solution("perl", "e"); + +say "\nExample 2:"; +solution("java", "a"); + +say "\nExample 3:"; +solution("python", "m"); + +say "\nExample 4:"; +solution("ada", "a"); + +say "\nExample 5:"; +solution("ballerina", "l"); + +say "\nExample 6:"; +solution("analitik", "k"); diff --git a/challenge-273/packy-anderson/perl/ch-2.pl b/challenge-273/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..2e786dd340 --- /dev/null +++ b/challenge-273/packy-anderson/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl +use v5.40; + +use builtin ':5.40'; + +sub bAfterA($str) { + my $seen_b = false; + foreach my $c (split //, $str) { + if ($seen_b) { + if ($c eq 'a') { + return false; + } + } + elsif ($c eq 'b') { + $seen_b = true; + } + } + return $seen_b; +} + +sub solution($str) { + say qq{Input: \$str = "$str"}; + say 'Output: ' . (bAfterA($str) ? 'True' : 'False'); +} + +say "Example 1:"; +solution("aabb"); + +say "\nExample 2:"; +solution("abab"); + +say "\nExample 3:"; +solution("aaa"); + +say "\nExample 4:"; +solution("bbb"); diff --git a/challenge-273/packy-anderson/python/ch-1.py b/challenge-273/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..b42a644162 --- /dev/null +++ b/challenge-273/packy-anderson/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +def charPercent(strVal, charVal): + char_cnt = len([ c for c in strVal if c == charVal ]) + return int( ( ( char_cnt / len(strVal) ) * 100 ) + 0.5 ) + +def solution(strVal, charVal): + print(f'Input: $str = "{strVal}", $char = "{charVal}"') + print(f'Output: { charPercent(strVal, charVal) }') + +print('Example 1:') +solution("perl", "e") + +print('\nExample 2:') +solution("java", "a") + +print('\nExample 3:') +solution("python", "m") + +print('\nExample 4:') +solution("ada", "a") + +print('\nExample 5:') +solution("ballerina", "l") + +print('\nExample 6:') +solution("analitik", "k") diff --git a/challenge-273/packy-anderson/python/ch-2.py b/challenge-273/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..1cc1847bbf --- /dev/null +++ b/challenge-273/packy-anderson/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +def bAfterA(strVal): + seen_b = False + for c in strVal: + if seen_b: + if c == 'a': + return False + elif c == 'b': + seen_b = True + return seen_b + +def solution(strVal): + print(f'Input: $str = "{strVal}"') + print(f'Output: {bAfterA(strVal)}') + +print('Example 1:') +solution("aabb") + +print('\nExample 2:') +solution("abab") + +print('\nExample 3:') +solution("aaa") + +print('\nExample 4:') +solution("bbb") diff --git a/challenge-273/packy-anderson/raku/ch-1.raku b/challenge-273/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..dbdd57995e --- /dev/null +++ b/challenge-273/packy-anderson/raku/ch-1.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku +use v6; + +sub charPercent($str, $char) { + return round(( $str.comb($char).elems / $str.chars ) * 100); +} + +sub solution($str, $char) { + say qq{Input: \$str = "$str", \$char = "$char"}; + say 'Output: ' ~ charPercent($str, $char); +} + +say "Example 1:"; +solution("perl", "e"); + +say "\nExample 2:"; +solution("java", "a"); + +say "\nExample 3:"; +solution("python", "m"); + +say "\nExample 4:"; +solution("ada", "a"); + +say "\nExample 5:"; +solution("ballerina", "l"); + +say "\nExample 6:"; +solution("analitik", "k"); diff --git a/challenge-273/packy-anderson/raku/ch-2.raku b/challenge-273/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..4ac853ca76 --- /dev/null +++ b/challenge-273/packy-anderson/raku/ch-2.raku @@ -0,0 +1,34 @@ +#!/usr/bin/env raku +use v6; + +sub bAfterA($str) { + my $seen_b = False; + for $str.comb -> $c { + if ($seen_b) { + if ($c eq 'a') { + return False; + } + } + elsif ($c eq 'b') { + $seen_b = True; + } + } + return $seen_b; +} + +sub solution($str) { + say qq{Input: \$str = "$str"}; + say 'Output: ' ~ bAfterA($str); +} + +say "Example 1:"; +solution("aabb"); + +say "\nExample 2:"; +solution("abab"); + +say "\nExample 3:"; +solution("aaa"); + +say "\nExample 4:"; +solution("bbb"); |
