diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-05-05 15:00:57 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-05-05 15:00:57 +0200 |
| commit | 30e70cc69f62c9cda1cb48eb13f130721c1cee37 (patch) | |
| tree | d4a6e26da74a52d3f0973eb27cf650b6d34c6291 /challenge-064 | |
| parent | fca710e33f9f55af0a163c95ad060a4d1c6e7be5 (diff) | |
| download | perlweeklychallenge-club-30e70cc69f62c9cda1cb48eb13f130721c1cee37.tar.gz perlweeklychallenge-club-30e70cc69f62c9cda1cb48eb13f130721c1cee37.tar.bz2 perlweeklychallenge-club-30e70cc69f62c9cda1cb48eb13f130721c1cee37.zip | |
Challenges 064 065 066 067 LK Perl Python
Diffstat (limited to 'challenge-064')
| -rw-r--r-- | challenge-064/lubos-kolouch/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-064/lubos-kolouch/perl/ch-2.pl | 31 | ||||
| -rw-r--r-- | challenge-064/lubos-kolouch/python/ch-1.py | 26 | ||||
| -rw-r--r-- | challenge-064/lubos-kolouch/python/ch-2.py | 30 |
4 files changed, 125 insertions, 0 deletions
diff --git a/challenge-064/lubos-kolouch/perl/ch-1.pl b/challenge-064/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..2b6420eecc --- /dev/null +++ b/challenge-064/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,38 @@ +use strict; +use warnings; + +sub min_sum_path { + my ($matrix) = @_; + my $rows = @$matrix; + my $columns = @{$matrix->[0]}; + + for my $i (1 .. $rows - 1) { + $matrix->[$i][0] += $matrix->[$i - 1][0]; + } + + for my $j (1 .. $columns - 1) { + $matrix->[0][$j] += $matrix->[0][$j - 1]; + } + + for my $i (1 .. $rows - 1) { + for my $j (1 .. $columns - 1) { + $matrix->[$i][$j] += min($matrix->[$i - 1][$j], $matrix->[$i][$j - 1]); + } + } + + return $matrix->[-1][-1]; +} + +sub min { + my ($a, $b) = @_; + return $a < $b ? $a : $b; +} + +my $matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9] +]; + +print "Minimum sum path: ", min_sum_path($matrix), "\n"; # Output: 21 + diff --git a/challenge-064/lubos-kolouch/perl/ch-2.pl b/challenge-064/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..9dc2a98517 --- /dev/null +++ b/challenge-064/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,31 @@ +use strict; +use warnings; + +sub word_sequence { + my ($S, $W) = @_; + return helper($S, $W, []); +} + +sub helper { + my ($s, $W, $words) = @_; + return $words unless $s; + + for my $word (@$W) { + if (index($s, $word) == 0) { + my $result = helper(substr($s, length($word)), $W, [@$words, $word]); + return $result if @$result; + } + } + + return []; +} + +my $S1 = "perlweeklychallenge"; +my $W1 = ["weekly", "challenge", "perl"]; + +my $S2 = "perlandraku"; +my $W2 = ["python", "ruby", "haskell"]; + +print join(', ', @{word_sequence($S1, $W1)}), "\n"; # Output: perl, weekly, challenge +print join(', ', @{word_sequence($S2, $W2)}), "\n"; # Output: 0 + diff --git a/challenge-064/lubos-kolouch/python/ch-1.py b/challenge-064/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..1d513712a3 --- /dev/null +++ b/challenge-064/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def min_sum_path(matrix: List[List[int]]) -> int: + rows = len(matrix) + columns = len(matrix[0]) + + for i in range(1, rows): + matrix[i][0] += matrix[i - 1][0] + + for j in range(1, columns): + matrix[0][j] += matrix[0][j - 1] + + for i in range(1, rows): + for j in range(1, columns): + matrix[i][j] += min(matrix[i - 1][j], matrix[i][j - 1]) + + return matrix[-1][-1] + + +matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + +print(f"Minimum sum path: {min_sum_path(matrix)}") # Output: 21 diff --git a/challenge-064/lubos-kolouch/python/ch-2.py b/challenge-064/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..df2d2041c3 --- /dev/null +++ b/challenge-064/lubos-kolouch/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def word_sequence(S: str, W: List[str]) -> List[str]: + + def helper(s: str, words: List[str]) -> List[str]: + if not s: + return words + for word in W: + if s.startswith(word): + result = helper(s[len(word):], words + [word]) + if result: + return result + return [] + + result = helper(S, []) + return result if result else [0] + + +S1 = "perlweeklychallenge" +W1 = ["weekly", "challenge", "perl"] + +S2 = "perlandraku" +W2 = ["python", "ruby", "haskell"] + +print(word_sequence(S1, W1)) # Output: ['perl', 'weekly', 'challenge'] +print(word_sequence(S2, W2)) # Output: [0] |
