From dc33da454b62dad3a43f4277b477892774ed82b5 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 20 Apr 2024 15:38:13 +0200 Subject: Challenge 265 LK Perl Python --- challenge-265/lubos-kolouch/perl/ch-1.pl | 32 +++++++++++++++++ challenge-265/lubos-kolouch/perl/ch-2.pl | 56 ++++++++++++++++++++++++++++++ challenge-265/lubos-kolouch/python/ch-1.py | 25 +++++++++++++ challenge-265/lubos-kolouch/python/ch-2.py | 20 +++++++++++ 4 files changed, 133 insertions(+) create mode 100644 challenge-265/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-265/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-265/lubos-kolouch/python/ch-1.py create mode 100644 challenge-265/lubos-kolouch/python/ch-2.py diff --git a/challenge-265/lubos-kolouch/perl/ch-1.pl b/challenge-265/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..9b898ca552 --- /dev/null +++ b/challenge-265/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,32 @@ +use strict; +use warnings; + +sub find_integer_appearing_33_percent_or_more { + my @ints = @_; + return undef unless @ints; + + my %count; + my $n = scalar @ints; + my $threshold = $n / 3; + + foreach my $int (@ints) { + $count{$int}++; + } + + my @valid_numbers = grep { $count{$_} >= $threshold } keys %count; + + @valid_numbers = sort { $a <=> $b } @valid_numbers; + + return $valid_numbers[0] if @valid_numbers; + return undef; +} + +# Test cases +use Test::More tests => 4; + +is(find_integer_appearing_33_percent_or_more(1, 2, 3, 3, 3, 3, 4, 2), 3, 'Example 1'); +is(find_integer_appearing_33_percent_or_more(1, 1), 1, 'Example 2'); +is(find_integer_appearing_33_percent_or_more(1, 2, 3), 1, 'Example 3'); +is(find_integer_appearing_33_percent_or_more(), undef, 'Empty list'); + +done_testing(); diff --git a/challenge-265/lubos-kolouch/perl/ch-2.pl b/challenge-265/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..8acd0caef4 --- /dev/null +++ b/challenge-265/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,56 @@ +package Ch2; +use strict; +use warnings; +use List::Util qw(min); + +sub shortest_completing_word { + my ( $target, @words ) = @_; + my %target_count = get_letter_counts($target); + + my @valid_words; + foreach my $word (@words) { + my %word_count = get_letter_counts($word); + push @valid_words, $word + if is_completing( \%target_count, \%word_count ); + } + + return minstr(@valid_words); +} + +sub get_letter_counts { + my $s = shift; + my %counts; + $s =~ s/[^a-zA-Z]//gx; + $s = lc $s; + $counts{$_}++ for split //, $s; + return %counts; +} + +sub is_completing { + my ( $target_count, $word_count ) = @_; + foreach my $letter ( keys %$target_count ) { + return 0 + if !$word_count->{$letter} + || $word_count->{$letter} < $target_count->{$letter}; + } + return 1; +} + +sub minstr { + my @strings = @_; + return qw// unless @strings; + return ( sort { length $a <=> length $b } @strings )[0]; +} + +# Test cases +use Test::More tests => 3; +is( shortest_completing_word( 'aBc 11c', 'accbbb', 'abc', 'abbc' ), + 'accbbb', 'Example 1' ); +is( shortest_completing_word( 'Da2 abc', 'abcm', 'baacd', 'abaadc' ), + 'baacd', 'Example 2' ); +is( shortest_completing_word( 'JB 007', 'jj', 'bb', 'bjb' ), + 'bjb', 'Example 3' ); + +done_testing(); + +1; \ No newline at end of file diff --git a/challenge-265/lubos-kolouch/python/ch-1.py b/challenge-265/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..c059f9fa60 --- /dev/null +++ b/challenge-265/lubos-kolouch/python/ch-1.py @@ -0,0 +1,25 @@ +from collections import Counter +from typing import List, Optional + + +def find_integer_appearing_33_percent_or_more(ints: List[int]) -> Optional[int]: + + if not ints: + return None + + count = Counter(ints) + n = len(ints) + threshold = n / 3 + + # Finding all numbers that appear at least 33% of the time + valid_numbers = [num for num, cnt in count.items() if cnt >= threshold] + + # Return the smallest integer that meets the criteria, or None if no such integer exists + return min(valid_numbers) if valid_numbers else None + + +# Test cases +assert find_integer_appearing_33_percent_or_more([1, 2, 3, 3, 3, 3, 4, 2]) == 3 +assert find_integer_appearing_33_percent_or_more([1, 1]) == 1 +assert find_integer_appearing_33_percent_or_more([1, 2, 3]) == 1 +assert find_integer_appearing_33_percent_or_more([]) is None diff --git a/challenge-265/lubos-kolouch/python/ch-2.py b/challenge-265/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..98270f0f99 --- /dev/null +++ b/challenge-265/lubos-kolouch/python/ch-2.py @@ -0,0 +1,20 @@ +from collections import Counter +from typing import List + + +def shortest_completing_word(target: str, words: List[str]) -> str: + # Function to clean and count the letters in a word + def get_letter_counts(s: str) -> Counter: + return Counter(c.lower() for c in s if c.isalpha()) + + target_count = get_letter_counts(target) + + # Filter and find the shortest completing word + valid_words = [word for word in words if not target_count - get_letter_counts(word)] + return min(valid_words, key=len) if valid_words else "" + + +# Test cases +assert shortest_completing_word("aBc 11c", ["accbbb", "abc", "abbc"]) == "accbbb" +assert shortest_completing_word("Da2 abc", ["abcm", "baacd", "abaadc"]) == "baacd" +assert shortest_completing_word("JB 007", ["jj", "bb", "bjb"]) == "bjb" -- cgit