diff options
| author | Packy Anderson <packy@cpan.org> | 2024-06-05 01:04:31 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-06-05 01:04:31 -0400 |
| commit | 469d474b58f0ee3956e31d7dd2e73e5210d3de2e (patch) | |
| tree | adf57ac612a9358ce83ac78c8393623967267689 | |
| parent | 7aeb2014a04d815bc006a3c337b73426a10ea591 (diff) | |
| download | perlweeklychallenge-club-469d474b58f0ee3956e31d7dd2e73e5210d3de2e.tar.gz perlweeklychallenge-club-469d474b58f0ee3956e31d7dd2e73e5210d3de2e.tar.bz2 perlweeklychallenge-club-469d474b58f0ee3956e31d7dd2e73e5210d3de2e.zip | |
Challenge 272 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-272/packy-anderson/README.md | 4 | ||||
| -rw-r--r-- | challenge-272/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-272/packy-anderson/elixir/ch-1.exs | 25 | ||||
| -rwxr-xr-x | challenge-272/packy-anderson/elixir/ch-2.exs | 57 | ||||
| -rwxr-xr-x | challenge-272/packy-anderson/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-272/packy-anderson/perl/ch-2.pl | 44 | ||||
| -rwxr-xr-x | challenge-272/packy-anderson/python/ch-1.py | 21 | ||||
| -rwxr-xr-x | challenge-272/packy-anderson/python/ch-2.py | 39 | ||||
| -rwxr-xr-x | challenge-272/packy-anderson/raku/ch-1.raku | 18 | ||||
| -rwxr-xr-x | challenge-272/packy-anderson/raku/ch-2.raku | 41 |
10 files changed, 267 insertions, 1 deletions
diff --git a/challenge-272/packy-anderson/README.md b/challenge-272/packy-anderson/README.md index bfedb42a8d..917fbbcdcf 100644 --- a/challenge-272/packy-anderson/README.md +++ b/challenge-272/packy-anderson/README.md @@ -11,13 +11,15 @@ * [Task 2](perl/ch-2.pl) ## Guest Language: Python + * [Task 1](python/ch-1.py) * [Task 2](python/ch-2.py) ## Guest Language: Elixir + * [Task 1](elixir/ch-1.exs) * [Task 2](elixir/ch-2.exs) ## Blog Post -[Only Ones](https://packy.dardan.com/b/MC) +[Defanged Addresses & String Scores](https://packy.dardan.com/b/MS) diff --git a/challenge-272/packy-anderson/blog.txt b/challenge-272/packy-anderson/blog.txt new file mode 100644 index 0000000000..2281c48db2 --- /dev/null +++ b/challenge-272/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/MS
\ No newline at end of file diff --git a/challenge-272/packy-anderson/elixir/ch-1.exs b/challenge-272/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..eb9fc285bf --- /dev/null +++ b/challenge-272/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,25 @@ +#!/usr/bin/env elixir + +defmodule PWC do + + def defang(ip) do + Regex.run( + ~r/(\d+)[.](\d+)[.](\d+)[.](\d+)/, + ip, + capture: :all_but_first + ) + |> Enum.join("[.]") + end + + def solution(ip) do + IO.puts("Input: $ip = \"#{ip}\"") + defanged = defang(ip) + IO.puts("Output: #{defanged}\"") + end +end + +IO.puts("Example 1:") +PWC.solution("1.1.1.1") + +IO.puts("\nExample 2:") +PWC.solution("255.101.1.0") diff --git a/challenge-272/packy-anderson/elixir/ch-2.exs b/challenge-272/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..92a210e2a7 --- /dev/null +++ b/challenge-272/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,57 @@ +#!/usr/bin/env elixir + +defmodule PWC do + # build up the lines like "h = 104" + def explain_mapping([], [], explain), do: explain + def explain_mapping([c | chars], [v | vals], explain) do + explain_mapping(chars, vals, explain ++ ["#{c} = #{v}"]) + end + + # process the list of codepoints and explain our calculations + def process_list([], _, line1, line2), do: {line1, line2} + def process_list([next | vals], last, line1, line2) do + line1 = line1 ++ ["| #{last} - #{next} |"] + line2 = line2 ++ [ abs(last - next) ] + process_list(vals, next, line1, line2) + end + + def score(str) do + chars = String.graphemes(str) + vals = String.to_charlist(str) + + # generate the first part of the explanation + explain = explain_mapping( + chars, vals, ["ASCII values of characters:"] + ) + + # get the first codepoint off the list + {last, vals} = List.pop_at(vals, 0) + # process the rest of the codepoints + {line1, line2} = process_list(vals, last, [], []) + + # now format the last part of the explanation + line1str = Enum.join(line1, " + ") + explain = explain ++ ["Score => #{line1str}"] + line2str = Enum.join(line2, " + ") + explain = explain ++ [" => #{line2str}"] + scoreVal = Enum.sum(line2) + explain = explain ++ [" => #{scoreVal}"] + + { scoreVal, Enum.join(explain, "\n") } + end + + def solution(str) do + IO.puts("Input: $str = \"#{str}\"") + {scoreVal, explain} = score(str) + IO.puts("Output: #{scoreVal}\n\n#{explain}") + end +end + +IO.puts("Example 1:") +PWC.solution("hello") + +IO.puts("\nExample 2:") +PWC.solution("perl") + +IO.puts("\nExample 3:") +PWC.solution("raku") diff --git a/challenge-272/packy-anderson/perl/ch-1.pl b/challenge-272/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..39a85ff898 --- /dev/null +++ b/challenge-272/packy-anderson/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +use v5.38; + +sub defang($ip) { + $ip =~ / (\d+) [.] (\d+) [.] (\d+) [.] (\d+) /x; + return join('[.]', @{^CAPTURE}); +} + +sub solution($ip) { + say 'Input: $ip = "' . $ip . '"'; + say 'Output: "' . defang($ip) . '"'; +} + +say "Example 1:"; +solution("1.1.1.1"); + +say "\nExample 2:"; +solution("255.101.1.0"); diff --git a/challenge-272/packy-anderson/perl/ch-2.pl b/challenge-272/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..42eff658f0 --- /dev/null +++ b/challenge-272/packy-anderson/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( sum zip ); + +sub score($str) { + my @chars = split //, $str; + my @vals = map { ord($_) } @chars; + my @explain = ("ASCII values of characters:"); + foreach my $z ( zip \@chars, \@vals ) { + my ($c, $v) = @$z; + push @explain, "$c = $v"; + } + my @line1; + my @line2; + + my $last = shift @vals; + while (my $next = shift @vals) { + push @line1, "| $last - $next |"; + push @line2, abs($last - $next); + $last = $next; + } + push @explain, "Score => " . join(" + ", @line1); + push @explain, " => " . join(" + ", @line2); + my $score = sum @line2; + push @explain, " => " . $score; + + return $score, join("\n", @explain); +} + +sub solution($str) { + say 'Input: $str = "' . $str . '"'; + my ($score, $explain) = score($str); + say "Output: $score\n\n$explain"; +} + +say "Example 1:"; +solution("hello"); + +say "\nExample 2:"; +solution("perl"); + +say "\nExample 3:"; +solution("raku"); diff --git a/challenge-272/packy-anderson/python/ch-1.py b/challenge-272/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..e9f9a9509f --- /dev/null +++ b/challenge-272/packy-anderson/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +import re + +def defang(ip): + match = re.search( + r'(\d+) [.] (\d+) [.] (\d+) [.] (\d+)', + ip, + re.X + ) + return '[.]'.join(match.group(1,2,3,4)) + +def solution(ip): + print(f'Input: $ip = "{ip}"') + print(f'Output: "{defang(ip)}"') + +print('Example 1:') +solution("1.1.1.1") + +print('\nExample 2:') +solution("255.101.1.0") diff --git a/challenge-272/packy-anderson/python/ch-2.py b/challenge-272/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..5d07ab7c9f --- /dev/null +++ b/challenge-272/packy-anderson/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +def score(strVal): + chars = [ c for c in strVal ] + vals = [ ord(c) for c in chars ] + explain = [ "ASCII values of characters:" ] + for c, v in zip(chars, vals): + explain.append(f"{c} = {v}") + + line1 = [] + line2 = [] + + last = vals.pop(0) + while vals: + next = vals.pop(0) + line1.append(f"| {last} - {next} |") + line2.append(abs(last - next)) + last = next + + explain.append("Score => " + " + ".join(line1)) + explain.append(" => " + " + ".join(map(lambda i: str(i), line2))) + score = sum(line2) + explain.append(f" => {score}") + + return score, "\n".join(explain) + +def solution(strVal): + print(f'Input: $str = "{strVal}"') + scoreVal, explain = score(strVal) + print(f'Output: {scoreVal}\n\n{explain}') + +print('Example 1:') +solution("hello") + +print('\nExample 2:') +solution("perl") + +print('\nExample 3:') +solution("raku") diff --git a/challenge-272/packy-anderson/raku/ch-1.raku b/challenge-272/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..16293b5734 --- /dev/null +++ b/challenge-272/packy-anderson/raku/ch-1.raku @@ -0,0 +1,18 @@ +#!/usr/bin/env raku +use v6; + +sub defang($ip) { + $ip ~~ / (\d+) '.' (\d+) '.' (\d+) '.' (\d+) /; + return @$/.join('[.]'); +} + +sub solution($ip) { + say 'Input: $ip = "' ~ $ip ~ '"'; + say 'Output: "' ~ defang($ip) ~ '"'; +} + +say "Example 1:"; +solution("1.1.1.1"); + +say "\nExample 2:"; +solution("255.101.1.0"); diff --git a/challenge-272/packy-anderson/raku/ch-2.raku b/challenge-272/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..2d6b01c91c --- /dev/null +++ b/challenge-272/packy-anderson/raku/ch-2.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku +use v6; + +sub score($str) { + my @chars = $str.comb; + my @vals = @chars.map: { .ord }; + my @explain = ("ASCII values of characters:"); + for @chars Z @vals -> ($c, $v) { + @explain.push: "$c = $v"; + } + my @line1; + my @line2; + + my $last = @vals.shift; + while (my $next = @vals.shift) { + @line1.push: "| $last - $next |"; + @line2.push: abs($last - $next); + $last = $next; + } + @explain.push: "Score => " ~ @line1.join(" + "); + @explain.push: " => " ~ @line2.join(" + "); + my $score = [+] @line2; + @explain.push: " => " ~ $score; + + return $score, @explain.join("\n"); +} + +sub solution($str) { + say 'Input: $str = "' ~ $str ~ '"'; + my ($score, $explain) = score($str); + say "Output: $score\n\n$explain"; +} + +say "Example 1:"; +solution("hello"); + +say "\nExample 2:"; +solution("perl"); + +say "\nExample 3:"; +solution("raku"); |
