diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-22 12:52:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-22 12:52:51 +0100 |
| commit | 95ff536e894a177444ac5cac0da499c9951f443f (patch) | |
| tree | 3ed0452da08231834d216cbf94f78a7445631e31 | |
| parent | b335afba876ef25a29b33491c40bb799259b739f (diff) | |
| parent | a1e3158274222306a52771654c65396d9cfc4788 (diff) | |
| download | perlweeklychallenge-club-95ff536e894a177444ac5cac0da499c9951f443f.tar.gz perlweeklychallenge-club-95ff536e894a177444ac5cac0da499c9951f443f.tar.bz2 perlweeklychallenge-club-95ff536e894a177444ac5cac0da499c9951f443f.zip | |
Merge pull request #12397 from simongreen-net/master
sgreen solutions to challenge 331
| -rw-r--r-- | challenge-331/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-331/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-331/sgreen/perl/ch-1.pl | 13 | ||||
| -rwxr-xr-x | challenge-331/sgreen/perl/ch-2.pl | 33 | ||||
| -rwxr-xr-x | challenge-331/sgreen/python/ch-1.py | 23 | ||||
| -rwxr-xr-x | challenge-331/sgreen/python/ch-2.py | 29 | ||||
| -rwxr-xr-x | challenge-331/sgreen/python/test.py | 23 |
7 files changed, 124 insertions, 2 deletions
diff --git a/challenge-331/sgreen/README.md b/challenge-331/sgreen/README.md index 08f6b8c2bb..e9aff07864 100644 --- a/challenge-331/sgreen/README.md +++ b/challenge-331/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 330 +# The Weekly Challenge 331 -Blog: [Clearly the Title](https://dev.to/simongreennet/weekly-challenge-clearly-the-title-4bom) +Blog: [The last word is my buddy](https://dev.to/simongreennet/weekly-challenge-the-last-word-is-my-buddy-13pm) diff --git a/challenge-331/sgreen/blog.txt b/challenge-331/sgreen/blog.txt new file mode 100644 index 0000000000..aed4eabb05 --- /dev/null +++ b/challenge-331/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-the-last-word-is-my-buddy-13pm
\ No newline at end of file diff --git a/challenge-331/sgreen/perl/ch-1.pl b/challenge-331/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..6299480320 --- /dev/null +++ b/challenge-331/sgreen/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($input_string) { + my ($word) = $input_string =~ /([a-z'-]*[a-z])[^a-z]*$/i; + say length($word // ''); +} + +main( $ARGV[0] ); diff --git a/challenge-331/sgreen/perl/ch-2.pl b/challenge-331/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..bb43e13152 --- /dev/null +++ b/challenge-331/sgreen/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use Algorithm::Combinatorics qw(combinations); + +sub main ( $source, $target ) { + # Strings must be of the same length + if ( length($source) != length($target) ) { + say 'false'; + return; + } + + my $iter = combinations( [ 0 .. length($source) - 1 ], 2 ); + while ( my $pair = $iter->next ) { + my ( $i, $j ) = @$pair; + # Swap characters at positions i and j in source + my $swapped_word = $source; + substr( $swapped_word, $i, 1, substr( $source, $j, 1 ) ); + substr( $swapped_word, $j, 1, substr( $source, $i, 1 ) ); + if ( $swapped_word eq $target ) { + say 'true'; + return; + } + } + + say 'false'; +} + +main( $ARGV[0], $ARGV[1] ); diff --git a/challenge-331/sgreen/python/ch-1.py b/challenge-331/sgreen/python/ch-1.py new file mode 100755 index 0000000000..272f25d770 --- /dev/null +++ b/challenge-331/sgreen/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def last_word(input_string: str) -> int: + # Find the last word in the input string and return its length + match = re.search( + r"([a-z'-]*[a-z])[^a-z]*$", + input_string.strip(), + re.IGNORECASE + ) + return len(match.group(1)) if match else 0 + + +def main(): + result = last_word(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-331/sgreen/python/ch-2.py b/challenge-331/sgreen/python/ch-2.py new file mode 100755 index 0000000000..dc63e17945 --- /dev/null +++ b/challenge-331/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + +from itertools import combinations + +def buddy_strings(source: str, target: str) -> bool: + # Strings must be of the same length + if len(source) != len(target): + return False + + for i, j in combinations(range(len(source)), 2): + # Swap characters at positions i and j in source + swapped = list(source) + swapped[i], swapped[j] = swapped[j], swapped[i] + # Check if the swapped string matches the target + if ''.join(swapped) == target: + return True + + return False + + +def main(): + result = buddy_strings(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-331/sgreen/python/test.py b/challenge-331/sgreen/python/test.py new file mode 100755 index 0000000000..636ba07a77 --- /dev/null +++ b/challenge-331/sgreen/python/test.py @@ -0,0 +1,23 @@ +#!/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.last_word("The Weekly Challenge"), 9) + self.assertEqual(ch_1.last_word("The Weekly Challenge!"), 9) + self.assertEqual(ch_1.last_word(" Hello World "), 5) + self.assertEqual(ch_1.last_word("Let's"), 5) + self.assertEqual(ch_1.last_word("!!!"), 0) + + def test_ch_2(self): + self.assertTrue(ch_2.buddy_strings("nice", "ncie")) + self.assertFalse(ch_2.buddy_strings("love", "love")) + self.assertTrue(ch_2.buddy_strings("feed", "feed")) + + +if __name__ == '__main__': + unittest.main() |
