From d22f3756f5a98fc71617e83f83d09f8224e028d1 Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Sat, 1 May 2021 14:57:04 -0500 Subject: Challenge 110 - Raku --- challenge-110/aaronreidsmith/blog.txt | 1 + challenge-110/aaronreidsmith/raku/ch-1.raku | 41 +++++++++++++++++++++++++++++ challenge-110/aaronreidsmith/raku/ch-2.raku | 37 ++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 challenge-110/aaronreidsmith/blog.txt create mode 100644 challenge-110/aaronreidsmith/raku/ch-1.raku create mode 100644 challenge-110/aaronreidsmith/raku/ch-2.raku 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(* ~~ / + ^ + * + [\+ ** 2 | \( ** 2\) | ** 4] + + ** 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(*)); + my $names = ('name', |@lines.map(*)).join(','); + my $ages = ('age', |@lines.map(*)).join(','); + my $sexes = ('sex', |@lines.map(*)).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; +} -- cgit