diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-05-17 06:57:07 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-05-17 06:57:07 +0100 |
| commit | c3cd45087006d3f63b05219b8280a25dc1ea7ba9 (patch) | |
| tree | ebd94e12f33bd5860791fb2d7ac49d4074c989f3 /challenge-113/andinus | |
| parent | b848049a94b216d459403b8590b26ec59e5746fd (diff) | |
| download | perlweeklychallenge-club-c3cd45087006d3f63b05219b8280a25dc1ea7ba9.tar.gz perlweeklychallenge-club-c3cd45087006d3f63b05219b8280a25dc1ea7ba9.tar.bz2 perlweeklychallenge-club-c3cd45087006d3f63b05219b8280a25dc1ea7ba9.zip | |
- Added template for week 113.
Diffstat (limited to 'challenge-113/andinus')
| -rw-r--r-- | challenge-113/andinus/README | 124 | ||||
| -rw-r--r-- | challenge-113/andinus/README.org | 102 |
2 files changed, 226 insertions, 0 deletions
diff --git a/challenge-113/andinus/README b/challenge-113/andinus/README new file mode 100644 index 0000000000..cef31723b7 --- /dev/null +++ b/challenge-113/andinus/README @@ -0,0 +1,124 @@ + ━━━━━━━━━━━━━━━ + CHALLENGE 110 + + Andinus + ━━━━━━━━━━━━━━━ + + + 2021-04-29 + + +Table of Contents +───────────────── + +Task 1 - Valid Phone Numbers +Task 2 - Transpose File + + + + + +Task 1 - Valid Phone Numbers +════════════════════════════ + + You are given a text file. + + Write a script to display all valid phone numbers in the given text + file. + + 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 +──── + + • Program: <file:raku/ch-1.raku> + + `PhoneNumber' grammar parses each entry one by one. `country-code' + handles the 3 cases for country codes & `number' handles the other + part. + + We just loop over every entry & print it if it's a valid phone number. + + ┌──── + │ 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); + │ } + └──── + + +Task 2 - Transpose File +═══════════════════════ + + You are given a text file. + + Write a script to transpose the contents of the given file. + + Input File: + ┌──── + │ name,age,sex + │ Mohammad,45,m + │ Joe,20,m + │ Julie,35,f + │ Cristina,10,f + └──── + + Output: + ┌──── + │ name,Mohammad,Joe,Julie,Cristina + │ age,45,20,35,10 + │ sex,m,m,f,f + └──── + + +Raku +──── + + • Program: <file:raku/ch-2.raku> + + `.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. + + 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. + + 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. + + We then join the entries with ",", loop over the formatted lines & + print them. + + ┌──── + │ .say for zip($file.IO.lines.map(*.split(","))).map(*.join(",")); + └──── diff --git a/challenge-113/andinus/README.org b/challenge-113/andinus/README.org new file mode 100644 index 0000000000..221092d3f0 --- /dev/null +++ b/challenge-113/andinus/README.org @@ -0,0 +1,102 @@ +#+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 - Valid Phone Numbers + +You are given a text file. + +Write a script to display all valid phone numbers in the given text file. + +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]] + +~PhoneNumber~ grammar parses each entry one by one. ~country-code~ handles +the 3 cases for country codes & ~number~ handles the other part. + +We just loop over every entry & print it if it's a valid phone number. + +#+begin_src raku +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 + +* Task 2 - Transpose File + +You are given a text file. + +Write a script to transpose the contents of the given file. + +Input File: +#+begin_src +name,age,sex +Mohammad,45,m +Joe,20,m +Julie,35,f +Cristina,10,f +#+end_src + +Output: +#+begin_src +name,Mohammad,Joe,Julie,Cristina +age,45,20,35,10 +sex,m,m,f,f +#+end_src + +** Raku + +- Program: [[file:raku/ch-2.raku]] + +~.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. + +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. + +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 + +We then join the entries with ",", loop over the formatted lines & print +them. + +#+begin_src raku +.say for zip($file.IO.lines.map(*.split(","))).map(*.join(",")); +#+end_src |
