diff options
| author | Ali <adeadmarshal@gmail.com> | 2025-11-12 07:41:22 +0330 |
|---|---|---|
| committer | Ali <adeadmarshal@gmail.com> | 2025-11-12 07:41:22 +0330 |
| commit | e91dd50dc57c0e673db9e94d6c16e2ec4a2994cc (patch) | |
| tree | c79558bf9b85115f9e0b0ed2a68eb5657d92fe8f | |
| parent | ef8c486e620b253bd27a3cebcb65af6f57d003ff (diff) | |
| download | perlweeklychallenge-club-e91dd50dc57c0e673db9e94d6c16e2ec4a2994cc.tar.gz perlweeklychallenge-club-e91dd50dc57c0e673db9e94d6c16e2ec4a2994cc.tar.bz2 perlweeklychallenge-club-e91dd50dc57c0e673db9e94d6c16e2ec4a2994cc.zip | |
TWC347
| -rw-r--r-- | challenge-347/deadmarshal/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-347/deadmarshal/go/ch1.go | 24 | ||||
| -rw-r--r-- | challenge-347/deadmarshal/go/ch2.go | 31 | ||||
| -rw-r--r-- | challenge-347/deadmarshal/java/Ch1.java | 17 | ||||
| -rw-r--r-- | challenge-347/deadmarshal/java/Ch2.java | 28 | ||||
| -rw-r--r-- | challenge-347/deadmarshal/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-347/deadmarshal/perl/ch-2.pl | 19 |
7 files changed, 138 insertions, 0 deletions
diff --git a/challenge-347/deadmarshal/blog.txt b/challenge-347/deadmarshal/blog.txt new file mode 100644 index 0000000000..ce3268493e --- /dev/null +++ b/challenge-347/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2025/11/twc347.html diff --git a/challenge-347/deadmarshal/go/ch1.go b/challenge-347/deadmarshal/go/ch1.go new file mode 100644 index 0000000000..4175a30332 --- /dev/null +++ b/challenge-347/deadmarshal/go/ch1.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "strconv" + "strings" +) + +func formatDate(date string) string { + s := strings.Split(date, " ") + day, _ := strconv.Atoi(s[0][:len(s[0])-2]) + months := "JanFebMarAprMayJunJulAugSepOctNovDec" + month := strings.Index(months, s[1])/3 + 1 + year, _ := strconv.Atoi(s[2]) + return fmt.Sprintf("%d-%02d-%02d", year, month, day) +} + +func main() { + fmt.Println(formatDate("1st Jan 2025")) + fmt.Println(formatDate("22nd Feb 2025")) + fmt.Println(formatDate("15th Apr 2025")) + fmt.Println(formatDate("23rd Oct 2025")) + fmt.Println(formatDate("31st Dec 2025")) +} diff --git a/challenge-347/deadmarshal/go/ch2.go b/challenge-347/deadmarshal/go/ch2.go new file mode 100644 index 0000000000..0515bf05e4 --- /dev/null +++ b/challenge-347/deadmarshal/go/ch2.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "strings" +) + +func formatPhoneNumber(phone string) string { + phone = strings.ReplaceAll(phone, " ", "") + phone = strings.ReplaceAll(phone, "-", "") + n := len(phone) + res := []string{} + for i := 0; i < n/3; i++ { + res = append(res, phone[i*3:i*3+3]) + } + if n%3 == 1 { + res[len(res)-1] = res[len(res)-1][:2] + res = append(res, phone[n-2:]) + } else if n%3 == 2 { + res = append(res, phone[n-2:]) + } + return strings.Join(res, "-") +} + +func main() { + fmt.Println(formatPhoneNumber("1-23-45-6")) + fmt.Println(formatPhoneNumber("1234")) + fmt.Println(formatPhoneNumber("12 345-6789")) + fmt.Println(formatPhoneNumber("123 4567")) + fmt.Println(formatPhoneNumber("123 456-78")) +} diff --git a/challenge-347/deadmarshal/java/Ch1.java b/challenge-347/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..a218d1b168 --- /dev/null +++ b/challenge-347/deadmarshal/java/Ch1.java @@ -0,0 +1,17 @@ +public class Ch1 { + public static void main(String[] args) { + System.out.println(format_date("1st Jan 2025")); + System.out.println(format_date("22nd Feb 2025")); + System.out.println(format_date("15th Apr 2025")); + System.out.println(format_date("23rd Oct 2025")); + System.out.println(format_date("31st Dec 2025")); + } + + private static String format_date(String date) { + var s = date.split(" "); + String months = "JanFebMarAprMayJunJulAugSepOctNovDec"; + int day = Integer.parseInt(s[0].substring(0, s[0].length() - 2)); + int month = months.indexOf(s[1]) / 3 + 1; + return String.format("%s-%02d-%02d", s[2], month, day); + } +} diff --git a/challenge-347/deadmarshal/java/Ch2.java b/challenge-347/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..9f13dbfab0 --- /dev/null +++ b/challenge-347/deadmarshal/java/Ch2.java @@ -0,0 +1,28 @@ +import java.util.ArrayList; +import java.util.List; + +public class Ch2 { + public static void main(String[] args) { + System.out.println(format_phone_number("1-23-45-6")); + System.out.println(format_phone_number("1234")); + System.out.println(format_phone_number("12 345-6789")); + System.out.println(format_phone_number("123 4567")); + System.out.println(format_phone_number("123 456-78")); + } + + private static String format_phone_number(String phone) { + phone = phone.replace("-", "").replace(" ", ""); + int n = phone.length(); + List<String> res = new ArrayList<>(); + for (int i = 0; i < n / 3; ++i) { + res.add(phone.substring(i * 3, i * 3 + 3)); + } + if (n % 3 == 1) { + res.set(res.size() - 1, res.get(res.size() - 1).substring(0, 2)); + res.add(phone.substring(n - 2)); + } else if (n % 3 == 2) { + res.add(phone.substring(n - 2)); + } + return String.join("-", res); + } +} diff --git a/challenge-347/deadmarshal/perl/ch-1.pl b/challenge-347/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..7e457053c0 --- /dev/null +++ b/challenge-347/deadmarshal/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub format_date{ + my @s = split ' ',$_[0]; + my $months = "JanFebMarAprMayJunJulAugSepOctNovDec"; + my $day = substr $s[0],0,length($s[0]) - 2; + my $month = index($months,$s[1]) / 3 + 1; + sprintf "%s-%02d-%02d",$s[2],$month,$day +} + +printf "%s\n",format_date("1st Jan 2025"); +printf "%s\n",format_date("22nd Feb 2025"); +printf "%s\n",format_date("15th Apr 2025"); +printf "%s\n",format_date("23rd Oct 2025"); +printf "%s\n",format_date("31st Dec 2025"); + diff --git a/challenge-347/deadmarshal/perl/ch-2.pl b/challenge-347/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..601658398f --- /dev/null +++ b/challenge-347/deadmarshal/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub format_phone_number{ + my ($phone) = @_; + s/[- ]//g, + s/(...)/$1-/g, + s/-(.?)$/$1/, + s/(^|-)(\d{2})(\d{2})$/$1$2-$3/ for $phone; + $phone +} + +printf "%s\n",format_phone_number('1-23-45-6'); +printf "%s\n",format_phone_number('1234'); +printf "%s\n",format_phone_number('12 345-6789'); +printf "%s\n",format_phone_number('123 4567'); +printf "%s\n",format_phone_number('123 456-78') + |
