aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-06 17:19:13 +0000
committerGitHub <noreply@github.com>2022-12-06 17:19:13 +0000
commit863aa06d6ffa18c4a7655ff8306ecab5b0d1a19b (patch)
treea4ab885cf9f40a8e377ac633a63a5bec8965fa52
parent8eb28ac7c89b06128d3288337c9cb080648cc13f (diff)
parent9b4fc20da72bbfc067c8f4579c5eb23824c6d691 (diff)
downloadperlweeklychallenge-club-863aa06d6ffa18c4a7655ff8306ecab5b0d1a19b.tar.gz
perlweeklychallenge-club-863aa06d6ffa18c4a7655ff8306ecab5b0d1a19b.tar.bz2
perlweeklychallenge-club-863aa06d6ffa18c4a7655ff8306ecab5b0d1a19b.zip
Merge pull request #7213 from vamsi-meenavilli/vamsi-challenge-194
Vamsi's Weekly Challenge 194 python solutions
-rw-r--r--challenge-194/vamsi-meenavilli/perl/ch-1.pl57
-rw-r--r--challenge-194/vamsi-meenavilli/perl/ch-2.pl42
-rw-r--r--challenge-194/vamsi-meenavilli/python/ch-1.py51
-rw-r--r--challenge-194/vamsi-meenavilli/python/ch-2.py41
4 files changed, 191 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();
diff --git a/challenge-194/vamsi-meenavilli/python/ch-1.py b/challenge-194/vamsi-meenavilli/python/ch-1.py
new file mode 100644
index 0000000000..7de3d3752b
--- /dev/null
+++ b/challenge-194/vamsi-meenavilli/python/ch-1.py
@@ -0,0 +1,51 @@
+import re
+import unittest
+
+"""
+ 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.
+"""
+
+
+class DigitalClock:
+ @staticmethod
+ def digital_clock(time: str) -> int:
+ missing_character_index = re.search("\\?", time).start()
+ time = re.sub("\\?", "9", time)
+
+ if missing_character_index < 3:
+ hours = list(map(int, time.split(":")[0]))
+
+ while int(''.join(map(str, hours))) > 23:
+ hours[missing_character_index] -= 1
+
+ return hours[missing_character_index]
+ else:
+ missing_character_index -= 3
+ minutes = list(map(int, time.split(":")[1]))
+
+ while int(''.join(map(str, minutes))) > 59:
+ minutes[missing_character_index] -= 1
+
+ return minutes[missing_character_index]
+
+
+class DigitalClockTestCases(unittest.TestCase):
+ def test_digital_clock(self):
+ self.assertEqual(1, DigitalClock.digital_clock("?5:00"), 'Test case 1 Failed.')
+ self.assertEqual(2, DigitalClock.digital_clock("?3:00"), 'Test case 2 Failed.')
+ self.assertEqual(9, DigitalClock.digital_clock("1?:00"), 'Test case 3 Failed.')
+ self.assertEqual(3, DigitalClock.digital_clock("2?:00"), 'Test case 4 Failed.')
+ self.assertEqual(5, DigitalClock.digital_clock("12:?5"), 'Test case 5 Failed.')
+ self.assertEqual(9, DigitalClock.digital_clock("12:5?"), 'Test case 6 Failed.')
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-194/vamsi-meenavilli/python/ch-2.py b/challenge-194/vamsi-meenavilli/python/ch-2.py
new file mode 100644
index 0000000000..d318dc3e51
--- /dev/null
+++ b/challenge-194/vamsi-meenavilli/python/ch-2.py
@@ -0,0 +1,41 @@
+import re
+import unittest
+
+"""
+ 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.
+"""
+
+
+class FrequencyEqualizer:
+ @staticmethod
+ def frequency_equalizer(string: str) -> bool:
+ character_frequency_map = dict()
+
+ for character in string:
+ if character in character_frequency_map:
+ character_frequency_map[character] += 1
+ else:
+ character_frequency_map[character] = 1
+
+ unique_frequencies = list(set(character_frequency_map.values()))
+
+ return True if len(unique_frequencies) == 2 and abs(unique_frequencies[0] - unique_frequencies[1]) == 1 else False
+
+
+class FrequencyEqualizerTestCases(unittest.TestCase):
+ def test_digital_clock(self):
+ self.assertTrue(FrequencyEqualizer.frequency_equalizer("abbc"), 'Test case 1 Failed.')
+ self.assertTrue(FrequencyEqualizer.frequency_equalizer("xyzyyxz"), 'Test case 2 Failed.')
+ self.assertFalse(FrequencyEqualizer.frequency_equalizer("xzxz"), 'Test case 3 Failed.')
+
+
+if __name__ == '__main__':
+ unittest.main()