diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-01 21:23:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-01 21:23:07 +0100 |
| commit | 2d220196d38a70e58091963472701a5e12d0575f (patch) | |
| tree | 9f3c46bd68de8c9c25698abc3887ef31fa395560 /challenge-110 | |
| parent | 98b600782520071bf6c8c7ff605104d68ece4a6b (diff) | |
| parent | d22f3756f5a98fc71617e83f83d09f8224e028d1 (diff) | |
| download | perlweeklychallenge-club-2d220196d38a70e58091963472701a5e12d0575f.tar.gz perlweeklychallenge-club-2d220196d38a70e58091963472701a5e12d0575f.tar.bz2 perlweeklychallenge-club-2d220196d38a70e58091963472701a5e12d0575f.zip | |
Merge pull request #3985 from aaronreidsmith/challenge-110
Challenge 110 - Raku
Diffstat (limited to 'challenge-110')
| -rw-r--r-- | challenge-110/aaronreidsmith/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-110/aaronreidsmith/raku/ch-1.raku | 41 | ||||
| -rw-r--r-- | challenge-110/aaronreidsmith/raku/ch-2.raku | 37 |
3 files changed, 79 insertions, 0 deletions
diff --git a/challenge-110/aaronreidsmith/blog.txt b/challenge-110/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..7ec6620068 --- /dev/null +++ b/challenge-110/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-110/ diff --git a/challenge-110/aaronreidsmith/raku/ch-1.raku b/challenge-110/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..17d6de24e7 --- /dev/null +++ b/challenge-110/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku + +sub challenge(Str $file-path) returns Str { + $file-path + .IO + .lines + .grep(* ~~ / + ^ + <space>* + [\+<digit> ** 2 | \(<digit> ** 2\) | <digit> ** 4] + <space> + <digit> ** 10 + $ + /) + .join("\n"); +} + +multi sub MAIN(Str $file-path) { + say challenge($file-path); +} + +multi sub MAIN(Bool :$test) { + use Test; + + # Create a file for testing + my $test-file = 'test.txt'; + $test-file.IO.spurt("0044 1148820341\n", :append); + $test-file.IO.spurt(" +44 1148820341\n", :append); + $test-file.IO.spurt(" 44-11-4882-0341\n", :append); + $test-file.IO.spurt("(44) 1148820341\n", :append); + $test-file.IO.spurt(" 00 1148820341\n", :append); + + my $expected = "0044 1148820341\n +44 1148820341\n(44) 1148820341"; + + is(challenge($test-file), $expected); + + # Delete when we're done + $test-file.IO.unlink; + + done-testing; +} diff --git a/challenge-110/aaronreidsmith/raku/ch-2.raku b/challenge-110/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..cb7050b012 --- /dev/null +++ b/challenge-110/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,37 @@ +#!/usr/bin/env raku + +use Text::CSV; # Imports `csv` function + +sub challenge(Str $file-path) returns Str { + my @lines = csv(in => $file-path, headers => "auto"); + say ('name', |@lines.map(*<name>)); + my $names = ('name', |@lines.map(*<name>)).join(','); + my $ages = ('age', |@lines.map(*<age>)).join(','); + my $sexes = ('sex', |@lines.map(*<sex>)).join(','); + ($names, $ages, $sexes).join("\n"); +} + +multi sub MAIN(Str $file-path) { + say challenge($file-path); +} + +multi sub MAIN(Bool :$test) { + use Test; + + # Create a file for testing + my $test-file = 'test.csv'; + $test-file.IO.spurt("name,age,sex\n", :append); + $test-file.IO.spurt("Mohammad,45,m\n", :append); + $test-file.IO.spurt("Joe,20,m\n", :append); + $test-file.IO.spurt("Julie,35,f\n", :append); + $test-file.IO.spurt("Cristina,10,f\n", :append); + + my $expected = "name,Mohammad,Joe,Julie,Cristina\nage,45,20,35,10\nsex,m,m,f,f"; + + is(challenge($test-file), $expected); + + # Delete when we're done + $test-file.IO.unlink; + + done-testing; +} |
