diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-28 11:33:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-28 11:33:15 +0100 |
| commit | 5896d52e3bf99f5a035d166819178231806516c9 (patch) | |
| tree | b7eb21e60a36843786aa919ad81f3809282c8827 | |
| parent | ad9ad78bc84642fb71e7302e49ea44e953273d79 (diff) | |
| parent | 2b5be1092b13e371b57966ee6ebb8505e5e1599c (diff) | |
| download | perlweeklychallenge-club-5896d52e3bf99f5a035d166819178231806516c9.tar.gz perlweeklychallenge-club-5896d52e3bf99f5a035d166819178231806516c9.tar.bz2 perlweeklychallenge-club-5896d52e3bf99f5a035d166819178231806516c9.zip | |
Merge pull request #12744 from simongreen-net/master
sgreen solutions to challenge 340
| -rw-r--r-- | challenge-340/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-340/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-340/sgreen/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-340/sgreen/perl/ch-2.pl | 26 | ||||
| -rwxr-xr-x | challenge-340/sgreen/python/ch-1.py | 32 | ||||
| -rwxr-xr-x | challenge-340/sgreen/python/ch-2.py | 38 | ||||
| -rwxr-xr-x | challenge-340/sgreen/python/test.py | 25 |
7 files changed, 141 insertions, 2 deletions
diff --git a/challenge-340/sgreen/README.md b/challenge-340/sgreen/README.md index a543fc1e96..6fbbc69ddc 100644 --- a/challenge-340/sgreen/README.md +++ b/challenge-340/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 339 +# The Weekly Challenge 340 -Blog: [Maximum climb](https://dev.to/simongreennet/weekly-challenge-maximum-climb-28i3) +Blog: [Ascending Regex to remove the Duplicates](https://dev.to/simongreennet/weekly-challenge-ascending-regex-to-remove-the-duplicates-2bd) diff --git a/challenge-340/sgreen/blog.txt b/challenge-340/sgreen/blog.txt new file mode 100644 index 0000000000..35c5874e5a --- /dev/null +++ b/challenge-340/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-ascending-regex-to-remove-the-duplicates-2bd
\ No newline at end of file diff --git a/challenge-340/sgreen/perl/ch-1.pl b/challenge-340/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..53b699f860 --- /dev/null +++ b/challenge-340/sgreen/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($input_string) { + my $changes = 1; + while ($changes) { + $changes = ($input_string =~ s/(.)\1+//g); + } + + say "'$input_string'"; +} + +main($ARGV[0]);
\ No newline at end of file diff --git a/challenge-340/sgreen/perl/ch-2.pl b/challenge-340/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..90ee6b3404 --- /dev/null +++ b/challenge-340/sgreen/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($input_string) { + # Extract numbers from the input string + my @number_array = grep { /^\d+$/ } split /\s+/, $input_string; + + if ( $#number_array == -1 ) { + die "No numbers found in the input string.\n"; + } + + # Check if the numbers are in ascending order + foreach my $i ( 1 .. $#number_array ) { + if ( $number_array[ $i - 1 ] >= $number_array[$i] ) { + say "false"; + return; + } + } + say "true"; +} + +main(@ARGV); diff --git a/challenge-340/sgreen/python/ch-1.py b/challenge-340/sgreen/python/ch-1.py new file mode 100755 index 0000000000..3ce7dcc43c --- /dev/null +++ b/challenge-340/sgreen/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def duplicate_removals(input_string: str) -> str: + """ + Remove all adjacent duplicate characters from the input string. + + Args: + input_string (str): The input string to process. + + Returns: + str: The processed string with adjacent duplicates removed. + """ + + while True: + # Keep removing adjacent duplicates until none remain + new_string = re.sub(r'(.)\1', '', input_string) + if new_string == input_string: + return new_string + input_string = new_string + + +def main(): + result = duplicate_removals(sys.argv[1]) + print("'" + result + "'") + + +if __name__ == '__main__': + main() diff --git a/challenge-340/sgreen/python/ch-2.py b/challenge-340/sgreen/python/ch-2.py new file mode 100755 index 0000000000..e3027aa4b4 --- /dev/null +++ b/challenge-340/sgreen/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def ascending_numbers(input_string: str) -> bool: + """ + Check if numbers in the input string are in ascending order. + + Args: + input_string (str): The input string containing numbers. + + Returns: + bool: True if numbers are in ascending order, False otherwise. + """ + + # Extract numbers from the input string + number_list = [int(n) for n in input_string.split() if re.match(r'^\d+$', n)] + + if not number_list: + raise ValueError("No numbers found in the input string.") + + # Check for ascending sequence + for i in range(1, len(number_list)): + if number_list[i-1] >= number_list[i]: + return False + + return True + + +def main(): + result = ascending_numbers(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-340/sgreen/python/test.py b/challenge-340/sgreen/python/test.py new file mode 100755 index 0000000000..60267a3994 --- /dev/null +++ b/challenge-340/sgreen/python/test.py @@ -0,0 +1,25 @@ +#!/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.duplicate_removals('abbaca'), 'ca') + self.assertEqual(ch_1.duplicate_removals('azxxzy'), 'ay') + self.assertEqual(ch_1.duplicate_removals('aaaaaaaa'), '') + self.assertEqual(ch_1.duplicate_removals('aabccba'), 'a') + self.assertEqual(ch_1.duplicate_removals('abcddcba'), '') + + def test_ch_2(self): + self.assertTrue(ch_2.ascending_numbers('The cat has 3 kittens 7 toys 10 beds')) + self.assertFalse(ch_2.ascending_numbers('Alice bought 5 apples 2 oranges 9 bananas')) + self.assertTrue(ch_2.ascending_numbers('I ran 1 mile 2 days 3 weeks 4 months')) + self.assertFalse(ch_2.ascending_numbers('Bob has 10 cars 10 bikes')) + self.assertTrue(ch_2.ascending_numbers('Zero is 0 one is 1 two is 2')) + + +if __name__ == '__main__': + unittest.main() |
