diff options
| -rw-r--r-- | challenge-288/packy-anderson/README.md | 4 | ||||
| -rw-r--r-- | challenge-288/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-288/packy-anderson/elixir/ch-1.exs | 49 | ||||
| -rwxr-xr-x | challenge-288/packy-anderson/perl/ch-1.pl | 45 | ||||
| -rwxr-xr-x | challenge-288/packy-anderson/perl/ch-2.pl | 111 | ||||
| -rwxr-xr-x | challenge-288/packy-anderson/python/ch-1.py | 39 | ||||
| -rwxr-xr-x | challenge-288/packy-anderson/python/ch-2.py | 99 | ||||
| -rwxr-xr-x | challenge-288/packy-anderson/raku/ch-1.raku | 45 | ||||
| -rwxr-xr-x | challenge-288/packy-anderson/raku/ch-2.raku | 110 |
9 files changed, 501 insertions, 2 deletions
diff --git a/challenge-288/packy-anderson/README.md b/challenge-288/packy-anderson/README.md index 221e5be870..6e22767c9c 100644 --- a/challenge-288/packy-anderson/README.md +++ b/challenge-288/packy-anderson/README.md @@ -18,8 +18,8 @@ ## Guest Language: Elixir * [Task 1](elixir/ch-1.exs) -* [Task 2](elixir/ch-2.exs) +<!-- * [Task 2](elixir/ch-2.exs) --> ## Blog Post -[Perl Weekly Challenge: Strong but Valid](https://packy.dardan.com/b/RU) +[Perl Weekly Challenge: A Man, A Plan, A Canal... PANAMA!](https://packy.dardan.com/b/Rm) diff --git a/challenge-288/packy-anderson/blog.txt b/challenge-288/packy-anderson/blog.txt new file mode 100644 index 0000000000..3f0f7eca9e --- /dev/null +++ b/challenge-288/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Rm
\ No newline at end of file diff --git a/challenge-288/packy-anderson/elixir/ch-1.exs b/challenge-288/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..a0d94b77b3 --- /dev/null +++ b/challenge-288/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,49 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def isPalindrome(num) do + # convert numerics to Strings, then reverse one of them + Integer.to_string(num) == + Integer.to_string(num) |> String.reverse + end + + def closestPalindrome(num, distance) do + cond do + # is the smaller number at this distance a palindrome? + isPalindrome(num - distance) -> + Integer.to_string(num - distance) + + # is the larger number at this distance a palindrome?a + isPalindrome(num + distance) -> + Integer.to_string(num + distance) + + # step 1 number futher away + true -> + closestPalindrome(num, distance + 1) + end + end + + def closestPalindrome(str) do + String.to_integer(str) |> closestPalindrome(1) + end + + def solution(str) do + IO.puts("Input: $str = \"#{str}\"") + IO.puts("Output: \"#{closestPalindrome(str)}\"") + end +end + +IO.puts("Example 1:") +PWC.solution("123") + +IO.puts("\nExample 2:") +PWC.solution("2") + +IO.puts("\nExample 3:") +PWC.solution("1400") + +IO.puts("\nExample 4:") +PWC.solution("1001") + +IO.puts("\nExample 5: (it doesn't say the input is a POSITIVE int)") +PWC.solution("-1") diff --git a/challenge-288/packy-anderson/perl/ch-1.pl b/challenge-288/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..688fa6929c --- /dev/null +++ b/challenge-288/packy-anderson/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +use v5.40; + +sub isPalindrome($num) { + # convert numerics to Strings, then reverse one of them + return "$num" eq reverse "$num"; +} + +sub closestPalindrome($str) { + my $num = $str + 0; # convert string to Integer + my $distance = 1; + while (true) { + # is the smaller number at this distance a palindrome? + if (isPalindrome($num - $distance)) { + return( ($num - $distance) ); + } + # is the larger number at this distance a palindrome? + if (isPalindrome($num + $distance)) { + return( ($num + $distance) ); + } + # step 1 number futher away + $distance++; + } +} + +sub solution($str) { + say "Input: \$str = \"$str\""; + my $closest = closestPalindrome($str); + say "Output: \"$closest\""; +} + +say "Example 1:"; +solution("123"); + +say "\nExample 2:"; +solution("2"); + +say "\nExample 3:"; +solution("1400"); + +say "\nExample 4:"; +solution("1001"); + +say "\nExample 5: (it doesn't say the input is a POSITIVE int)"; +solution("-1"); diff --git a/challenge-288/packy-anderson/perl/ch-2.pl b/challenge-288/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..2c61b69018 --- /dev/null +++ b/challenge-288/packy-anderson/perl/ch-2.pl @@ -0,0 +1,111 @@ +#!/usr/bin/env perl +use v5.40; + +use List::AllUtils qw( max ); + +# helper functions to determine if the adjacent cells +# BEFORE this one have the same value +sub prevXSame($matrix, $x, $y) { + return $x > 0 && $$matrix[$y]->[$x] eq $$matrix[$y]->[$x-1]; +} +sub prevYSame($matrix, $x, $y) { + return $y > 0 && $$matrix[$y]->[$x] eq $$matrix[$y-1]->[$x]; +} + +sub contiguousBlock($matrix) { + # first, find out the size of the matrix + my $height = $#{$matrix}; + my $width = $#{$$matrix[0]}; + # start a counter for the number of blocks + my $next_block = 0; + # a matrix of blocks + my @blocks; + + foreach my $y (0 .. $height) { + foreach my $x (0 .. $width) { + if (prevXSame($matrix, $x, $y)) { + # make this cell's block number match + # the one above it + $blocks[$y][$x] = $blocks[$y][$x-1]; + } + if (prevYSame($matrix, $x, $y)) { + # if we've already assigned a block number + # based on the prev X being the same, and + # it's a DIFFERENT block than the prev Y + if (defined($blocks[$y][$x]) && + $blocks[$y][$x] != $blocks[$y-1][$x]) { + # renumber the block for the prev X to + # match the block for the prev Y + my $new = $blocks[$y-1][$x]; + my $old = $blocks[$y][$x-1]; + foreach my $y2 (0 .. $y) { + foreach my $x2 (0 .. $width) { + $blocks[$y2][$x2] = $new + if $blocks[$y2][$x2] == $old; + } + } + } + # make this cell's block number match + # the one before it + $blocks[$y][$x] = $blocks[$y-1][$x]; + } + if (! defined $blocks[$y][$x]) { + # neither previous adjacent cell matches, + # assign a new block number to this cell + $blocks[$y][$x] = $next_block++; + } + } + } + + # now let's count the elements in each block + my @counts; + foreach my $y (0 .. $height) { + foreach my $x (0 .. $width) { + $counts[$blocks[$y][$x]]++; + } + } + + return max(@counts); +} + +sub solution($matrix) { + say 'Input: $matrix = ' . formatMatrix($matrix); + say 'Output: ' . contiguousBlock($matrix); +} + +sub formatMatrix($matrix, $indent=17) { + my @output; + foreach my $row (@$matrix) { + my $output_row = q{ } x $indent . " ["; + $output_row .= join(', ', map { sprintf "'%1s'", $_ } @$row) . ']'; + push @output, $output_row; + } + return "[\n" + . join(",\n", @output) + . "\n" + . q{ } x $indent . "]"; +} + +say "Example 1:"; +solution([ + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'o', 'o'], + ]); + +say "\nExample 2:"; +solution([ + ['x', 'x', 'x', 'x', 'x'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ]); + +say "\nExample 3:"; +solution([ + ['x', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ['o', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ]); diff --git a/challenge-288/packy-anderson/python/ch-1.py b/challenge-288/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..f19780b1a8 --- /dev/null +++ b/challenge-288/packy-anderson/python/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +def isPalindrome(num): + # convert numerics to Strings, then reverse one of them + return str(num) == str(num)[::-1] + +def closestPalindrome(string): + num = int(string) + distance = 1 + while True: + # is the smaller number at this distance a palindrome? + if isPalindrome(num - distance): + return str(num - distance) + + # is the larger number at this distance a palindrome? + if isPalindrome(num + distance): + return str(num + distance) + + # step 1 number futher away + distance += 1 + +def solution(string): + print(f'Input: $str = "{string}"') + print(f'Output: "{closestPalindrome(string)}"') + +print('Example 1:') +solution("123") + +print('\nExample 2:') +solution("2") + +print('\nExample 3:') +solution("1400") + +print('\nExample 4:') +solution("1001") + +print('\nExample 5: (it doesn\'t say the input is a POSITIVE int)') +solution("-1") diff --git a/challenge-288/packy-anderson/python/ch-2.py b/challenge-288/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..fd60c27664 --- /dev/null +++ b/challenge-288/packy-anderson/python/ch-2.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python + +# helper functions to determine if the adjacent cells +# BEFORE this one have the same value +def prevXSame(matrix, x, y): + return x > 0 and matrix[y][x] == matrix[y][x-1] + +def prevYSame(matrix, x, y): + return y > 0 and matrix[y][x] == matrix[y-1][x] + +def contiguousBlock(matrix): + # first, find out the size of the matrix + height = len(matrix) + width = len(matrix[0]) + # start a counter for the number of blocks + next_block = 0 + # a matrix of blocks + blocks = [[None for x in range(width)] for y in range(height)] + + for y in range(height): + for x in range(width): + if prevXSame(matrix, x, y): + # make this cell's block number match + # the one above it + blocks[y][x] = blocks[y][x-1] + + if prevYSame(matrix, x, y): + # if we've already assigned a block number + # based on the prev X being the same, and + # it's a DIFFERENT block than the prev Y + if (blocks[y][x] is not None and + blocks[y][x] != blocks[y-1][x]): + # renumber the block for the prev X to + # match the block for the prev Y + new = blocks[y-1][x] + old = blocks[y][x-1] + for y2 in range(y+1): + for x2 in range(width): + if blocks[y2][x2] == old: + blocks[y2][x2] = new + + # make this cell's block number match + # the one before it + blocks[y][x] = blocks[y-1][x] + + if blocks[y][x] is None: + # neither previous adjacent cell matches, + # assign a new block number to this cell + blocks[y][x] = next_block + next_block += 1 + + # now let's count the elements in each block + counts = [0 for x in range(next_block)] + for y in range(height): + for x in range(width): + counts[blocks[y][x]] += 1 + + return max(counts) + +def formatMatrix(matrix, indent=17): + output = [] + for row in matrix: + output_row = ' ' * indent + ' [' + output_row += ', '.join(map(lambda i: f"'{i}'", row)) + output_row += ']' + output.append(output_row) + + return( + "[\n" + ",\n".join(output) + "\n" + + ' ' * indent + ']' + ) + +def solution(matrix): + print(f'Input: $matrix = {formatMatrix(matrix)}') + print(f'Output: { contiguousBlock(matrix) }') + +print('Example 1:') +solution([ + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'o', 'o'], + ]) + +print('\nExample 2:') +solution([ + ['x', 'x', 'x', 'x', 'x'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ]) + +print('\nExample 3:') +solution([ + ['x', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ['o', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ]) diff --git a/challenge-288/packy-anderson/raku/ch-1.raku b/challenge-288/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..5c8d2a3fe6 --- /dev/null +++ b/challenge-288/packy-anderson/raku/ch-1.raku @@ -0,0 +1,45 @@ +#!/usr/bin/env raku +use v6; + +sub isPalindrome($num) { + # convert numerics to Strings, then reverse one of them + return $num.Str eq $num.Str.flip; +} + +sub closestPalindrome($str) { + my $num = $str.Int; # convert string to Integer + my $distance = 1; + while (True) { + # is the smaller number at this distance a palindrome? + if (isPalindrome($num - $distance)) { + return( ($num - $distance).Str ); + } + # is the larger number at this distance a palindrome? + if (isPalindrome($num + $distance)) { + return( ($num + $distance).Str ); + } + # step 1 number futher away + $distance++; + } +} + +sub solution($str) { + say "Input: \$str = \"$str\""; + my $closest = closestPalindrome($str); + say "Output: \"$closest\""; +} + +say "Example 1:"; +solution("123"); + +say "\nExample 2:"; +solution("2"); + +say "\nExample 3:"; +solution("1400"); + +say "\nExample 4:"; +solution("1001"); + +say "\nExample 5: (it doesn't say the input is a POSITIVE int)"; +solution("-1"); diff --git a/challenge-288/packy-anderson/raku/ch-2.raku b/challenge-288/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..03fca1f56c --- /dev/null +++ b/challenge-288/packy-anderson/raku/ch-2.raku @@ -0,0 +1,110 @@ +#!/usr/bin/env raku +use v6; + +# helper functions to determine if the adjacent cells +# BEFORE this one have the same value +sub prevXSame(@matrix, $x, $y) { + return $x > 0 && @matrix[$y][$x] eq @matrix[$y][$x-1]; +} +sub prevYSame(@matrix, $x, $y) { + return $y > 0 && @matrix[$y][$x] eq @matrix[$y-1][$x]; +} + +sub contiguousBlock(@matrix) { + # first, find out the size of the matrix + my $height = @matrix.end; + my $width = @matrix[0].end; + # start a counter for the number of blocks + my $next_block = 0; + # a matrix of blocks + my @blocks; + + for 0 .. $height -> $y { + for 0 .. $width -> $x { + if (prevXSame(@matrix, $x, $y)) { + # make this cell's block number match + # the one above it + @blocks[$y][$x] = @blocks[$y][$x-1]; + } + if (prevYSame(@matrix, $x, $y)) { + # if we've already assigned a block number + # based on the prev X being the same, and + # it's a DIFFERENT block than the prev Y + if (@blocks[$y][$x].defined && + @blocks[$y][$x] != @blocks[$y-1][$x]) { + # renumber the block for the prev X to + # match the block for the prev Y + my $new = @blocks[$y-1][$x]; + my $old = @blocks[$y][$x-1]; + for 0 .. $y -> $y2 { + for 0 .. $width -> $x2 { + @blocks[$y2][$x2] = $new + if @blocks[$y2][$x2] == $old; + } + } + } + # make this cell's block number match + # the one before it + @blocks[$y][$x] = @blocks[$y-1][$x]; + } + if (! @blocks[$y][$x].defined) { + # neither previous adjacent cell matches, + # assign a new block number to this cell + @blocks[$y][$x] = $next_block++; + } + } + } + + # now let's count the elements in each block + my @counts; + for 0 .. $height -> $y { + for 0 .. $width -> $x { + @counts[@blocks[$y][$x]]++; + } + } + + return max(@counts); +} + +sub solution(@matrix) { + say 'Input: $matrix = ' ~ formatMatrix(@matrix); + say 'Output: ' ~ contiguousBlock(@matrix); +} + +sub formatMatrix(@matrix, $indent=17) { + my @output; + for @matrix -> @row { + my $output_row = q{ } x $indent ~ " ["; + $output_row ~= @row.map({ sprintf "'%1s'", $_ }) + .join(', ') ~ "]"; + @output.push($output_row); + } + return "[\n" + ~ @output.join(",\n") + ~ "\n" + ~ q{ } x $indent ~ "]"; +} + +say "Example 1:"; +solution([ + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'o', 'o'], + ]); + +say "\nExample 2:"; +solution([ + ['x', 'x', 'x', 'x', 'x'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ]); + +say "\nExample 3:"; +solution([ + ['x', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ['o', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ]); |
