diff options
| author | Aaron Smith <aaronreidsmith@gmail.com> | 2021-05-01 14:57:04 -0500 |
|---|---|---|
| committer | Aaron Smith <aaronreidsmith@gmail.com> | 2021-05-01 14:57:04 -0500 |
| commit | d22f3756f5a98fc71617e83f83d09f8224e028d1 (patch) | |
| tree | 5431d16f81da8f9f39618d34d719234d92829fdc /challenge-110 | |
| parent | 5a54a93c68966fcf89aba39ae48d25e6d333e36f (diff) | |
| download | perlweeklychallenge-club-d22f3756f5a98fc71617e83f83d09f8224e028d1.tar.gz perlweeklychallenge-club-d22f3756f5a98fc71617e83f83d09f8224e028d1.tar.bz2 perlweeklychallenge-club-d22f3756f5a98fc71617e83f83d09f8224e028d1.zip | |
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; +} |
