diff options
| author | Packy Anderson <packy@cpan.org> | 2025-09-22 23:59:20 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2025-09-22 23:59:20 -0400 |
| commit | 15e8ffd9daeb40667b1fb2b76dece74e471450ea (patch) | |
| tree | 85136a8a1e675edb70fefc47e307776bd632fee1 | |
| parent | c410a0181e730b574b7c8f80700d62d3cfb6fdc8 (diff) | |
| download | perlweeklychallenge-club-15e8ffd9daeb40667b1fb2b76dece74e471450ea.tar.gz perlweeklychallenge-club-15e8ffd9daeb40667b1fb2b76dece74e471450ea.tar.bz2 perlweeklychallenge-club-15e8ffd9daeb40667b1fb2b76dece74e471450ea.zip | |
Challenge 340 solutions by Packy Anderson
* Raku that is finally Raku-ish
* Perl
* Python that kinda looks like Raku
* Elixir that is starting to look like Elixir
1 blog post
| -rw-r--r-- | challenge-340/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-340/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-340/packy-anderson/elixir/ch-1.exs | 33 | ||||
| -rwxr-xr-x | challenge-340/packy-anderson/elixir/ch-2.exs | 41 | ||||
| -rwxr-xr-x | challenge-340/packy-anderson/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-340/packy-anderson/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-340/packy-anderson/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-340/packy-anderson/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-340/packy-anderson/raku/ch-1.raku | 28 | ||||
| -rwxr-xr-x | challenge-340/packy-anderson/raku/ch-2.raku | 31 |
10 files changed, 250 insertions, 1 deletions
diff --git a/challenge-340/packy-anderson/README.md b/challenge-340/packy-anderson/README.md index 1013ff24f7..7af85bca11 100644 --- a/challenge-340/packy-anderson/README.md +++ b/challenge-340/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Points? What's the diff?](https://packy.dardan.com/b/bN) +[Perl Weekly Challenge: Rising in Num](https://packy.dardan.com/b/bj) diff --git a/challenge-340/packy-anderson/blog.txt b/challenge-340/packy-anderson/blog.txt new file mode 100644 index 0000000000..f0268a9a3f --- /dev/null +++ b/challenge-340/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/bj
\ No newline at end of file diff --git a/challenge-340/packy-anderson/elixir/ch-1.exs b/challenge-340/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..65013250fc --- /dev/null +++ b/challenge-340/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,33 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def duplicate_removal(str) do + match = Regex.run(~r/(\p{L})\1/, str, capture: :first) + if match do + String.replace(str, match, "", global: false) + |> duplicate_removal + else + str + end + end + + def solution(str) do + IO.puts("Input: $str = '#{str}'") + IO.puts("Output: '#{duplicate_removal(str)}'") + end +end + +IO.puts("Example 1:") +PWC.solution("abbaca") + +IO.puts("\nExample 2:") +PWC.solution("azxxzy") + +IO.puts("\nExample 3:") +PWC.solution("aaaaaaaa") + +IO.puts("\nExample 4:") +PWC.solution("aabccba") + +IO.puts("\nExample 5:") +PWC.solution("abcddcba") diff --git a/challenge-340/packy-anderson/elixir/ch-2.exs b/challenge-340/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..73a47e15f8 --- /dev/null +++ b/challenge-340/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,41 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def ascending(list) when is_list(list) and length(list) == 1, + do: "true" + + def ascending([head | list]) do + if head < List.first(list) do + ascending(list) + else + "false" + end + end + + def ascending_num(str) do + Regex.scan(~r/\d+/, str, capture: :first) + |> List.flatten + |> Enum.map(fn n -> String.to_integer(n) end) + |> ascending + end + + def solution(str) do + IO.puts("Input: $str = '#{str}'") + IO.puts("Output: #{ascending_num(str)}") + end +end + +IO.puts("Example 1:") +PWC.solution("The cat has 3 kittens 7 toys 10 beds") + +IO.puts("\nExample 2:") +PWC.solution("Alice bought 5 apples 2 oranges 9 bananas") + +IO.puts("\nExample 3:") +PWC.solution("I ran 1 mile 2 days 3 weeks 4 months") + +IO.puts("\nExample 4:") +PWC.solution("Bob has 10 cars 10 bikes") + +IO.puts("\nExample 5:") +PWC.solution("Zero is 0 one is 1 two is 2") diff --git a/challenge-340/packy-anderson/perl/ch-1.pl b/challenge-340/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..80cb0b4f84 --- /dev/null +++ b/challenge-340/packy-anderson/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.40; + +sub duplicateRemoval($str) { + return $str + unless $str =~ s/([a-z])\g1//; + duplicateRemoval($str); +} + +sub solution($str) { + say "Input: \$str = '$str'"; + say "Output: '@{[duplicateRemoval($str)]}'"; +} + +say "Example 1:"; +solution("abbaca"); + +say "\nExample 2:"; +solution("azxxzy"); + +say "\nExample 3:"; +solution("aaaaaaaa"); + +say "\nExample 4:"; +solution("aabccba"); + +say "\nExample 5:"; +solution("abcddcba"); diff --git a/challenge-340/packy-anderson/perl/ch-2.pl b/challenge-340/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..0dc1b498f1 --- /dev/null +++ b/challenge-340/packy-anderson/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +use v5.40; + +sub ascendingNum($str) { + my @list = $str =~ /(\d+)/g; + while (@list > 1) { + return 'false' if $list[0] >= $list[1]; + shift @list; + } + return 'true'; +} + +sub solution($str) { + say "Input: \$str = '$str'"; + say "Output: @{[ascendingNum($str)]}"; +} + +say "Example 1:"; +solution("The cat has 3 kittens 7 toys 10 beds"); + +say "\nExample 2:"; +solution("Alice bought 5 apples 2 oranges 9 bananas"); + +say "\nExample 3:"; +solution("I ran 1 mile 2 days 3 weeks 4 months"); + +say "\nExample 4:"; +solution("Bob has 10 cars 10 bikes"); + +say "\nExample 5:"; +solution("Zero is 0 one is 1 two is 2"); diff --git a/challenge-340/packy-anderson/python/ch-1.py b/challenge-340/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..4b0733e391 --- /dev/null +++ b/challenge-340/packy-anderson/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +import re + +def duplicate_removal(strval): + m = re.search(r'([a-z])\1', strval) + if not m: return strval + return duplicate_removal( + strval.replace(m.group(0), '', count=1) + ) + +def solution(strval): + print(f"Input: $str = '{strval}'") + print(f"Output: '{duplicate_removal(strval)}'") + +print('Example 1:') +solution("abbaca") + +print('\nExample 2:') +solution("azxxzy") + +print('\nExample 3:') +solution("aaaaaaaa") + +print('\nExample 4:') +solution("aabccba") + +print('\nExample 5:') +solution("abcddcba") diff --git a/challenge-340/packy-anderson/python/ch-2.py b/challenge-340/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..55b4f2b288 --- /dev/null +++ b/challenge-340/packy-anderson/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +def ascending_num(strval): + list = [ int(n) for n in strval.split() if n.isnumeric() ] + while len(list) > 1: + if list[0] >= list[1]: return False + list.pop(0) + return True + +def solution(strval): + print(f"Input: $str = '{strval}'") + print(f"Output: {ascending_num(strval)}") + +print('Example 1:') +solution("The cat has 3 kittens 7 toys 10 beds") + +print('\nExample 2:') +solution("Alice bought 5 apples 2 oranges 9 bananas") + +print('\nExample 3:') +solution("I ran 1 mile 2 days 3 weeks 4 months") + +print('\nExample 4:') +solution("Bob has 10 cars 10 bikes") + +print('\nExample 5:') +solution("Zero is 0 one is 1 two is 2") diff --git a/challenge-340/packy-anderson/raku/ch-1.raku b/challenge-340/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..9bd7914c33 --- /dev/null +++ b/challenge-340/packy-anderson/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; + +sub duplicateRemoval($str is copy) { + return $str + unless $str ~~ s/(<lower>) {} $0//; + duplicateRemoval($str); +} + +sub solution($str) { + say "Input: \$str = '$str'"; + say "Output: '{duplicateRemoval($str)}'"; +} + +say "Example 1:"; +solution("abbaca"); + +say "\nExample 2:"; +solution("azxxzy"); + +say "\nExample 3:"; +solution("aaaaaaaa"); + +say "\nExample 4:"; +solution("aabccba"); + +say "\nExample 5:"; +solution("abcddcba"); diff --git a/challenge-340/packy-anderson/raku/ch-2.raku b/challenge-340/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..935d273a0b --- /dev/null +++ b/challenge-340/packy-anderson/raku/ch-2.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku +use v6; + +sub ascendingNum($str) { + my @list = $str.comb(/\d+/); + while (@list.elems > 1) { + return False if @list[0] >= @list[1]; + @list.shift; + } + return True; +} + +sub solution($str) { + say "Input: \$str = '$str'"; + say "Output: {ascendingNum($str)}"; +} + +say "Example 1:"; +solution("The cat has 3 kittens 7 toys 10 beds"); + +say "\nExample 2:"; +solution("Alice bought 5 apples 2 oranges 9 bananas"); + +say "\nExample 3:"; +solution("I ran 1 mile 2 days 3 weeks 4 months"); + +say "\nExample 4:"; +solution("Bob has 10 cars 10 bikes"); + +say "\nExample 5:"; +solution("Zero is 0 one is 1 two is 2"); |
