aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-04-29 12:25:22 +0100
committerGitHub <noreply@github.com>2021-04-29 12:25:22 +0100
commitab06dafbe218406535ed46f1ed4d76d282c6ca0c (patch)
tree1739b7e970f7dabd66933ba5f61d6620b5c75107
parent0b697cd60bdc24818afe742c171643f888ea1b72 (diff)
parent5cb878b47514a7184ff6e8ec032f5faf400c0e8a (diff)
downloadperlweeklychallenge-club-ab06dafbe218406535ed46f1ed4d76d282c6ca0c.tar.gz
perlweeklychallenge-club-ab06dafbe218406535ed46f1ed4d76d282c6ca0c.tar.bz2
perlweeklychallenge-club-ab06dafbe218406535ed46f1ed4d76d282c6ca0c.zip
Merge pull request #3978 from andinus/master
Add Raku solutions for challenge-110
-rw-r--r--challenge-110/andinus/README148
-rw-r--r--challenge-110/andinus/README.org140
-rw-r--r--challenge-110/andinus/blog-1.txt1
-rw-r--r--challenge-110/andinus/blog-2.txt1
-rwxr-xr-xchallenge-110/andinus/raku/ch-1.raku16
-rwxr-xr-xchallenge-110/andinus/raku/ch-2.raku5
-rw-r--r--challenge-110/andinus/raku/input-ch15
-rw-r--r--challenge-110/andinus/raku/input-ch25
8 files changed, 178 insertions, 143 deletions
diff --git a/challenge-110/andinus/README b/challenge-110/andinus/README
index 3113a6fd55..cef31723b7 100644
--- a/challenge-110/andinus/README
+++ b/challenge-110/andinus/README
@@ -1,32 +1,53 @@
━━━━━━━━━━━━━━━
- CHALLENGE 100
+ CHALLENGE 110
Andinus
━━━━━━━━━━━━━━━
- 2021-02-20
+ 2021-04-29
Table of Contents
─────────────────
-Task 1 - Fun Time
-.. Raku
+Task 1 - Valid Phone Numbers
+Task 2 - Transpose File
-Task 1 - Fun Time
-═════════════════
+Task 1 - Valid Phone Numbers
+════════════════════════════
- You are given a time (12 hour / 24 hour).
+ You are given a text file.
- Write a script to convert the given time from 12 hour format to 24
- hour format and vice versa.
+ Write a script to display all valid phone numbers in the given text
+ file.
- Ideally we expect a one-liner.
+ Acceptable Phone Number Formats:
+ ┌────
+ │ +nn nnnnnnnnnn
+ │ (nn) nnnnnnnnnn
+ │ nnnn nnnnnnnnnn
+ └────
+
+ Input File:
+ ┌────
+ │ 0044 1148820341
+ │ +44 1148820341
+ │ 44-11-4882-0341
+ │ (44) 1148820341
+ │ 00 1148820341
+ └────
+
+ Output:
+ ┌────
+ │ 0044 1148820341
+ │ +44 1148820341
+ │ (44) 1148820341
+ └────
Raku
@@ -34,89 +55,70 @@ Raku
• Program: <file:raku/ch-1.raku>
- One should use `DateTime' module to solve this but that is not fun so
- we solve it the wrong way!
+ `PhoneNumber' grammar parses each entry one by one. `country-code'
+ handles the 3 cases for country codes & `number' handles the other
+ part.
- The program will accept any string as `$time'. We do the format check
- later.
+ We just loop over every entry & print it if it's a valid phone number.
┌────
- │ #| convert 12-hour formatted time to 24-hour format and vice-versa
- │ unit sub MAIN (
- │ Str $time, #= time (format: 05:15pm or "05:15 pm" or 17:00)
- │ );
+ │ grammar PhoneNumber {
+ │ token TOP { \s* <country-code> \s+ <number> }
+ │ token country-code { '+' \d \d | '(' \d \d ')' | \d ** 4 }
+ │ token number { \d ** 10 }
+ │ }
+ │
+ │ for $file.IO.lines -> $entry {
+ │ say $entry if PhoneNumber.parse($entry);
+ │ }
└────
- The grammar `Time' will parse `$time' to give us meaningful
- information required to do the task.
- ⁃ `hour' & `minute' match any digit ranging from 00 to 99
- ⁃ `meridiem' matches either /am/ or /pm/
+Task 2 - Transpose File
+═══════════════════════
+
+ You are given a text file.
- ⁃ Note: This grammar will consider "99:99" as a valid timestamp.
+ Write a script to transpose the contents of the given file.
+ Input File:
┌────
- │ grammar Time {
- │ token TOP { <hour> ':' <minute> ' '? <meridiem>? }
- │ token hour { \d ** 1..2 }
- │ token minute { \d ** 1..2 }
- │ token meridiem { ['am'|'pm'] }
- │ }
+ │ name,age,sex
+ │ Mohammad,45,m
+ │ Joe,20,m
+ │ Julie,35,f
+ │ Cristina,10,f
└────
- We parse `$time' with `Time' grammar. If `meridiem' is set then it
- must be 12-hour format time, otherwise it'll be 24-hour format time.
-
+ Output:
┌────
- │ # Match for time format.
- │ if Time.parse($time) -> $m {
- │ given $m<meridiem> {
- │ ...
- │ }
- │ } else {
- │ note "Wrong format!";
- │ exit 1;
- │ }
+ │ name,Mohammad,Joe,Julie,Cristina
+ │ age,45,20,35,10
+ │ sex,m,m,f,f
└────
- For "am" we just check if the hour is 12, if so then we print it as
- "00" otherwise just print the hour.
- ┌────
- │ when 'am' {
- │ printf "%02d:%02d\n",
- │ $m<hour> == 12 ?? "00" !! $m<hour>,
- │ $m<minute>;
- │ }
- └────
+Raku
+────
- If the hour is < 12 then we print `hour + 12' otherwise just print the
- hour.
+ • Program: <file:raku/ch-2.raku>
- ┌────
- │ when 'pm' {
- │ printf "%02d:%02d\n",
- │ $m<hour> < 12 ?? $m<hour> + 12 !! $m<hour>,
- │ $m<minute>;
- │ }
- └────
+ `.IO.lines' part creates a list of lines in the files, then we split
+ it at ",". `zip' then returns a list which is formatted the way we
+ want it to be.
- If the hour is 0 then print 12 otherwise check if it's > 12, if so
- then print `hour - 12' otherwise just print the hour.
+ From the documentation: <https://docs.raku.org/routine/zip>
+ Creates a supply that emits combined values as soon as
+ there is a new value seen on all of the supplies.
- 23 -> 11
- greater than 12
- 00 -> 12
- equal to 0
- 12 -> 12
- neither equal to 0, nor greater than 12
+ zip iterates through each of the input lists
+ synchronously, 'Zipping' them together, so that elements
+ are grouped according to their input list index, in the
+ order that the lists are provided.
- If the hour is < 12 then print "am" otherwise print "pm".
+ We then join the entries with ",", loop over the formatted lines &
+ print them.
┌────
- │ default {
- │ printf "%02d:%02d%s\n",
- │ $m<hour> == 0 ?? "12" !! $m<hour> > 12 ?? $m<hour> - 12 !! $m<hour>,
- │ $m<minute>, $m<hour> < 12 ?? "am" !! "pm";
- │ }
+ │ .say for zip($file.IO.lines.map(*.split(","))).map(*.join(","));
└────
diff --git a/challenge-110/andinus/README.org b/challenge-110/andinus/README.org
index 9fe366c58c..221092d3f0 100644
--- a/challenge-110/andinus/README.org
+++ b/challenge-110/andinus/README.org
@@ -1,102 +1,102 @@
-#+title: Challenge 100
-#+date: 2021-02-20
-#+options: toc:2
+#+title: Challenge 110
+#+date: 2021-04-29
#+html_link_up: ../index.html
#+export_file_name: index
#+setupfile: ~/.emacs.d/org-templates/level-2.org
-* Task 1 - Fun Time
+* Task 1 - Valid Phone Numbers
-You are given a time (12 hour / 24 hour).
+You are given a text file.
-Write a script to convert the given time from 12 hour format to 24 hour
-format and vice versa.
+Write a script to display all valid phone numbers in the given text file.
-Ideally we expect a one-liner.
+Acceptable Phone Number Formats:
+#+begin_src
++nn nnnnnnnnnn
+(nn) nnnnnnnnnn
+nnnn nnnnnnnnnn
+#+end_src
+
+Input File:
+#+begin_src
+0044 1148820341
+ +44 1148820341
+ 44-11-4882-0341
+(44) 1148820341
+ 00 1148820341
+#+end_src
+
+Output:
+#+begin_src
+0044 1148820341
+ +44 1148820341
+(44) 1148820341
+#+end_src
** Raku
- Program: [[file:raku/ch-1.raku]]
-One should use ~DateTime~ module to solve this but that is not fun so we
-solve it the wrong way!
+~PhoneNumber~ grammar parses each entry one by one. ~country-code~ handles
+the 3 cases for country codes & ~number~ handles the other part.
-The program will accept any string as ~$time~. We do the format check later.
+We just loop over every entry & print it if it's a valid phone number.
#+begin_src raku
-#| convert 12-hour formatted time to 24-hour format and vice-versa
-unit sub MAIN (
- Str $time, #= time (format: 05:15pm or "05:15 pm" or 17:00)
-);
+grammar PhoneNumber {
+ token TOP { \s* <country-code> \s+ <number> }
+ token country-code { '+' \d \d | '(' \d \d ')' | \d ** 4 }
+ token number { \d ** 10 }
+}
+
+for $file.IO.lines -> $entry {
+ say $entry if PhoneNumber.parse($entry);
+}
#+end_src
-The grammar ~Time~ will parse ~$time~ to give us meaningful information
-required to do the task.
+* Task 2 - Transpose File
-+ ~hour~ & ~minute~ match any digit ranging from 00 to 99
-+ ~meridiem~ matches either /am/ or /pm/
+You are given a text file.
-+ *Note*: This grammar will consider "99:99" as a valid timestamp.
+Write a script to transpose the contents of the given file.
-#+begin_src raku
-grammar Time {
- token TOP { <hour> ':' <minute> ' '? <meridiem>? }
- token hour { \d ** 1..2 }
- token minute { \d ** 1..2 }
- token meridiem { ['am'|'pm'] }
-}
+Input File:
+#+begin_src
+name,age,sex
+Mohammad,45,m
+Joe,20,m
+Julie,35,f
+Cristina,10,f
#+end_src
-We parse ~$time~ with ~Time~ grammar. If ~meridiem~ is set then it must be
-12-hour format time, otherwise it'll be 24-hour format time.
-
-#+begin_src raku
-# Match for time format.
-if Time.parse($time) -> $m {
- given $m<meridiem> {
- ...
- }
-} else {
- note "Wrong format!";
- exit 1;
-}
+Output:
+#+begin_src
+name,Mohammad,Joe,Julie,Cristina
+age,45,20,35,10
+sex,m,m,f,f
#+end_src
-For "am" we just check if the hour is 12, if so then we print it as "00"
-otherwise just print the hour.
-
-#+begin_src raku
-when 'am' {
- printf "%02d:%02d\n",
- $m<hour> == 12 ?? "00" !! $m<hour>,
- $m<minute>;
-}
-#+end_src
+** Raku
-If the hour is < 12 then we print =hour + 12= otherwise just print the
-hour.
+- Program: [[file:raku/ch-2.raku]]
-#+begin_src raku
-when 'pm' {
- printf "%02d:%02d\n",
- $m<hour> < 12 ?? $m<hour> + 12 !! $m<hour>,
- $m<minute>;
-}
-#+end_src
+~.IO.lines~ part creates a list of lines in the files, then we split it at
+",". ~zip~ then returns a list which is formatted the way we want it to
+be.
-If the hour is 0 then print 12 otherwise check if it's > 12, if so then
-print =hour - 12= otherwise just print the hour.
+From the documentation: https://docs.raku.org/routine/zip
+#+begin_quote
+Creates a supply that emits combined values as soon as there is a new
+value seen on all of the supplies.
-+ 23 -> 11 :: greater than 12
-+ 00 -> 12 :: equal to 0
-+ 12 -> 12 :: neither equal to 0, nor greater than 12
+zip iterates through each of the input lists synchronously, 'Zipping'
+them together, so that elements are grouped according to their input
+list index, in the order that the lists are provided.
+#+end_quote
-If the hour is < 12 then print "am" otherwise print "pm".
+We then join the entries with ",", loop over the formatted lines & print
+them.
#+begin_src raku
-default {
- printf "%02d:%02d%s\n",
- $m<hour> == 0 ?? "12" !! $m<hour> > 12 ?? $m<hour> - 12 !! $m<hour>,
- $m<minute>, $m<hour> < 12 ?? "am" !! "pm";
-}
+.say for zip($file.IO.lines.map(*.split(","))).map(*.join(","));
#+end_src
diff --git a/challenge-110/andinus/blog-1.txt b/challenge-110/andinus/blog-1.txt
new file mode 100644
index 0000000000..1514f49801
--- /dev/null
+++ b/challenge-110/andinus/blog-1.txt
@@ -0,0 +1 @@
+https://andinus.tilde.institute/pwc/challenge-110/
diff --git a/challenge-110/andinus/blog-2.txt b/challenge-110/andinus/blog-2.txt
new file mode 100644
index 0000000000..1514f49801
--- /dev/null
+++ b/challenge-110/andinus/blog-2.txt
@@ -0,0 +1 @@
+https://andinus.tilde.institute/pwc/challenge-110/
diff --git a/challenge-110/andinus/raku/ch-1.raku b/challenge-110/andinus/raku/ch-1.raku
new file mode 100755
index 0000000000..eec4583c95
--- /dev/null
+++ b/challenge-110/andinus/raku/ch-1.raku
@@ -0,0 +1,16 @@
+#!/usr/bin/env raku
+
+#| displays valid phone numbers, given a text file
+unit sub MAIN (
+ Str $file where *.IO.f = "input-ch1", #= file with phone numbers
+);
+
+grammar PhoneNumber {
+ token TOP { \s* <country-code> \s+ <number> }
+ token country-code { '+' \d \d | '(' \d \d ')' | \d ** 4 }
+ token number { \d ** 10 }
+}
+
+for $file.IO.lines -> $entry {
+ say $entry if PhoneNumber.parse($entry);
+}
diff --git a/challenge-110/andinus/raku/ch-2.raku b/challenge-110/andinus/raku/ch-2.raku
new file mode 100755
index 0000000000..c43e808008
--- /dev/null
+++ b/challenge-110/andinus/raku/ch-2.raku
@@ -0,0 +1,5 @@
+#!/usr/bin/env raku
+
+unit sub MAIN (Str $file where *.IO.f = "input-ch2");
+
+.say for zip($file.IO.lines.map(*.split(","))).map(*.join(","));
diff --git a/challenge-110/andinus/raku/input-ch1 b/challenge-110/andinus/raku/input-ch1
new file mode 100644
index 0000000000..48d6254741
--- /dev/null
+++ b/challenge-110/andinus/raku/input-ch1
@@ -0,0 +1,5 @@
+0044 1148820341
+ +44 1148820341
+ 44-11-4882-0341
+(44) 1148820341
+ 00 1148820341
diff --git a/challenge-110/andinus/raku/input-ch2 b/challenge-110/andinus/raku/input-ch2
new file mode 100644
index 0000000000..716ebdce75
--- /dev/null
+++ b/challenge-110/andinus/raku/input-ch2
@@ -0,0 +1,5 @@
+name,age,sex
+Mohammad,45,m
+Joe,20,m
+Julie,35,f
+Cristina,10,f