diff options
| author | Packy Anderson <packy@cpan.org> | 2024-09-25 01:26:17 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-09-25 01:28:31 -0400 |
| commit | 6db7aa87619e0d1d6e05be63baa1ab49e60279d7 (patch) | |
| tree | 278c660e146a483886e0a6a232af2f4522d29ad8 /challenge-288/packy-anderson/python | |
| parent | 9ced81d32e594001ffe943db9363bb48a4583d67 (diff) | |
| download | perlweeklychallenge-club-6db7aa87619e0d1d6e05be63baa1ab49e60279d7.tar.gz perlweeklychallenge-club-6db7aa87619e0d1d6e05be63baa1ab49e60279d7.tar.bz2 perlweeklychallenge-club-6db7aa87619e0d1d6e05be63baa1ab49e60279d7.zip | |
Challenge 288 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl
* Perl
* Python that definitely looks like Perl
* Elixir solution for Task 1 (for now)
1 Blog post
Diffstat (limited to 'challenge-288/packy-anderson/python')
| -rwxr-xr-x | challenge-288/packy-anderson/python/ch-1.py | 39 | ||||
| -rwxr-xr-x | challenge-288/packy-anderson/python/ch-2.py | 99 |
2 files changed, 138 insertions, 0 deletions
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'], + ]) |
