diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2023-11-20 00:13:36 +0100 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2023-11-20 00:13:36 +0100 |
| commit | 2900e0e8619dff5788c4d6c2b0cdf3cd4e0db2b1 (patch) | |
| tree | 380171d2449661c35e11f532c8ff7572970f0c03 /challenge-243 | |
| parent | 6325bf2a0a0c73dcb8868aa4668122955a3213a7 (diff) | |
| download | perlweeklychallenge-club-2900e0e8619dff5788c4d6c2b0cdf3cd4e0db2b1.tar.gz perlweeklychallenge-club-2900e0e8619dff5788c4d6c2b0cdf3cd4e0db2b1.tar.bz2 perlweeklychallenge-club-2900e0e8619dff5788c4d6c2b0cdf3cd4e0db2b1.zip | |
feat: add solutions for challenge 243 from BarrOff
Diffstat (limited to 'challenge-243')
| -rw-r--r-- | challenge-243/barroff/julia/ch-1.jl | 12 | ||||
| -rw-r--r-- | challenge-243/barroff/raku/ch-1.p6 | 20 | ||||
| -rw-r--r-- | challenge-243/barroff/raku/ch-2.p6 | 20 |
3 files changed, 52 insertions, 0 deletions
diff --git a/challenge-243/barroff/julia/ch-1.jl b/challenge-243/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..3e23ec0339 --- /dev/null +++ b/challenge-243/barroff/julia/ch-1.jl @@ -0,0 +1,12 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function reverse_pairs(nums::Vector{T}) where {T<:Integer} + sum(map(x -> length(filter(y -> x[2] > 2 * y, nums[x[1]:end])), enumerate(nums))) +end + +@testset "reverse pairs" begin + @test reverse_pairs([1, 3, 2, 3, 1]) == 2 + @test reverse_pairs([2, 4, 3, 5, 1]) == 3 +end diff --git a/challenge-243/barroff/raku/ch-1.p6 b/challenge-243/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..79458c5f4b --- /dev/null +++ b/challenge-243/barroff/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#!/usr/bin/env raku + +sub reverse-pairs(@nums --> Int) { + grep({ $_[0] > 2 * $_[1] }, combinations(@nums, 2)).elems; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is reverse-pairs([1, 3, 2, 3, 1]), 2, 'Works for (1, 3, 2, 3, 1)'; + is reverse-pairs([2, 4, 3, 5, 1]), 3, 'Works for (2, 4, 3, 5, 1)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(Int @ints) { + my Int @nums = @ints; + say reverse-pairs(@nums); +} diff --git a/challenge-243/barroff/raku/ch-2.p6 b/challenge-243/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..4d313dbd8f --- /dev/null +++ b/challenge-243/barroff/raku/ch-2.p6 @@ -0,0 +1,20 @@ +#!/usr/bin/env raku + +sub floor-sum(@nums where all(@nums) ≥ 1 --> Int) { + sum(map({ $_[0] div $_[1] + $_[1] div $_[0] }, combinations(@nums, 2))) + @nums.elems; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is floor-sum([2, 5, 9]), 10, 'Works for (2, 5, 9)'; + is floor-sum([7, 7, 7, 7, 7, 7, 7]), 49, 'Works for (7, 7, 7, 7, 7, 7, 7)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(Int @ints) { + my Int @nums = @ints; + say floor-sum(@nums); +} |
