diff options
| -rw-r--r-- | challenge-288/aplgr/.gitignore | 1 | ||||
| -rw-r--r-- | challenge-288/aplgr/README | 1 | ||||
| -rw-r--r-- | challenge-288/aplgr/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-288/aplgr/go/ch-1.go | 36 | ||||
| -rw-r--r-- | challenge-288/aplgr/go/ch-1_test.go | 22 | ||||
| -rw-r--r-- | challenge-288/aplgr/go/ch-2.go | 44 | ||||
| -rw-r--r-- | challenge-288/aplgr/go/ch-2_test.go | 51 | ||||
| -rw-r--r-- | challenge-288/aplgr/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-288/aplgr/perl/ch-1.t | 18 | ||||
| -rw-r--r-- | challenge-288/aplgr/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-288/aplgr/perl/ch-2.t | 35 |
11 files changed, 276 insertions, 0 deletions
diff --git a/challenge-288/aplgr/.gitignore b/challenge-288/aplgr/.gitignore new file mode 100644 index 0000000000..780010228f --- /dev/null +++ b/challenge-288/aplgr/.gitignore @@ -0,0 +1 @@ +go.mod
\ No newline at end of file diff --git a/challenge-288/aplgr/README b/challenge-288/aplgr/README new file mode 100644 index 0000000000..5bd6a6b0cb --- /dev/null +++ b/challenge-288/aplgr/README @@ -0,0 +1 @@ +Solution by André Plöger. diff --git a/challenge-288/aplgr/blog.txt b/challenge-288/aplgr/blog.txt new file mode 100644 index 0000000000..728c48f84b --- /dev/null +++ b/challenge-288/aplgr/blog.txt @@ -0,0 +1 @@ +https://dev.to/aplgr/diving-deep-recursive-solutions-for-palindromes-and-contiguous-blocks-1pah
\ No newline at end of file diff --git a/challenge-288/aplgr/go/ch-1.go b/challenge-288/aplgr/go/ch-1.go new file mode 100644 index 0000000000..48eae68c5a --- /dev/null +++ b/challenge-288/aplgr/go/ch-1.go @@ -0,0 +1,36 @@ +package main + +import ( + "strconv" +) + +func isPalindrome(num int) bool { + reversed := 0 + original := num + + for num > 0 { + digit := num % 10 + reversed = reversed*10 + digit + num /= 10 + } + + return original == reversed +} + +func findClosest(lower, upper, original int) string { + switch { + case isPalindrome(lower) && lower != original: + return strconv.Itoa(lower) + case isPalindrome(upper) && upper != original: + return strconv.Itoa(upper) + case lower > 0: + return findClosest(lower-1, upper+1, original) + default: + return strconv.Itoa(upper + 1) + } +} + +func closestPalindrome(str string) string { + num, _ := strconv.Atoi(str) + return findClosest(num-1, num+1, num) +} diff --git a/challenge-288/aplgr/go/ch-1_test.go b/challenge-288/aplgr/go/ch-1_test.go new file mode 100644 index 0000000000..cdbe1a02e1 --- /dev/null +++ b/challenge-288/aplgr/go/ch-1_test.go @@ -0,0 +1,22 @@ +package main + +import ( + "testing" +) + +func TestClosestPalindrome(t *testing.T) { + testCases := [][]string{ + { "123", "121" }, + { "2", "1" }, + { "1400", "1441" }, + { "1001", "999" }, + } + + for _, tc := range testCases { + got := closestPalindrome(tc[0]) + want := tc[1] + if got != want { + t.Errorf("closestPalindrome(%s) = %s; want %s", tc[0], got, want) + } + } +} diff --git a/challenge-288/aplgr/go/ch-2.go b/challenge-288/aplgr/go/ch-2.go new file mode 100644 index 0000000000..b3c118f91e --- /dev/null +++ b/challenge-288/aplgr/go/ch-2.go @@ -0,0 +1,44 @@ +package main + +func largestContiguousBlock(matrix [][]rune) int { + rows := len(matrix) + if rows == 0 { + return 0 + } + cols := len(matrix[0]) + visited := make([][]bool, rows) + for i := range visited { + visited[i] = make([]bool, cols) + } + + maxSize := 0 + + for r := 0; r < rows; r++ { + for c := 0; c < cols; c++ { + symbol := matrix[r][c] + size := dfs(matrix, visited, r, c, symbol) + if size > maxSize { + maxSize = size + } + } + } + + return maxSize +} + +func dfs(matrix [][]rune, visited [][]bool, row, col int, symbol rune) int { + if row < 0 || row >= len(matrix) || col < 0 || col >= len(matrix[0]) || + visited[row][col] || matrix[row][col] != symbol { + return 0 + } + + visited[row][col] = true + count := 1 + + count += dfs(matrix, visited, row+1, col, symbol) + count += dfs(matrix, visited, row-1, col, symbol) + count += dfs(matrix, visited, row, col+1, symbol) + count += dfs(matrix, visited, row, col-1, symbol) + + return count +} diff --git a/challenge-288/aplgr/go/ch-2_test.go b/challenge-288/aplgr/go/ch-2_test.go new file mode 100644 index 0000000000..b1c45a976d --- /dev/null +++ b/challenge-288/aplgr/go/ch-2_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "testing" +) + +func TestLargestContiguousBlock(t *testing.T) { + tests := []struct { + matrix [][]rune + expected int + }{ + { + matrix: [][]rune{ + {'x', 'x', 'x', 'x', 'o'}, + {'x', 'o', 'o', 'o', 'o'}, + {'x', 'o', 'o', 'o', 'o'}, + {'x', 'x', 'x', 'o', 'o'}, + }, + expected: 11, + }, + { + matrix: [][]rune{ + {'x', 'x', 'x', 'x', 'x'}, + {'x', 'o', 'o', 'o', 'o'}, + {'x', 'x', 'x', 'x', 'o'}, + {'x', 'o', 'o', 'o', 'o'}, + }, + expected: 11, + }, + { + matrix: [][]rune{ + {'x', 'x', 'x', 'o', 'o'}, + {'o', 'o', 'o', 'x', 'x'}, + {'o', 'x', 'x', 'o', 'o'}, + {'o', 'o', 'o', 'x', 'x'}, + }, + expected: 7, + }, + { + matrix: [][]rune{}, + expected: 0, + }, + } + + for i, test := range tests { + result := largestContiguousBlock(test.matrix) + if result != test.expected { + t.Errorf("Test %d failed: expected %d, got %d", i+1, test.expected, result) + } + } +} diff --git a/challenge-288/aplgr/perl/ch-1.pl b/challenge-288/aplgr/perl/ch-1.pl new file mode 100644 index 0000000000..f53c50101b --- /dev/null +++ b/challenge-288/aplgr/perl/ch-1.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; + +sub is_palindrome { + my ($num) = @_; + return $num eq reverse($num); +} + +sub find_closest { + my ($lower, $upper, $original) = @_; + return $lower if is_palindrome($lower) && $lower != $original; + return $upper if is_palindrome($upper) && $upper != $original; + return find_closest($lower-1, $upper+1, $original) if $lower > 0; + return $upper + 1 +} + +sub closest_palindrome { + my ($str) = @_; + my $num = int($str); + return find_closest($num -1, $num + 1, $num); +} + +1;
\ No newline at end of file diff --git a/challenge-288/aplgr/perl/ch-1.t b/challenge-288/aplgr/perl/ch-1.t new file mode 100644 index 0000000000..6d535ad8e0 --- /dev/null +++ b/challenge-288/aplgr/perl/ch-1.t @@ -0,0 +1,18 @@ +use strict; +use warnings; +use Test::More; +require "./ch-1.pl"; + +my @test_cases = ( + [ "123", "121" ], + [ "2", "1" ], + [ "1400", "1441" ], + [ "1001", "999" ], +); + +# Tests durchführen +foreach my $case (@test_cases) { + is(closest_palindrome($case->[0]), $case->[1], "Closest palindrome to $case->[0] is $case->[1]"); +} + +done_testing(); diff --git a/challenge-288/aplgr/perl/ch-2.pl b/challenge-288/aplgr/perl/ch-2.pl new file mode 100644 index 0000000000..c64dc30116 --- /dev/null +++ b/challenge-288/aplgr/perl/ch-2.pl @@ -0,0 +1,44 @@ +use strict; +use warnings; + + +#https://www.lavivienpost.com/depth-first-search-and-matrix/ + + +sub largest_contiguous_block { + my ($matrix) = @_; + my $rows = @$matrix; + my $cols = @{$matrix->[0]}; + my @visited = map { [(0) x $cols] } 1..$rows; + + my $max_size = 0; + + for my $r (0 .. $rows - 1) { + for my $c (0 .. $cols - 1) { + my $symbol = $matrix->[$r][$c]; + my $size = dfs($matrix, \@visited, $r, $c, $symbol); + $max_size = $size if $size > $max_size; + } + } + + return $max_size; +} + +sub dfs { + my ($matrix, $visited, $row, $col, $symbol) = @_; + + return 0 if $row < 0 || $row >= @$matrix || $col < 0 || $col >= @{$matrix->[0]} + || $visited->[$row][$col] || $matrix->[$row][$col] ne $symbol; + + $visited->[$row][$col] = 1; + my $count = 1; + + $count += dfs($matrix, $visited, $row + 1, $col, $symbol); + $count += dfs($matrix, $visited, $row - 1, $col, $symbol); + $count += dfs($matrix, $visited, $row, $col + 1, $symbol); + $count += dfs($matrix, $visited, $row, $col - 1, $symbol); + + return $count; +} + +1;
\ No newline at end of file diff --git a/challenge-288/aplgr/perl/ch-2.t b/challenge-288/aplgr/perl/ch-2.t new file mode 100644 index 0000000000..0a82b2c3e3 --- /dev/null +++ b/challenge-288/aplgr/perl/ch-2.t @@ -0,0 +1,35 @@ +use strict; +use warnings; +use Test::More; + +require './ch-2.pl'; + +my @matrices = ( + [ + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'o', 'o'], + ], + [ + ['x', 'x', 'x', 'x', 'x'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ], + [ + ['x', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ['o', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ], +); + +my @expected_results = (11, 11, 7); + +for my $i (0 .. $#matrices) { + my $result = largest_contiguous_block($matrices[$i]); + is($result, $expected_results[$i], "Test case $i: Size of largest contiguous block is $result"); +} + +done_testing(); |
