aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-29 02:18:18 +0000
committerGitHub <noreply@github.com>2024-01-29 02:18:18 +0000
commit59ca57d90b96d74296f55d67c014da32e38ee29e (patch)
tree8acf0c461d5c25720e88842ce87b0a1879d206dc
parentf193e95f770bb46d18c1a69d507e33cfc574622f (diff)
parent2ceb364657318354184f5cc5eb895dc13e63afc8 (diff)
downloadperlweeklychallenge-club-59ca57d90b96d74296f55d67c014da32e38ee29e.tar.gz
perlweeklychallenge-club-59ca57d90b96d74296f55d67c014da32e38ee29e.tar.bz2
perlweeklychallenge-club-59ca57d90b96d74296f55d67c014da32e38ee29e.zip
Merge pull request #9477 from BarrOff/barroff-253
feat: add solutions for challenge 253 from BarrOff
-rw-r--r--challenge-253/barroff/julia/ch-1.jl17
-rw-r--r--challenge-253/barroff/nim/ch_1.nim29
-rw-r--r--challenge-253/barroff/perl/ch-1.pl31
-rw-r--r--challenge-253/barroff/raku/ch-1.p624
4 files changed, 101 insertions, 0 deletions
diff --git a/challenge-253/barroff/julia/ch-1.jl b/challenge-253/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..7f8a4c3133
--- /dev/null
+++ b/challenge-253/barroff/julia/ch-1.jl
@@ -0,0 +1,17 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function split_strings(separator::Char, words::Vector{T}) where {T<:AbstractString}
+ return collect(
+ Iterators.flatten([
+ filter(x -> lastindex(x) > 0, x) for x in map(x -> split(x, separator), words)
+ ]),
+ )
+end
+
+@testset "split strings" begin
+ @test split_strings('.', ["one.two.three", "four.five", "six"]) ==
+ ["one", "two", "three", "four", "five", "six"]
+ @test split_strings('$', [raw"$perl$$", raw"$$raku$"]) == ["perl", "raku"]
+end
diff --git a/challenge-253/barroff/nim/ch_1.nim b/challenge-253/barroff/nim/ch_1.nim
new file mode 100644
index 0000000000..19daff58c5
--- /dev/null
+++ b/challenge-253/barroff/nim/ch_1.nim
@@ -0,0 +1,29 @@
+import std/unittest
+
+from std/strutils import split
+
+# run tests with following command:
+# nim c -r ch_1.nim
+
+proc split_strings(separator: char, words: openArray[string]): seq[string] =
+ for word in words:
+ for s in split(word, separator):
+ if len(s) > 0:
+ result.add(s)
+
+
+suite "split strings":
+ test """("one.two.three","four.five","six")""":
+ let
+ words = ["one.two.three", "four.five", "six"]
+ separator = '.'
+
+ check(split_strings(separator, words) == @["one", "two", "three", "four",
+ "five", "six"])
+
+ test """("$perl$$", "$$raku$")""":
+ let
+ words = ["$perl$$", "$$raku$"]
+ separator = '$'
+
+ check(split_strings(separator, words) == @["perl", "raku"])
diff --git a/challenge-253/barroff/perl/ch-1.pl b/challenge-253/barroff/perl/ch-1.pl
new file mode 100644
index 0000000000..565cf93d1a
--- /dev/null
+++ b/challenge-253/barroff/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+sub split_strings ( $separator, @words ) {
+ my @res = grep( { length($_) > 0 }
+ map( { split( /\Q$separator\E/, $_ ) } @words ) );
+
+ return \@res;
+}
+
+sub MAIN() {
+ if (@ARGV) {
+
+ #| Run on command line argument
+ say split_strings( $ARGV[1], @ARGV[ 2 .. -1 ] );
+ }
+ else {
+ #| Run test cases
+ use Test2::V0 qw( is plan );
+ plan 2;
+
+ is split_strings( ".", ( "one.two.three", "four.five", "six" ) ),
+ [ "one", "two", "three", "four", "five", "six" ],
+ 'works for ("one.two.three","four.five","six")';
+ is split_strings( '$', ( '$perl$$', '$$raku$' ) ), [ "perl", "raku" ],
+ 'works for ("$perl$$", "$$raku$")';
+ }
+}
+
+MAIN();
diff --git a/challenge-253/barroff/raku/ch-1.p6 b/challenge-253/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..722a65a595
--- /dev/null
+++ b/challenge-253/barroff/raku/ch-1.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub split-strings(Str $separator, @words --> Seq) {
+ map({ split($separator, $_, :skip-empty).Slip}, @words);
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 2;
+
+ is split-strings(".", ("one.two.three","four.five","six")),
+ ("one","two","three","four","five","six"),
+ 'works for ("one.two.three","four.five","six")';
+ is split-strings('$', ('$perl$$', '$$raku$')), ("perl","raku"),
+ 'works for ("$perl$$", "$$raku$")';
+}
+
+#| Take user provided list like aba aabb abcd bac aabc
+multi sub MAIN(*@s) {
+ say split-strings(@s[0], @s[1..*]);
+}