diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-05-25 19:10:21 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-05-25 19:10:21 +0200 |
| commit | c5f458236c03d80c209f6086666f2368dd2b58fe (patch) | |
| tree | 31627d584d223c6b865e70cbf965d4523bcb4421 | |
| parent | 31421edb60e97acb8fadb9e871fc0ca1c3d3c816 (diff) | |
| download | perlweeklychallenge-club-c5f458236c03d80c209f6086666f2368dd2b58fe.tar.gz perlweeklychallenge-club-c5f458236c03d80c209f6086666f2368dd2b58fe.tar.bz2 perlweeklychallenge-club-c5f458236c03d80c209f6086666f2368dd2b58fe.zip | |
feat(challenge-111/lubos-kolouch/perl,python/): Challenge 111 LK Perl Python
| -rw-r--r-- | challenge-111/lubos-kolouch/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-111/lubos-kolouch/perl/ch-2.pl | 29 | ||||
| -rw-r--r-- | challenge-111/lubos-kolouch/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-111/lubos-kolouch/python/ch-2.py | 22 |
4 files changed, 125 insertions, 0 deletions
diff --git a/challenge-111/lubos-kolouch/perl/ch-1.pl b/challenge-111/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..50997dafc0 --- /dev/null +++ b/challenge-111/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub search_matrix { + my ($matrix, $target) = @_; + my $rows = scalar @$matrix; + my $cols = scalar @{$matrix->[0]}; + + my ($left, $right) = (0, $rows * $cols - 1); + + while ($left <= $right) { + my $mid = int(($left + $right) / 2); + my $mid_value = $matrix->[$mid / $cols][$mid % $cols]; + + if ($mid_value == $target) { + return 1; + } elsif ($mid_value < $target) { + $left = $mid + 1; + } else { + $right = $mid - 1; + } + } + return 0; +} + +# Test cases: +my $matrix = [ + [1, 2, 3, 5, 7], + [9, 11, 15, 19, 20], + [23, 24, 25, 29, 31], + [32, 33, 39, 40, 42], + [45, 47, 48, 49, 50] +]; + +print search_matrix($matrix, 35), "\n"; # Output: 0 +print search_matrix($matrix, 39), "\n"; # Output: 1 + diff --git a/challenge-111/lubos-kolouch/perl/ch-2.pl b/challenge-111/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..2f581495a8 --- /dev/null +++ b/challenge-111/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,29 @@ +use strict; +use warnings; + +sub find_longest_ordered_words { + my ($filename) = @_; + + open my $fh, '<', $filename or die "Could not open $filename: $!"; + my @words = <$fh>; + chomp @words; + close $fh; + + my $longest_length = 0; + my @longest_words; + for my $word (@words) { + if (lc($word) eq join('', sort split //, lc($word))) { + if (length($word) > $longest_length) { + $longest_length = length($word); + @longest_words = ($word); + } elsif (length($word) == $longest_length) { + push @longest_words, $word; + } + } + } + return @longest_words; +} + +my @longest_ordered_words = find_longest_ordered_words('words.txt'); +print join("\n", @longest_ordered_words), "\n"; + diff --git a/challenge-111/lubos-kolouch/python/ch-1.py b/challenge-111/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..8ad7f124b6 --- /dev/null +++ b/challenge-111/lubos-kolouch/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def search_matrix(matrix: List[List[int]], target: int) -> bool: + if not matrix: + return False + + rows, cols = len(matrix), len(matrix[0]) + left, right = 0, rows * cols - 1 + + while left <= right: + mid = (left + right) // 2 + mid_value = matrix[mid // cols][mid % cols] + + if mid_value == target: + return True + elif mid_value < target: + left = mid + 1 + else: + right = mid - 1 + + return False + + +# Test cases: +matrix = [[1, 2, 3, 5, 7], + [9, 11, 15, 19, 20], + [23, 24, 25, 29, 31], + [32, 33, 39, 40, 42], + [45, 47, 48, 49, 50]] + +print(search_matrix(matrix, 35)) # Output: False +print(search_matrix(matrix, 39)) # Output: True diff --git a/challenge-111/lubos-kolouch/python/ch-2.py b/challenge-111/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..1f9491fd0d --- /dev/null +++ b/challenge-111/lubos-kolouch/python/ch-2.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def find_longest_ordered_words(filename): + with open(filename, 'r') as f: + words = f.read().splitlines() + + longest_length = 0 + longest_words = [] + for word in words: + if word.lower() == ''.join(sorted(word.lower())): + if len(word) > longest_length: + longest_length = len(word) + longest_words = [word] + elif len(word) == longest_length: + longest_words.append(word) + return longest_words + + +longest_ordered_words = find_longest_ordered_words('words.txt') +for word in longest_ordered_words: + print(word) |
