diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-08-08 11:32:58 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-08-08 11:32:58 +0200 |
| commit | 267f197dc9dd52a0a7dac2d59bccc642abe542a0 (patch) | |
| tree | 13c80210ef02fb81521be73c49244bc4995d583c | |
| parent | 5da36d0a50855edf2b59e483452ec20f74b78da6 (diff) | |
| download | perlweeklychallenge-club-267f197dc9dd52a0a7dac2d59bccc642abe542a0.tar.gz perlweeklychallenge-club-267f197dc9dd52a0a7dac2d59bccc642abe542a0.tar.bz2 perlweeklychallenge-club-267f197dc9dd52a0a7dac2d59bccc642abe542a0.zip | |
feat(challenge-195/lubos-kolouch/perl,python/): Challenge 195 LK Perl Python
| -rw-r--r-- | challenge-195/lubos-kolouch/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-195/lubos-kolouch/perl/ch-2.pl | 33 | ||||
| -rw-r--r-- | challenge-195/lubos-kolouch/python/ch-1.py | 23 | ||||
| -rw-r--r-- | challenge-195/lubos-kolouch/python/ch-2.py | 26 |
4 files changed, 110 insertions, 0 deletions
diff --git a/challenge-195/lubos-kolouch/perl/ch-1.pl b/challenge-195/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..55fbd5bcf9 --- /dev/null +++ b/challenge-195/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; +use List::Util qw/all/; + +sub count_special_integers { + my ($n) = @_; + my $count = 0; + + # Iterate through 1 to $n and check if each number has unique digits + for my $i ( 1 .. $n ) { + my %digits_seen; + $digits_seen{$_}++ for split //, $i; + + # If all digits are unique, the number is special + $count++ if all { $_ == 1 } values %digits_seen; + } + + return $count; +} + +# Test Cases +is( count_special_integers(15), 14, 'Count of special integers up to 15' ); +is( count_special_integers(35), 32, 'Count of special integers up to 35' ); + +done_testing(); diff --git a/challenge-195/lubos-kolouch/perl/ch-2.pl b/challenge-195/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..0f35f67787 --- /dev/null +++ b/challenge-195/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use List::Util qw(min max); +use Test::More; + +sub most_frequent_even { + my (@list) = @_; + + my %frequency; + + # Count the frequency of even numbers + for my $num (@list) { + $frequency{$num}++ if $num % 2 == 0; + } + + # If no even numbers found + return -1 unless keys %frequency; + + # Find the even number with the highest frequency + my $max_freq = max values %frequency; + my @candidates = grep { $frequency{$_} == $max_freq } keys %frequency; + + return min @candidates; +} + +# Test Cases +is( most_frequent_even( 1, 1, 2, 6, 2 ), 2, 'Most frequent even number' ); +is( most_frequent_even( 1, 3, 5, 7 ), -1, 'No even number' ); +is( most_frequent_even( 6, 4, 4, 6, 1 ), 4, 'Multiple most frequent even numbers' ); + +done_testing(); diff --git a/challenge-195/lubos-kolouch/python/ch-1.py b/challenge-195/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..62d4921709 --- /dev/null +++ b/challenge-195/lubos-kolouch/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def count_special_integers(n): + count = 0 + + # Iterate through 1 to n and check if each number has unique digits + for i in range(1, n + 1): + digits_seen = {} + for digit in str(i): + digits_seen[digit] = digits_seen.get(digit, 0) + 1 + # If all digits are unique, the number is special + if all(value == 1 for value in digits_seen.values()): + count += 1 + + return count + + +# Test Cases +assert count_special_integers(15) == 14, 'Count of special integers up to 15' +assert count_special_integers(35) == 32, 'Count of special integers up to 35' + +print("All tests passed!") diff --git a/challenge-195/lubos-kolouch/python/ch-2.py b/challenge-195/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..5ff21a573b --- /dev/null +++ b/challenge-195/lubos-kolouch/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from collections import Counter + + +def most_frequent_even(numbers): + even_frequency = Counter(num for num in numbers if num % 2 == 0) + + if not even_frequency: + return -1 + + max_freq = max(even_frequency.values()) + candidates = [num for num, freq in even_frequency.items() + if freq == max_freq] + + return min(candidates) + + +# Test Cases +assert most_frequent_even([1, 1, 2, 6, 2]) == 2, 'Most frequent even number' +assert most_frequent_even([1, 3, 5, 7]) == -1, 'No even number' +assert most_frequent_even( + [6, 4, 4, 6, 1]) == 4, 'Multiple most frequent even numbers' + +print("All tests passed!") |
