diff options
| author | David Ferrone <zapwai@gmail.com> | 2025-11-10 10:35:01 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-10 10:35:01 -0500 |
| commit | 2e04e089fa13ddcf96a18ac530185f6028590b2c (patch) | |
| tree | 2b6b877625a51516ec7a7d4525c6bc935626253f | |
| parent | 83c02309afd45cdbfce02a54ee39361fce28c592 (diff) | |
| download | perlweeklychallenge-club-2e04e089fa13ddcf96a18ac530185f6028590b2c.tar.gz perlweeklychallenge-club-2e04e089fa13ddcf96a18ac530185f6028590b2c.tar.bz2 perlweeklychallenge-club-2e04e089fa13ddcf96a18ac530185f6028590b2c.zip | |
Week 347
| -rw-r--r-- | challenge-347/zapwai/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-347/zapwai/perl/ch-2.pl | 43 |
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-347/zapwai/perl/ch-1.pl b/challenge-347/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..36d8391100 --- /dev/null +++ b/challenge-347/zapwai/perl/ch-1.pl @@ -0,0 +1,40 @@ +use v5.38; + +my @DAYS = qw(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 = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); + +sub dodi($n) { + if ($n < 10) { + return "0$n"; + } else { + return $n; + } +} + +sub proc($str) { + say "Input: \$str = $str"; + my ($day, $mon, $yr) = split ' ', $str; + my ($day_index, $mon_index); + for my $i (0 .. $#DAYS) { + $day_index = $i if ($DAYS[$i] eq $day); + } + + for my $j (0 .. $#MONTHS) { + $mon_index = $j if ($MONTHS[$j] eq $mon); + } + my $output = "$yr-".dodi($mon_index+1)."-".dodi($day_index+1); + say "Output: $output"; +} + +my $str = "1st Jan 2025"; +proc($str); +$str = "22nd Feb 2025"; +proc($str); +$str = "15th Apr 2025"; +proc($str); +$str = "23rd Oct 2025"; +proc($str); +$str = "31st Dec 2025"; +proc($str); diff --git a/challenge-347/zapwai/perl/ch-2.pl b/challenge-347/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..acaea0bc83 --- /dev/null +++ b/challenge-347/zapwai/perl/ch-2.pl @@ -0,0 +1,43 @@ +use v5.38; + +sub proc($phone) { + say "Input : $phone"; + my @num; + for my $c (split '', $phone) { + push @num, $c unless ($c eq "-" || $c eq " "); + } + my $o = ""; + my $post = ""; # last four chars, if necessary + my $len = scalar @num; + my @post; # used only in that case + if ($len % 3 == 1) { + for (1 .. 4) { + unshift @post, pop @num; + } + $post = "$post[0]$post[1]-$post[2]$post[3]"; + } + for my $i (0 .. $#num) { + $o .= $num[$i]; + $o .= "-" if ($i % 3 == 2 && $i > 0 && $i < $#num); + } + + print "Output: $o"; + if ($post) { + print "-" if ($o); + say "$post"; + } else { + say ""; + } + +} + +my $phone = "1-23-45-6"; +proc($phone); +$phone = "1234"; +proc($phone); +$phone = "12 345-6789"; +proc($phone); +$phone = "123 4567"; +proc($phone); +$phone = "123 456-78"; +proc($phone); |
