|
| 1 | +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/ |
| 2 | +/*# |
| 3 | +
|
| 4 | +Task 1: Format Date |
| 5 | +
|
| 6 | +Submitted by: [44]Mohammad Sajid Anwar |
| 7 | + __________________________________________________________________ |
| 8 | +
|
| 9 | + You are given a date in the form: 10th Nov 2025. |
| 10 | +
|
| 11 | + Write a script to format the given date in the form: 2025-11-10 using |
| 12 | + the set below. |
| 13 | +@DAYS = ("1st", "2nd", "3rd", ....., "30th", "31st") |
| 14 | +@MONTHS = ("Jan", "Feb", "Mar", ....., "Nov", "Dec") |
| 15 | +@YEARS = (1900..2100) |
| 16 | +
|
| 17 | +Example 1 |
| 18 | +
|
| 19 | +Input: $str = "1st Jan 2025" |
| 20 | +Output: "2025-01-01" |
| 21 | +
|
| 22 | +Example 2 |
| 23 | +
|
| 24 | +Input: $str = "22nd Feb 2025" |
| 25 | +Output: "2025-02-22" |
| 26 | +
|
| 27 | +Example 3 |
| 28 | +
|
| 29 | +Input: $str = "15th Apr 2025" |
| 30 | +Output: "2025-04-15" |
| 31 | +
|
| 32 | +Example 4 |
| 33 | +
|
| 34 | +Input: $str = "23rd Oct 2025" |
| 35 | +Output: "2025-10-23" |
| 36 | +
|
| 37 | +Example 5 |
| 38 | +
|
| 39 | +Input: $str = "31st Dec 2025" |
| 40 | +Output: "2025-12-31" |
| 41 | +
|
| 42 | +Task 2: Format Phone Number |
| 43 | +#*/ |
| 44 | + |
| 45 | + |
| 46 | +package main |
| 47 | + |
| 48 | +import ( |
| 49 | + "fmt" |
| 50 | + "io" |
| 51 | + "os" |
| 52 | + "slices" |
| 53 | + "strconv" |
| 54 | + "strings" |
| 55 | + |
| 56 | + "github.com/google/go-cmp/cmp" |
| 57 | +) |
| 58 | + |
| 59 | +func fd(str string) string { |
| 60 | + m := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} |
| 61 | + d := strings.Split(str, " ") |
| 62 | + day, _ := strconv.Atoi(d[0][:len(d[0])-2]) |
| 63 | + return fmt.Sprintf("%s-%02d-%02d", d[2], slices.Index(m, d[1])+1, day) |
| 64 | +} |
| 65 | + |
| 66 | +func main() { |
| 67 | + for _, data := range []struct { |
| 68 | + input, output string |
| 69 | + }{ |
| 70 | + {"1st Jan 2025", "2025-01-01"}, |
| 71 | + {"22nd Feb 2025", "2025-02-22"}, |
| 72 | + {"15th Apr 2025", "2025-04-15"}, |
| 73 | + {"23rd Oct 2025", "2025-10-23"}, |
| 74 | + {"31st Dec 2025", "2025-12-31"}, |
| 75 | + } { |
| 76 | + io.WriteString(os.Stdout, cmp.Diff(fd(data.input), data.output)) // blank if ok, otherwise show the difference |
| 77 | + } |
| 78 | +} |
0 commit comments