diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-08-08 10:46:15 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-08-08 10:46:15 +0200 |
| commit | 5da36d0a50855edf2b59e483452ec20f74b78da6 (patch) | |
| tree | 1020cb62670d7b7a75d1d8ee64c13cebede74033 | |
| parent | e321394215212f81f3c7cedf82ea86ce9c492dff (diff) | |
| download | perlweeklychallenge-club-5da36d0a50855edf2b59e483452ec20f74b78da6.tar.gz perlweeklychallenge-club-5da36d0a50855edf2b59e483452ec20f74b78da6.tar.bz2 perlweeklychallenge-club-5da36d0a50855edf2b59e483452ec20f74b78da6.zip | |
feat(challenge-194/lubos-kolouch/perl,python/): Challenge 194 LK Perl Python
| -rw-r--r-- | challenge-194/lubos-kolouch/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-194/lubos-kolouch/perl/ch-2.pl | 37 | ||||
| -rw-r--r-- | challenge-194/lubos-kolouch/python/ch-1.py | 56 | ||||
| -rw-r--r-- | challenge-194/lubos-kolouch/python/ch-2.py | 36 |
4 files changed, 172 insertions, 0 deletions
diff --git a/challenge-194/lubos-kolouch/perl/ch-1.pl b/challenge-194/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..53670f261c --- /dev/null +++ b/challenge-194/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; + +sub find_highest_digit { + my ($time) = @_; + my ( $hour, $minute ) = split( ':', $time ); + + # Handle the hour part + if ( $hour =~ /^\?(\d)$/ ) { + my $units_digit = $1; + if ( $units_digit <= 3 ) { + return 2; + } + else { + return 1; + } + } + if ( $hour =~ /\?$/ ) { + return ( $hour =~ /^1./ ) ? 9 : 3; + } + + # Handle the minute part + if ( $minute =~ /^\?/ ) { + return 5; + } + if ( $minute =~ /\?$/ ) { + return 9; + } + + return; # Should never reach here +} + +is( find_highest_digit('?5:00'), 1, 'Example 1' ); +is( find_highest_digit('?3:00'), 2, 'Example 2' ); +is( find_highest_digit('1?:00'), 9, 'Example 3' ); +is( find_highest_digit('2?:00'), 3, 'Example 4' ); +is( find_highest_digit('12:?5'), 5, 'Example 5' ); +is( find_highest_digit('12:5?'), 9, 'Example 6' ); + +done_testing(); diff --git a/challenge-194/lubos-kolouch/perl/ch-2.pl b/challenge-194/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..85f085514a --- /dev/null +++ b/challenge-194/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; + +sub frequency_equalizer { + my ($s) = @_; + my %frequency; + + # Count the frequency of each character in the string + $frequency{$_}++ for split //, $s; + + # Create a frequency histogram to track the count of each frequency + my %frequency_histogram; + $frequency_histogram{$_}++ for values %frequency; + + # Check if there's only one frequency, and that removing one occurrence will not equalize frequencies + return 0 if keys %frequency_histogram == 1 && ( values %frequency_histogram )[0] == 1; + + # Check if there is one frequency that appears only once and the rest are the same + if ( grep { $frequency_histogram{$_} == 1 } keys %frequency_histogram ) { + my @unique_freq = grep { $frequency_histogram{$_} == 1 } keys %frequency_histogram; + delete $frequency_histogram{ $unique_freq[0] }; + return 1 + if keys %frequency_histogram == 1 + && ( $unique_freq[0] == 1 || $unique_freq[0] - 1 == ( keys %frequency_histogram )[0] ); + } + + return 0; +} + +is( frequency_equalizer('abbc'), 1, 'One character with frequency one more than others' ); +is( frequency_equalizer('xyzyyxz'), 1, 'One character with frequency one more than others, mixed' ); +is( frequency_equalizer('xzxz'), 0, 'All characters with equal frequency' ); + +done_testing(); diff --git a/challenge-194/lubos-kolouch/python/ch-1.py b/challenge-194/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..f897869a76 --- /dev/null +++ b/challenge-194/lubos-kolouch/python/ch-1.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest + + +def find_highest_digit(time: str) -> int: + hour, minute = time.split(':') + + # Handle the hour part + if hour[0] == '?': + units_digit = int(hour[1]) + if units_digit <= 3: + return 2 + else: + return 1 + if hour[1] == '?': + return 9 if hour[0] == '1' else 3 + + # Handle the minute part + if minute[0] == '?': + return 5 + if minute[1] == '?': + return 9 + + return -1 # Should never reach here + + +class TestFindHighestDigit(unittest.TestCase): + def test_example_1(self): + self.assertEqual(find_highest_digit('?5:00'), 1, + "Tens place missing in hour, units digit 5") + + def test_example_2(self): + self.assertEqual(find_highest_digit('?3:00'), 2, + "Tens place missing in hour, units digit 3") + + def test_example_3(self): + self.assertEqual(find_highest_digit('1?:00'), 9, + "Units place missing in hour, tens digit 1") + + def test_example_4(self): + self.assertEqual(find_highest_digit('2?:00'), 3, + "Units place missing in hour, tens digit 2") + + def test_example_5(self): + self.assertEqual(find_highest_digit('12:?5'), 5, + "Tens place missing in minute") + + def test_example_6(self): + self.assertEqual(find_highest_digit('12:5?'), 9, + "Units place missing in minute") + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-194/lubos-kolouch/python/ch-2.py b/challenge-194/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..66a5292cc0 --- /dev/null +++ b/challenge-194/lubos-kolouch/python/ch-2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from collections import Counter + + +def frequency_equalizer(s): + # Count the frequency of each character in the string + frequency = Counter(s) + + # Create a frequency histogram to track the count of each frequency + frequency_histogram = Counter(frequency.values()) + + # Check if there's only one frequency, and that removing one occurrence will not equalize frequencies + if len(frequency_histogram) == 1 and list(frequency_histogram.values())[0] == 1: + return 0 + + # Check if there is one frequency that appears only once and the rest are the same + unique_freq = [freq for freq, + count in frequency_histogram.items() if count == 1] + if unique_freq: + del frequency_histogram[unique_freq[0]] + if len(frequency_histogram) == 1 and (unique_freq[0] == 1 or unique_freq[0] - 1 == list(frequency_histogram.keys())[0]): + return 1 + + return 0 + + +# Test Cases +assert frequency_equalizer( + 'abbc') == 1, 'One character with frequency one more than others' +assert frequency_equalizer( + 'xyzyyxz') == 1, 'One character with frequency one more than others, mixed' +assert frequency_equalizer('xzxz') == 0, 'All characters with equal frequency' + +print("All tests passed!") |
