diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-04-07 22:28:33 +0200 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-04-07 22:28:33 +0200 |
| commit | 0d15af8d561aa1ea1bed9aabca6c502d115adac2 (patch) | |
| tree | 164b668117e17ac513e6f2ea3eba949e26156d96 /challenge-263 | |
| parent | 0495c7534995083a63370f8e93d3834bbe1f054d (diff) | |
| download | perlweeklychallenge-club-0d15af8d561aa1ea1bed9aabca6c502d115adac2.tar.gz perlweeklychallenge-club-0d15af8d561aa1ea1bed9aabca6c502d115adac2.tar.bz2 perlweeklychallenge-club-0d15af8d561aa1ea1bed9aabca6c502d115adac2.zip | |
feat: add solutions for challenge 263 from BarrOff
Diffstat (limited to 'challenge-263')
| -rw-r--r-- | challenge-263/barroff/bqn/ch-1.bqn | 7 | ||||
| -rw-r--r-- | challenge-263/barroff/julia/ch-1.jl | 13 | ||||
| -rw-r--r-- | challenge-263/barroff/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-263/barroff/raku/ch-1.p6 | 24 | ||||
| -rw-r--r-- | challenge-263/barroff/raku/ch-2.p6 | 25 |
5 files changed, 93 insertions, 0 deletions
diff --git a/challenge-263/barroff/bqn/ch-1.bqn b/challenge-263/barroff/bqn/ch-1.bqn new file mode 100644 index 0000000000..e98f6bed71 --- /dev/null +++ b/challenge-263/barroff/bqn/ch-1.bqn @@ -0,0 +1,7 @@ +#/usr/bin/env bqn + +TargetIndex ← { ((𝕨⊸=∧)/(↕≠))𝕩 } + +•Show 2 TargetIndex 1‿5‿3‿2‿4‿2 +•Show 6 TargetIndex 1‿2‿4‿3‿5 +•Show 4 TargetIndex 5‿3‿2‿4‿2‿1 diff --git a/challenge-263/barroff/julia/ch-1.jl b/challenge-263/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..4ecbb63549 --- /dev/null +++ b/challenge-263/barroff/julia/ch-1.jl @@ -0,0 +1,13 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function target_index(k::T, ints::Vector{T}) where {T<:Integer} + findall(x -> x == k, sort(ints)) .- 1 +end + +@testset "target index" begin + @test target_index(2, [1, 5, 3, 2, 4, 2]) == [1, 2] + @test target_index(6, [1, 2, 4, 3, 5]) == [] + @test target_index(4, [5, 3, 2, 4, 2, 1]) == [4] +end diff --git a/challenge-263/barroff/perl/ch-1.pl b/challenge-263/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..67714a3b8a --- /dev/null +++ b/challenge-263/barroff/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env raku + +use v6.d; + +sub target-index(Int:D $k, @ints --> Positional) { + indices(@ints.sort.join, $k) +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is target-index(2, [1, 5, 3, 2, 4, 2]), (1, 2), + 'works for 2 and (1, 5, 3, 2, 4, 2)'; + is target-index(6, [1, 2, 4, 3, 5]), (), 'works for 6 and (1, 2, 4, 3, 5)'; + is target-index(4, [5, 3, 2, 4, 2, 1]), (4), + 'works for 4 and (5, 3, 2, 4, 2, 1)'; +} + +#| Take user provided number like 10 1 111 24 1000 +multi sub MAIN(Int:D $k, *@ints) { + say target-index($k, @ints); +} diff --git a/challenge-263/barroff/raku/ch-1.p6 b/challenge-263/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..67714a3b8a --- /dev/null +++ b/challenge-263/barroff/raku/ch-1.p6 @@ -0,0 +1,24 @@ +#!/usr/bin/env raku + +use v6.d; + +sub target-index(Int:D $k, @ints --> Positional) { + indices(@ints.sort.join, $k) +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is target-index(2, [1, 5, 3, 2, 4, 2]), (1, 2), + 'works for 2 and (1, 5, 3, 2, 4, 2)'; + is target-index(6, [1, 2, 4, 3, 5]), (), 'works for 6 and (1, 2, 4, 3, 5)'; + is target-index(4, [5, 3, 2, 4, 2, 1]), (4), + 'works for 4 and (5, 3, 2, 4, 2, 1)'; +} + +#| Take user provided number like 10 1 111 24 1000 +multi sub MAIN(Int:D $k, *@ints) { + say target-index($k, @ints); +} diff --git a/challenge-263/barroff/raku/ch-2.p6 b/challenge-263/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..0c55055914 --- /dev/null +++ b/challenge-263/barroff/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub merge-items(@items1, @items2 --> Positional) { + my %dict; + my @res; + map({ %dict{ $_[0] } += $_[1] }, @items1); + map({ %dict{ $_[0] } += $_[1] }, @items2); + map({ @res.push([$_.Int, %dict{$_}]) }, %dict.keys); + return @res; +} + +#| Run test cases +multi sub MAIN() { + use Test; + plan 3; + + is sort(merge-items([ [1,1], [2,1], [3,2] ], [ [2,2], [1,3] ])), + [ [1,4], [2,3], [3,2] ], 'works for [ [1,4], [2,3], [3,2] ]'; + is sort(merge-items([ [1,2], [2,3], [1,3], [3,2] ], [ [3,1], [1,3] ])), + [ [1,8], [2,3], [3,3] ], 'works for [ [1,8], [2,3], [3,3] ]'; + is sort(merge-items([ [1,1], [2,2], [3,3] ], [ [2,3], [2,4] ])), + [ [1,1], [2,9], [3,3] ], 'works for [ [1,1], [2,9], [3,3] ]'; +} |
