aboutsummaryrefslogtreecommitdiff
path: root/challenge-289
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2024-10-07 00:42:33 +1100
committer冯昶 <fengchang@novel-supertv.com>2024-10-08 09:56:18 +0800
commite30d3d5542f421c3eebfb6ed94c7b12edc434f3e (patch)
treeee58887c420af5c59e5e8d615bf78dc4a964195c /challenge-289
parenta593a8d4edd9d374666ad28fbfcccb1ec3411dd8 (diff)
downloadperlweeklychallenge-club-e30d3d5542f421c3eebfb6ed94c7b12edc434f3e.tar.gz
perlweeklychallenge-club-e30d3d5542f421c3eebfb6ed94c7b12edc434f3e.tar.bz2
perlweeklychallenge-club-e30d3d5542f421c3eebfb6ed94c7b12edc434f3e.zip
sgreen solutions to challenge 289
Diffstat (limited to 'challenge-289')
-rw-r--r--challenge-289/sgreen/README.md2
-rwxr-xr-xchallenge-289/sgreen/perl/ch-1.pl23
-rwxr-xr-xchallenge-289/sgreen/perl/ch-2.pl36
-rwxr-xr-xchallenge-289/sgreen/python/ch-1.py26
-rwxr-xr-xchallenge-289/sgreen/python/ch-2.py31
-rwxr-xr-xchallenge-289/sgreen/python/test.py27
6 files changed, 144 insertions, 1 deletions
diff --git a/challenge-289/sgreen/README.md b/challenge-289/sgreen/README.md
index f83d2827dc..0c36b1adfb 100644
--- a/challenge-289/sgreen/README.md
+++ b/challenge-289/sgreen/README.md
@@ -1 +1 @@
-# The Weekly Challenge 288
+# The Weekly Challenge 289
diff --git a/challenge-289/sgreen/perl/ch-1.pl b/challenge-289/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..51e497d214
--- /dev/null
+++ b/challenge-289/sgreen/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'uniq';
+
+sub main (@ints) {
+ # Sort the list, removing duplicates
+ my @sorted_ints = sort { $b <=> $a } uniq(@ints);
+
+ if ( $#sorted_ints < 2 ) {
+ # If there are less than 3 elements, return the largest
+ say $sorted_ints[0];
+ }
+ else {
+ # Otherwise, return the third largest
+ say $sorted_ints[2];
+ }
+}
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-289/sgreen/perl/ch-2.pl b/challenge-289/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..857cf3db92
--- /dev/null
+++ b/challenge-289/sgreen/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'shuffle';
+
+sub shuffle_word($word) {
+ # Shuffle the middle characters of the word
+ my @middle = split //, substr( $word, 1, -1 );
+ return
+ substr( $word, 0, 1 ) . join( '', shuffle @middle ) . substr( $word, -1 );
+}
+
+sub main ($str) {
+ # Split the string into a list of words
+ my @word_list = split( /([a-z]+)/i, $str );
+ my $jumbled_phrase = '';
+
+ foreach my $word (@word_list) {
+ if ( $word =~ /[a-z]{4,}/ ) {
+ # If the word has more than 3 letters, jumble the middle characters
+ $jumbled_phrase .= shuffle_word($word);
+ }
+ else {
+ # Otherwise, add the word as is (punctuation falls into this category)
+ $jumbled_phrase .= $word;
+ }
+ }
+
+ say $jumbled_phrase;
+}
+
+main( $ARGV[0] ); \ No newline at end of file
diff --git a/challenge-289/sgreen/python/ch-1.py b/challenge-289/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..7cdb843286
--- /dev/null
+++ b/challenge-289/sgreen/python/ch-1.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def third_maximum(ints: list) -> str:
+ # Sort the list, removing duplicates
+ sorted_ints = sorted(set(ints))
+
+ if len(sorted_ints) < 3:
+ # If there are less than 3 elements, return the largest
+ return sorted_ints[-1]
+ else:
+ # Otherwise, return the third largest
+ return sorted_ints[-3]
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = third_maximum(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-289/sgreen/python/ch-2.py b/challenge-289/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..113fbab357
--- /dev/null
+++ b/challenge-289/sgreen/python/ch-2.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+import random
+import re
+import sys
+
+
+def jumbled_letters(s: str) -> str:
+ # Split the string into a list of words
+ word_list = re.split('([a-z]+)', s, flags=re.IGNORECASE)
+ jumbled_phrase = ''
+
+ for word in word_list:
+ if re.search('[a-z]{4,}', word, flags=re.IGNORECASE):
+ # If the word has more than 3 letters, jumble the middle characters
+ jumbled_phrase += word[0] + \
+ ''.join(random.sample(word[1:-1], len(word)-2)) + word[-1]
+ else:
+ # Otherwise, add the word as is (punctuation falls into this category)
+ jumbled_phrase += word
+
+ return jumbled_phrase
+
+
+def main():
+ result = jumbled_letters(sys.argv[1])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-289/sgreen/python/test.py b/challenge-289/sgreen/python/test.py
new file mode 100755
index 0000000000..cc212cf499
--- /dev/null
+++ b/challenge-289/sgreen/python/test.py
@@ -0,0 +1,27 @@
+#!/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.third_maximum([4, 6, 5, 1]), 4)
+ self.assertEqual(ch_1.third_maximum([4, 5]), 5)
+ self.assertEqual(ch_1.third_maximum([1, 2, 2, 3]), 1)
+
+
+ def test_ch_2(self):
+ word1 = 'The quick brown fox jumps over the lazy dog.'
+ regex1 = r'^The q[uic]{3}k b[row]{3}n fox j[ump]{3}s o[ve]{2}r the l[az]{2}y dog\.$'
+
+ word2 = 'Perl is very much alive!!'
+ regex2 = '^P[er]{2}l is v[er]{2}y m[uc]{2}h a[liv]{3}e!!$'
+
+ self.assertRegex(ch_2.jumbled_letters(word1), regex1)
+ self.assertRegex(ch_2.jumbled_letters(word2), regex2)
+
+
+if __name__ == '__main__':
+ unittest.main()