diff options
| author | Simon Green <mail@simon.green> | 2025-11-16 23:13:00 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-11-16 23:13:00 +1000 |
| commit | 9e07c2fbb9f9eae0a3ea4a904281372e8cd996b7 (patch) | |
| tree | 5e34e38b2e0eedd4a615f76fe9a1fa5151a98d41 | |
| parent | d945cc3d48b1284d49aba765341514707f7bee50 (diff) | |
| download | perlweeklychallenge-club-9e07c2fbb9f9eae0a3ea4a904281372e8cd996b7.tar.gz perlweeklychallenge-club-9e07c2fbb9f9eae0a3ea4a904281372e8cd996b7.tar.bz2 perlweeklychallenge-club-9e07c2fbb9f9eae0a3ea4a904281372e8cd996b7.zip | |
sgreen solutions to challenge 347
| -rw-r--r-- | challenge-347/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-347/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-347/sgreen/perl/ch-1.pl | 53 | ||||
| -rwxr-xr-x | challenge-347/sgreen/perl/ch-2.pl | 25 | ||||
| -rwxr-xr-x | challenge-347/sgreen/python/ch-1.py | 51 | ||||
| -rwxr-xr-x | challenge-347/sgreen/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-347/sgreen/python/test.py | 25 |
7 files changed, 184 insertions, 2 deletions
diff --git a/challenge-347/sgreen/README.md b/challenge-347/sgreen/README.md index 4cc0150d80..ee0870fa09 100644 --- a/challenge-347/sgreen/README.md +++ b/challenge-347/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 346 +# The Weekly Challenge 347 -Blog: [Longest Expression](https://dev.to/simongreennet/weekly-challenge-longest-expression-b8b) +Blog: [The one about formatting](https://dev.to/simongreennet/weekly-challenge-the-one-about-formatting-kfp) diff --git a/challenge-347/sgreen/blog.txt b/challenge-347/sgreen/blog.txt new file mode 100644 index 0000000000..5ab7363305 --- /dev/null +++ b/challenge-347/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-the-one-about-formatting-kfp
\ No newline at end of file diff --git a/challenge-347/sgreen/perl/ch-1.pl b/challenge-347/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..15e3c26a54 --- /dev/null +++ b/challenge-347/sgreen/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::MoreUtils 'first_index'; + +sub main ($input_string) { + my @DAYS = ( + "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", + "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", + "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", + "25th", "26th", "27th", "28th", "29th", "30th", "31st" + ); + + my @MONTHS = ( + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + ); + + my @YEARS = ( 1900 .. 2100 ); + + my @fields = ( + [ "day of month", \@DAYS, 1 ], + [ "month", \@MONTHS, 1 ], + [ "year", \@YEARS, 1900 ], + ); + + my @input_array = split /\s+/, $input_string; + my @output_array = (); + + if ( $#input_array != 2 ) { + die "Input must contain day, month, and year\n"; + } + + foreach my $i ( 0, 1, 2 ) { + my ( $name, $values, $offset ) = @{ $fields[$i] }; + my $value = $input_array[$i]; + + my $idx = first_index { $_ eq $value } @$values; + if ( $idx == -1 ) { + die "Invalid $name: $value\n"; + } + + push @output_array, sprintf( "%02d", $idx + $offset ); + } + + say join( "-", reverse @output_array ); +} + +main(@ARGV); diff --git a/challenge-347/sgreen/perl/ch-2.pl b/challenge-347/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..39d47faa22 --- /dev/null +++ b/challenge-347/sgreen/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'min'; + +sub main ($input_string) { + # Strip all non-digit characters + $input_string =~ s/\D//g; + + my @parts = (); + while ($input_string) { + # Decide length of next part + my $l = + length($input_string) == 4 ? 2 : min( 3, length($input_string) ); + push @parts, substr( $input_string, 0, $l, "" ); + } + + say join( "-", @parts ); +} + +main( $ARGV[0] ); diff --git a/challenge-347/sgreen/python/ch-1.py b/challenge-347/sgreen/python/ch-1.py new file mode 100755 index 0000000000..a0e124c070 --- /dev/null +++ b/challenge-347/sgreen/python/ch-1.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import re +import sys + +DAYS = [ + "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", + "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", + "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", + "29th", "30th", "31st" +] + +MONTHS = [ + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", + "Nov", "Dec" +] + +YEARS = list(map(str, range(1900, 2101))) + + +def format_date(input_string: str) -> str: + fields = [ + ("day of month", DAYS, 1), + ("month", MONTHS, 1), + ("year", YEARS, 1900) + ] + + input_list = input_string.split() + output_list = [] + + if len(input_list) != 3: + raise ValueError("Input must contain day, month, and year") + + for i in (range(3)): + name, values, offset = fields[i] + value = input_list[i] + if value not in values: + raise ValueError(f"Invalid {name}: {value}") + index = values.index(value) + offset + output_list.append(f"{index:02d}") + + return "-".join(reversed(output_list)) + + +def main(): + result = format_date(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-347/sgreen/python/ch-2.py b/challenge-347/sgreen/python/ch-2.py new file mode 100755 index 0000000000..776a289e1d --- /dev/null +++ b/challenge-347/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def format_phone(input_string: str) -> str: + # Strip all non-digit characters + input_string = re.sub(r'\D', '', input_string) + + parts = [] + while input_string: + # Decide length of next part + l = 2 if len(input_string) == 4 else min(3, len(input_string)) + parts.append(input_string[:l]) + input_string = input_string[l:] + + return '-'.join(parts) + + +def main(): + result = format_phone(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-347/sgreen/python/test.py b/challenge-347/sgreen/python/test.py new file mode 100755 index 0000000000..369b8579bc --- /dev/null +++ b/challenge-347/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.format_date('1st Jan 2025'), '2025-01-01') + self.assertEqual(ch_1.format_date('22nd Feb 2025'), '2025-02-22') + self.assertEqual(ch_1.format_date('15th Apr 2025'), '2025-04-15') + self.assertEqual(ch_1.format_date('23rd Oct 2025'), '2025-10-23') + self.assertEqual(ch_1.format_date('31st Dec 2025'), '2025-12-31') + + def test_ch_2(self): + self.assertEqual(ch_2.format_phone('1-23-45-6'), '123-456') + self.assertEqual(ch_2.format_phone('1234'), '12-34') + self.assertEqual(ch_2.format_phone('12 345-6789'), '123-456-789') + self.assertEqual(ch_2.format_phone('123 4567'), '123-45-67') + self.assertEqual(ch_2.format_phone('123 456-78'), '123-456-78') + + +if __name__ == '__main__': + unittest.main() |
