diff options
| author | vamsimeenavilli <vamsi.meenavilli@gmail.com> | 2022-12-06 22:30:23 +0530 |
|---|---|---|
| committer | vamsimeenavilli <vamsi.meenavilli@gmail.com> | 2022-12-06 22:30:23 +0530 |
| commit | 9b4fc20da72bbfc067c8f4579c5eb23824c6d691 (patch) | |
| tree | 51b819ae47f811797b772eb7da7827dd1a67b1d5 | |
| parent | b1726df08f5b305243c77b4955131f36b6660c59 (diff) | |
| download | perlweeklychallenge-club-9b4fc20da72bbfc067c8f4579c5eb23824c6d691.tar.gz perlweeklychallenge-club-9b4fc20da72bbfc067c8f4579c5eb23824c6d691.tar.bz2 perlweeklychallenge-club-9b4fc20da72bbfc067c8f4579c5eb23824c6d691.zip | |
Vamsi's Weekly Challenge 194 perl solutions
| -rw-r--r-- | challenge-194/vamsi-meenavilli/perl/ch-1.pl | 57 | ||||
| -rw-r--r-- | challenge-194/vamsi-meenavilli/perl/ch-2.pl | 42 |
2 files changed, 99 insertions, 0 deletions
diff --git a/challenge-194/vamsi-meenavilli/perl/ch-1.pl b/challenge-194/vamsi-meenavilli/perl/ch-1.pl new file mode 100644 index 0000000000..6c9e74c04e --- /dev/null +++ b/challenge-194/vamsi-meenavilli/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl +use strict; +use warnings FATAL => 'all'; + +use Test2::V0; + +=head1 AUTHORS + +Vamsi Meenavilli + +=head1 DESCRIPTION + + Week 194: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-194 + + Task 1: Digital Clock + Submitted by: Mohammad S Anwar + You are given time in the format hh:mm with one missing digit. + + Write a script to find the highest digit between 0-9 that makes it valid time. + +=cut + +is(digitalClock('?5:00'), 1, 'Test Case 1 Failed.'); +is(digitalClock('?3:00'), 2, 'Test Case 2 Failed.'); +is(digitalClock('1?:00'), 9, 'Test Case 3 Failed.'); +is(digitalClock('2?:00'), 3, 'Test Case 4 Failed.'); +is(digitalClock('12:?5'), 5, 'Test Case 5 Failed.'); +is(digitalClock('12:5?'), 9, 'Test Case 6 Failed.'); + +sub digitalClock { + my ($time) = @_; + + $time =~ m/\?/g; + my $missing_character_index = pos($time) - 1; + $time =~ s/\?/9/; + my ($hour, $minute) = split(/:/, $time); + + if ($missing_character_index < 2) { + while (int($hour) > 23) { + substr($hour, $missing_character_index, 1, substr($hour, $missing_character_index, 1) - 1); + } + + return(substr($hour, $missing_character_index, 1)) + } + else { + $missing_character_index -= 3; + while (int($minute) > 59) { + substr($minute, $missing_character_index, 1, substr($minute, $missing_character_index, 1) - 1); + } + + return(substr($minute, $missing_character_index, 1)) + } +} + +done_testing(); diff --git a/challenge-194/vamsi-meenavilli/perl/ch-2.pl b/challenge-194/vamsi-meenavilli/perl/ch-2.pl new file mode 100644 index 0000000000..505f34e7f3 --- /dev/null +++ b/challenge-194/vamsi-meenavilli/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use strict; +use warnings FATAL => 'all'; +use Test2::V0; +use List::MoreUtils qw(uniq); + +=pod + +=head1 AUTHORS + +Vamsi Meenavilli + +=head1 DESCRIPTION + + Week 194: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-194 + + Task 2: Frequency Equalizer + Submitted by: Mohammad S Anwar + You are given a string made of alphabetic characters only, a-z. + + Write a script to determine whether removing only one character can make the frequency of the remaining characters + the same. + +=cut + +is(frequencyEqualizer('abbc'), 1, 'Test Case 1 Failed.'); +is(frequencyEqualizer('xyzyyxz'), 1, 'Test Case 2 Failed.'); +is(frequencyEqualizer('xzxz'), 0, 'Test Case 3 Failed.'); + +sub frequencyEqualizer { + my ($string) = @_; + + my %frequency_character_map = (); + $frequency_character_map{$_} += 1 for (split(//, $string)); + my @unique_frequencies = uniq(values(%frequency_character_map)); + + return((scalar(@unique_frequencies) == 2 and abs($unique_frequencies[0] - $unique_frequencies[1]) == 1) ? 1 : 0); +} + +done_testing(); |
