aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-11-17 00:39:30 +0000
committerGitHub <noreply@github.com>2025-11-17 00:39:30 +0000
commit95b981c5c758b7cd9a66cbf7c7ecdd0a9cff9357 (patch)
treea67eed5dfb2a8e1e75081aafae828c09a50eaa35
parent239301df32c395e801d6a8dc014667ddc3e1a09b (diff)
parentfda37e11166cd5d90e18f28a55d3759143e4ac35 (diff)
downloadperlweeklychallenge-club-95b981c5c758b7cd9a66cbf7c7ecdd0a9cff9357.tar.gz
perlweeklychallenge-club-95b981c5c758b7cd9a66cbf7c7ecdd0a9cff9357.tar.bz2
perlweeklychallenge-club-95b981c5c758b7cd9a66cbf7c7ecdd0a9cff9357.zip
Merge pull request #13037 from ysth/challenge-346-ysth
weekly challenge 346 perl, python, and go solutions by ysth
-rw-r--r--challenge-346/ysth/go/ch-1.go61
-rw-r--r--challenge-346/ysth/go/ch-2.go40
-rw-r--r--challenge-346/ysth/perl/README.md1
-rw-r--r--challenge-346/ysth/perl/ch-1.pl36
-rw-r--r--challenge-346/ysth/perl/ch-2.pl21
-rw-r--r--challenge-346/ysth/python/ch-1.py47
-rw-r--r--challenge-346/ysth/python/ch-2.py31
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()