aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2025-07-20 22:13:31 +1000
committerSimon Green <mail@simon.green>2025-07-20 22:13:31 +1000
commit632d4c4c514525f8782e52e7f4f78fe5dfeef436 (patch)
treef4ea97b2284568082d3df790af8d634651894a2c
parent6345c73edaafe1c1252e99cf8991c8fc27890445 (diff)
downloadperlweeklychallenge-club-632d4c4c514525f8782e52e7f4f78fe5dfeef436.tar.gz
perlweeklychallenge-club-632d4c4c514525f8782e52e7f4f78fe5dfeef436.tar.bz2
perlweeklychallenge-club-632d4c4c514525f8782e52e7f4f78fe5dfeef436.zip
sgreen solutions to challenge 330
-rw-r--r--challenge-330/sgreen/README.md4
-rw-r--r--challenge-330/sgreen/blog.txt1
-rwxr-xr-xchallenge-330/sgreen/perl/ch-1.pl20
-rwxr-xr-xchallenge-330/sgreen/perl/ch-2.pl19
-rwxr-xr-xchallenge-330/sgreen/python/ch-1.py34
-rwxr-xr-xchallenge-330/sgreen/python/ch-2.py33
-rwxr-xr-xchallenge-330/sgreen/python/test.py21
7 files changed, 130 insertions, 2 deletions
diff --git a/challenge-330/sgreen/README.md b/challenge-330/sgreen/README.md
index f06345033b..08f6b8c2bb 100644
--- a/challenge-330/sgreen/README.md
+++ b/challenge-330/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 329
+# The Weekly Challenge 330
-No blog this week, sorry.
+Blog: [Clearly the Title](https://dev.to/simongreennet/weekly-challenge-clearly-the-title-4bom)
diff --git a/challenge-330/sgreen/blog.txt b/challenge-330/sgreen/blog.txt
new file mode 100644
index 0000000000..e556dc5ee6
--- /dev/null
+++ b/challenge-330/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-clearly-the-title-4bom \ No newline at end of file
diff --git a/challenge-330/sgreen/perl/ch-1.pl b/challenge-330/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..2769901015
--- /dev/null
+++ b/challenge-330/sgreen/perl/ch-1.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main ($input_string) {
+ my $solution = $input_string;
+
+ my $changes = 1;
+ while ($changes) {
+ # See if there are any letter / digit pairs to remove
+ $changes = ( $solution =~ s/[a-z][0-9]// );
+ }
+
+ say '"', $solution, '"';
+}
+
+main( $ARGV[0] );
diff --git a/challenge-330/sgreen/perl/ch-2.pl b/challenge-330/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..71dcf15609
--- /dev/null
+++ b/challenge-330/sgreen/perl/ch-2.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main ($input_string) {
+ # Convert the input string to lowercase, split it into words
+ my @words = split / /, lc $input_string;
+
+ # Capitalize the first letter of each word longer than 2 characters
+ my @new_words = map { length($_) > 2 ? ucfirst($_) : $_ } @words;
+
+ # Join the words back into a single string with spaces
+ say '"', join( ' ', @new_words ), '"';
+}
+
+main( $ARGV[0] );
diff --git a/challenge-330/sgreen/python/ch-1.py b/challenge-330/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..0f8f6ddc2e
--- /dev/null
+++ b/challenge-330/sgreen/python/ch-1.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+def clear_digits(input_string) -> str:
+ """
+ Remove all letter/digit pairs from the input string until no more pairs
+ can be removed.
+
+ :param input_string: The string from which to remove letter/digit pairs.
+ :return: The modified string with all letter/digit pairs removed.
+ """
+ solution = input_string
+
+ while True:
+ # See if there are any letter / digit pairs to remove
+ new_string = re.sub(r'[a-z][0-9]', '', solution)
+ if new_string == solution:
+ # No more pairs to remove, rerturn the result
+ return solution
+
+ # Update the solution with the new string and run the loop again
+ solution = new_string
+
+
+def main():
+ result = clear_digits(sys.argv[1])
+ print('"'+result+'"')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-330/sgreen/python/ch-2.py b/challenge-330/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..68050246b8
--- /dev/null
+++ b/challenge-330/sgreen/python/ch-2.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def title_capital(input_string: str) -> str:
+ """
+ Convert the input string to title case, capitalizing the first letter of
+ each word longer than 2 characters, and converting the rest to lowercase.
+
+ :param input_string: The string to convert to title case.
+ :return: The modified string in title case.
+ """
+ # Convert the input string to lowercase, split it into words
+ words = input_string.lower().split()
+
+ # Capitalize the first letter of each word longer than 2 characters
+ new_words = [
+ word.capitalize() if len(word) > 2 else word
+ for word in words
+ ]
+
+ # Join the words back into a single string with spaces
+ return ' '.join(new_words)
+
+
+def main():
+ result = title_capital(sys.argv[1])
+ print('"' + result+'"')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-330/sgreen/python/test.py b/challenge-330/sgreen/python/test.py
new file mode 100755
index 0000000000..89980aa8ca
--- /dev/null
+++ b/challenge-330/sgreen/python/test.py
@@ -0,0 +1,21 @@
+#!/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.clear_digits("cab12"), "c")
+ self.assertEqual(ch_1.clear_digits("xy99"), "")
+ self.assertEqual(ch_1.clear_digits("pa1erl"), "perl")
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.title_capital("PERL IS gREAT"), "Perl is Great")
+ self.assertEqual(ch_2.title_capital("THE weekly challenge"), "The Weekly Challenge")
+ self.assertEqual(ch_2.title_capital("YoU ARE A stAR"), "You Are a Star")
+
+
+if __name__ == '__main__':
+ unittest.main()