aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarrOff <58253563+BarrOff@users.noreply.github.com>2024-06-09 23:09:56 +0200
committerBarrOff <58253563+BarrOff@users.noreply.github.com>2024-06-09 23:09:56 +0200
commit03a9b960f2507a2dc694c05b1229235940ba65f3 (patch)
treee6211daaeedc29f7be76195c27dfdf079f238bd1
parent14e4db4d669770384019246cd819e0842cc67a4e (diff)
downloadperlweeklychallenge-club-03a9b960f2507a2dc694c05b1229235940ba65f3.tar.gz
perlweeklychallenge-club-03a9b960f2507a2dc694c05b1229235940ba65f3.tar.bz2
perlweeklychallenge-club-03a9b960f2507a2dc694c05b1229235940ba65f3.zip
feat: add solutions for challenge 272 from BarrOff
-rw-r--r--challenge-272/barroff/julia/ch-1.jl12
-rw-r--r--challenge-272/barroff/julia/ch-2.jl13
-rw-r--r--challenge-272/barroff/raku/ch-1.p623
-rw-r--r--challenge-272/barroff/raku/ch-2.p630
4 files changed, 78 insertions, 0 deletions
diff --git a/challenge-272/barroff/julia/ch-1.jl b/challenge-272/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..f594cc0fdb
--- /dev/null
+++ b/challenge-272/barroff/julia/ch-1.jl
@@ -0,0 +1,12 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function defang_ip_address(ip::AbstractString)::String
+ replace(ip, "." => "[.]")
+end
+
+@testset "defang IP address" begin
+ @test defang_ip_address("1.1.1.1") == "1[.]1[.]1[.]1"
+ @test defang_ip_address("255.101.1.0") == "255[.]101[.]1[.]0"
+end
diff --git a/challenge-272/barroff/julia/ch-2.jl b/challenge-272/barroff/julia/ch-2.jl
new file mode 100644
index 0000000000..513b5e03d5
--- /dev/null
+++ b/challenge-272/barroff/julia/ch-2.jl
@@ -0,0 +1,13 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function string_score(str::AbstractString)::Int
+ sum([abs(str[i] - str[i + 1]) for i in 1:lastindex(str) - 1])
+end
+
+@testset "string score" begin
+ @test string_score("hello") == 13
+ @test string_score("perl") == 30
+ @test string_score("raku") == 37
+end
diff --git a/challenge-272/barroff/raku/ch-1.p6 b/challenge-272/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..c3009b3171
--- /dev/null
+++ b/challenge-272/barroff/raku/ch-1.p6
@@ -0,0 +1,23 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub defang-IP-address(Str $ip --> Str) {
+ $ip.subst(".", "[.]", :g)
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 2;
+
+ is defang-IP-address("1.1.1.1"), "1[.]1[.]1[.]1",
+ 'works for 1.1.1.1';
+ is defang-IP-address("255.101.1.0"), "255[.]101[.]1[.]0",
+ 'works for 255.101.1.0';
+}
+
+#| Take user provided ip like 1.1.1.1
+multi sub MAIN(Str $ip) {
+ say defang-IP-address($ip);
+}
diff --git a/challenge-272/barroff/raku/ch-2.p6 b/challenge-272/barroff/raku/ch-2.p6
new file mode 100644
index 0000000000..0e7ac3bdea
--- /dev/null
+++ b/challenge-272/barroff/raku/ch-2.p6
@@ -0,0 +1,30 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub string-score(Str $str --> Int) {
+ my @ascii-values = $str.ords;
+ sum(
+ map(
+ { abs(
+ @ascii-values[$_] - @ascii-values[$_ + 1]
+ ) },
+ 0..$str.chars - 2
+ )
+ );
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is string-score("hello"), 13, 'works for "hello"';
+ is string-score("perl"), 30, 'works for "perl"';
+ is string-score("raku"), 37, 'works for "raku"';
+}
+
+#| Take user provided string like "hello"
+multi sub MAIN(Str $str) {
+ say string-score($str);
+}