diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-06 23:06:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-06 23:06:42 +0100 |
| commit | 259e0413ae2d77dd21017d6ed5f1fc8bfaf98937 (patch) | |
| tree | a08774ba3411e016a6d39046ebd0f3de1807726c | |
| parent | 8d230dd8f7bc2fbf026029fab1c38b7179f7a8d4 (diff) | |
| parent | 517928ee2af6c019d908776d7a2b4fe4107b529d (diff) | |
| download | perlweeklychallenge-club-259e0413ae2d77dd21017d6ed5f1fc8bfaf98937.tar.gz perlweeklychallenge-club-259e0413ae2d77dd21017d6ed5f1fc8bfaf98937.tar.bz2 perlweeklychallenge-club-259e0413ae2d77dd21017d6ed5f1fc8bfaf98937.zip | |
Merge pull request #12292 from simongreen-net/master
sgreen solutions to challenge 328
| -rw-r--r-- | challenge-328/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-328/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-328/sgreen/perl/ch-1.pl | 40 | ||||
| -rwxr-xr-x | challenge-328/sgreen/perl/ch-2.pl | 35 | ||||
| -rwxr-xr-x | challenge-328/sgreen/python/ch-1.py | 37 | ||||
| -rwxr-xr-x | challenge-328/sgreen/python/ch-2.py | 29 | ||||
| -rwxr-xr-x | challenge-328/sgreen/python/test.py | 22 |
7 files changed, 166 insertions, 2 deletions
diff --git a/challenge-328/sgreen/README.md b/challenge-328/sgreen/README.md index 81e0c1c823..57261d9d9b 100644 --- a/challenge-328/sgreen/README.md +++ b/challenge-328/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 327 +# The Weekly Challenge 328 -No blog this week. +Blog: [A good question](https://dev.to/simongreennet/weekly-challenge-a-good-question-3e3p) diff --git a/challenge-328/sgreen/blog.txt b/challenge-328/sgreen/blog.txt new file mode 100644 index 0000000000..ef9d8115c6 --- /dev/null +++ b/challenge-328/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-a-good-question-3e3p
\ No newline at end of file diff --git a/challenge-328/sgreen/perl/ch-1.pl b/challenge-328/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..a4a3383b9a --- /dev/null +++ b/challenge-328/sgreen/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($input_string) { + my $solution = $input_string; + foreach my $idx ( 0 .. length($solution) - 1 ) { + my $char = substr( $solution, $idx, 1 ); + if ( $char ne '?' ) { + next; + } + + # Get the surrounding characters + my %letters = (); + if ( $idx > 0 ) { + $letters{ substr( $solution, $idx - 1, 1 ) } = 1; + } + if ( $idx < length($solution) - 1 ) { + $letters{ substr( $solution, $idx + 1, 1 ) } = 1; + } + + # Replace '?' with 'a', 'b', or 'c' based on surrounding characters + if ( not exists $letters{'a'} ) { + substr( $solution, $idx, 1 ) = 'a'; + } + elsif ( not exists $letters{'b'} ) { + substr( $solution, $idx, 1 ) = 'b'; + } + else { + substr( $solution, $idx, 1 ) = 'c'; + } + } + + say '"', $solution, '"'; +} + +main( $ARGV[0] ); diff --git a/challenge-328/sgreen/perl/ch-2.pl b/challenge-328/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..2722a89443 --- /dev/null +++ b/challenge-328/sgreen/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($input_string) { + my $solution = $input_string; + O: while (1) { + foreach my $idx ( 0 .. length($solution) - 2 ) { + my $char = substr( $solution, $idx, 1 ); + + # If the nex two letters are the same but different case, remove them + if ( + ( + $char eq uc($char) + and substr( $solution, $idx + 1, 1 ) eq lc($char) + ) + or ( $char eq lc($char) + and substr( $solution, $idx + 1, 1 ) eq uc($char) ) + ) + { + substr( $solution, $idx, 2 ) = ''; + next O; + } + } + + last; + } + + say '"', $solution, '"'; +} + +main( $ARGV[0] ); diff --git a/challenge-328/sgreen/python/ch-1.py b/challenge-328/sgreen/python/ch-1.py new file mode 100755 index 0000000000..16e539b9b1 --- /dev/null +++ b/challenge-328/sgreen/python/ch-1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +import sys + + +def replace_all_questions(input_string: str) -> str: + solution = '' + for idx, char in enumerate(input_string): + if char != '?': + solution += char + continue + + # Get the surrounding characters + letters = [] + if idx > 0: + letters.append(input_string[idx - 1]) + if idx < len(input_string) - 1: + letters.append(input_string[idx + 1]) + + # Replace '?' with 'a', 'b', or 'c' based on surrounding characters + if 'a' not in letters: + solution += 'a' + elif 'b' not in letters: + solution += 'b' + else: + solution += 'c' + + return solution + + +def main(): + result = replace_all_questions(sys.argv[1]) + print('"' + result + '"') + + +if __name__ == '__main__': + main() diff --git a/challenge-328/sgreen/python/ch-2.py b/challenge-328/sgreen/python/ch-2.py new file mode 100755 index 0000000000..dff805c0b4 --- /dev/null +++ b/challenge-328/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + + +def good_string(input_string: str) -> str: + solution = input_string + while True: + for idx in range(0, len(solution)-1): + char = solution[idx] + # If the next two letters are the same but different case, remove them + if ((char.isupper() and solution[idx + 1] == char.lower()) or + (char.islower() and solution[idx + 1] == char.upper())): + solution = solution[:idx] + solution[idx + 2:] + break + else: + # No pairs were removed + break + + return solution + + +def main(): + result = good_string(sys.argv[1]) + print('"' + result + '"') + + +if __name__ == '__main__': + main() diff --git a/challenge-328/sgreen/python/test.py b/challenge-328/sgreen/python/test.py new file mode 100755 index 0000000000..3e9e91f5d9 --- /dev/null +++ b/challenge-328/sgreen/python/test.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.replace_all_questions('a?z'), 'abz') + self.assertEqual(ch_1.replace_all_questions('pe?k'), 'peak') + self.assertEqual(ch_1.replace_all_questions('gra?te'), 'grabte') + self.assertEqual(ch_1.replace_all_questions('gra?be'), 'gracbe') + + def test_ch_2(self): + self.assertEqual(ch_2.good_string('WeEeekly'), 'Weekly') + self.assertEqual(ch_2.good_string('abBAdD'), '') + self.assertEqual(ch_2.good_string('abc'), 'abc') + + +if __name__ == '__main__': + unittest.main() |
