diff options
| author | Yitzchak Scott-Thoennes <sthoenna@gmail.com> | 2025-11-16 18:55:03 -0500 |
|---|---|---|
| committer | Yitzchak Scott-Thoennes <sthoenna@gmail.com> | 2025-11-16 18:55:03 -0500 |
| commit | fda37e11166cd5d90e18f28a55d3759143e4ac35 (patch) | |
| tree | 9fca45ac7882422d478fcf2409ef9b0bc8433655 /challenge-346 | |
| parent | 83c02309afd45cdbfce02a54ee39361fce28c592 (diff) | |
| download | perlweeklychallenge-club-fda37e11166cd5d90e18f28a55d3759143e4ac35.tar.gz perlweeklychallenge-club-fda37e11166cd5d90e18f28a55d3759143e4ac35.tar.bz2 perlweeklychallenge-club-fda37e11166cd5d90e18f28a55d3759143e4ac35.zip | |
weekly challenge 346 perl, python, and go solutions by ysth
Diffstat (limited to 'challenge-346')
| -rw-r--r-- | challenge-346/ysth/go/ch-1.go | 61 | ||||
| -rw-r--r-- | challenge-346/ysth/go/ch-2.go | 40 | ||||
| -rw-r--r-- | challenge-346/ysth/perl/README.md | 1 | ||||
| -rw-r--r-- | challenge-346/ysth/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-346/ysth/perl/ch-2.pl | 21 | ||||
| -rw-r--r-- | challenge-346/ysth/python/ch-1.py | 47 | ||||
| -rw-r--r-- | challenge-346/ysth/python/ch-2.py | 31 |
7 files changed, 237 insertions, 0 deletions
diff --git a/challenge-346/ysth/go/ch-1.go b/challenge-346/ysth/go/ch-1.go new file mode 100644 index 0000000000..9e7b4a7dbf --- /dev/null +++ b/challenge-346/ysth/go/ch-1.go @@ -0,0 +1,61 @@ +package main + +import ( + "errors" + "fmt" + "strconv" + "strings" + "time" + "github.com/dustin/go-humanize" + run "github.com/ysth/runweeklychallenge-go" +) + +var mon = make(map[string]int) +var day = make(map[string]int) +func init() { + for m := 1; m < 13; m++ { + mon[ time.Date(0,time.Month(m),1,0,0,0,0,time.UTC).Format("Jan") ] = m + } + for d := 1; d <= 31; d++ { + day[ humanize.Ordinal(d) ] = d + } +} + +func FormatDate(dateString string) (string, error) { + dateParts := strings.Split(dateString, " ") + if len(dateParts) != 3 { + return "", errors.New("invalid date format") + } + d := day[dateParts[0]] + if d == 0 { + return "", errors.New("invalid day in date") + } + m := mon[dateParts[1]] + if m == 0 { + return "", errors.New("invalid month in date") + } + y, err := strconv.Atoi(dateParts[2]) + if err != nil { + return "", errors.New("invalid year in date") + } + if y < 1900 || y > 2100 { + return "", errors.New("year out of range in date") + } + formattedDate := fmt.Sprintf("%04d-%02d-%02d", y, m, d) + + return formattedDate, nil +} + +func main() { + runSolution := func(inputs any) (any, error) { + return FormatDate(inputs.(string)) + } + + inputExample := `"()()"` + + inputSchema := `{ + "type": "string" + }` + + run.RunWeeklyChallenge(runSolution, inputExample, inputSchema) +} diff --git a/challenge-346/ysth/go/ch-2.go b/challenge-346/ysth/go/ch-2.go new file mode 100644 index 0000000000..37da62a3c3 --- /dev/null +++ b/challenge-346/ysth/go/ch-2.go @@ -0,0 +1,40 @@ +package main + +import ( + run "github.com/ysth/runweeklychallenge-go" +) + +func FormatPhoneNumber(phoneNumber string) (string) { + formattedPhoneNumber := make([]rune, 0, len(phoneNumber)+(len(phoneNumber)-1)/3) + count := 0 + for _, r := range phoneNumber { + if r >= '0' && r <= '9' { + if count == 3 { + count = 0 + formattedPhoneNumber = append(formattedPhoneNumber, '-') + } + formattedPhoneNumber = append(formattedPhoneNumber, r) + count++ + } + } + count = len(formattedPhoneNumber) + if count > 4 && formattedPhoneNumber[count-2] == '-' { + formattedPhoneNumber[count-2], formattedPhoneNumber[count-3] = + formattedPhoneNumber[count-3], formattedPhoneNumber[count-2] + } + return string(formattedPhoneNumber) +} + +func main() { + runSolution := func(inputs any) (any, error) { + return FormatPhoneNumber(inputs.(string)), nil + } + + inputExample := `"12 345-6789"` + + inputSchema := `{ + "type": "string" + }` + + run.RunWeeklyChallenge(runSolution, inputExample, inputSchema) +} diff --git a/challenge-346/ysth/perl/README.md b/challenge-346/ysth/perl/README.md new file mode 100644 index 0000000000..60d86e8401 --- /dev/null +++ b/challenge-346/ysth/perl/README.md @@ -0,0 +1 @@ +# Solutions by Yitzchak Scott-Thoennes diff --git a/challenge-346/ysth/perl/ch-1.pl b/challenge-346/ysth/perl/ch-1.pl new file mode 100644 index 0000000000..d22b27d9f4 --- /dev/null +++ b/challenge-346/ysth/perl/ch-1.pl @@ -0,0 +1,36 @@ +use 5.040; + +use Lingua::EN::Inflect (); +use Time::Piece (); + +sub format_date($date_string) { + use Exception::Class 'InvalidDateString' => { 'description' => "Invalid date string" }; + + state @mon_list = Time::Piece::mon_list(); + state %month = map( ($mon_list[$_], $_+1), 0..$#mon_list ); + state @day_list = map Lingua::EN::Inflect::ORD($_), 1..31; + state %day = map( ($day_list[$_], $_+1), 0..$#day_list ); + state $parse_date_string = do { local $"='|'; qr/^(@day_list) (@mon_list) ((?:19|20)\d\d|2100)\z/a }; + + my ($day, $month, $year) = $date_string =~ $parse_date_string + or InvalidDateString->throw; + + my $formatted_date = sprintf '%04d-%02d-%02d', $year, $month{$month}, $day{$day}; + + return $formatted_date; +} + +sub main() { + require Run::WeeklyChallenge; + + my $run_solution = sub ($inputs) { + format_date($inputs); + }; + my $inputs_example = '"10th Nov 2025"'; + my $inputs_schema_json = '{ + "type": "string" + }'; + Run::WeeklyChallenge::run_weekly_challenge($run_solution, $inputs_example, $inputs_schema_json); +} + +main() unless caller; diff --git a/challenge-346/ysth/perl/ch-2.pl b/challenge-346/ysth/perl/ch-2.pl new file mode 100644 index 0000000000..9cc1c5cb01 --- /dev/null +++ b/challenge-346/ysth/perl/ch-2.pl @@ -0,0 +1,21 @@ +use 5.040; + +sub format_phone_number($phone_number) { + # hyphen in the middle of 4 final digits, otherwise every three digits + join '-', $phone_number =~ y/0-9//cdr =~ /(\d{2,3}(?!\d\z))/ga; +} + +sub main() { + require Run::WeeklyChallenge; + + my $run_solution = sub ($inputs) { + format_phone_number($inputs); + }; + my $inputs_example = '"12 345-6789"'; + my $inputs_schema_json = '{ + "type": "string" + }'; + Run::WeeklyChallenge::run_weekly_challenge($run_solution, $inputs_example, $inputs_schema_json); +} + +main() unless caller; diff --git a/challenge-346/ysth/python/ch-1.py b/challenge-346/ysth/python/ch-1.py new file mode 100644 index 0000000000..d9a730a688 --- /dev/null +++ b/challenge-346/ysth/python/ch-1.py @@ -0,0 +1,47 @@ +import run_weeklychallenge as run +import datetime +import humanize +import locale +from typing import Dict + +locale.setlocale(locale.LC_TIME, 'en_US.utf8') +humanize.activate('en_US') + +mon: Dict[str, int] = { datetime.date(datetime.MINYEAR,m,1).strftime('%b'): m for m in range(1,13) } +day: Dict[str, int] = { humanize.ordinal(d): d for d in range(1,32) } + +def format_date(date_string: str) -> str: + date_parts = date_string.split(' ') + if len(date_parts) != 3: + raise Exception("invalid date format") + d = day.get(date_parts[0]) + if not d: + raise Exception("invalid day in date") + m = mon.get(date_parts[1]) + if not m: + raise Exception("invalid month in date") + try: + y = int(date_parts[2]) + except ValueError: + raise Exception("invalid year in date") + if y < 1900 or y > 2100: + raise Exception("year out of range in date") + + formatted_date = f'{y:04}-{m:02}-{d:02}' + + return formatted_date + +def main() -> None: + def run_solution(inputs: object) -> object: + return format_date(str(inputs)) + + input_schema_json = '''{ + "type": "string" + }''' + + input_example = '"10th Nov 2025"' + + run.run_weekly_challenge(run_solution, input_example, input_schema_json) + +if __name__ == '__main__': + main() diff --git a/challenge-346/ysth/python/ch-2.py b/challenge-346/ysth/python/ch-2.py new file mode 100644 index 0000000000..fde2e95b49 --- /dev/null +++ b/challenge-346/ysth/python/ch-2.py @@ -0,0 +1,31 @@ +import run_weeklychallenge as run +import itertools +import re + +def format_phone_number(phone_number: str) -> str: + formatted_phone_number = re.sub( + r'(.)-(.)\Z', + r'-\1\2', + '-'.join( + ''.join(group) for group in itertools.batched( + re.sub(r'[^0-9]', '', phone_number), + 3 + ) + ) + ) + return formatted_phone_number + +def main() -> None: + def run_solution(inputs: object) -> object: + return format_phone_number(str(inputs)) + + input_schema_json = '''{ + "type": "string" + }''' + + input_example = '"12 345-6789"' + + run.run_weekly_challenge(run_solution, input_example, input_schema_json) + +if __name__ == '__main__': + main() |
