From 9d8a12156df742b9fbfecf87a6b13f8a34985b99 Mon Sep 17 00:00:00 2001 From: BarrOff <58253563+BarrOff@users.noreply.github.com> Date: Sun, 26 Nov 2023 23:14:33 +0100 Subject: feat: add solutions for challenge 244 from BarrOff --- challenge-244/barroff/julia/ch-1.jl | 14 ++++++++++++++ challenge-244/barroff/perl/ch-1.pl | 36 ++++++++++++++++++++++++++++++++++++ challenge-244/barroff/raku/ch-1.p6 | 22 ++++++++++++++++++++++ challenge-244/barroff/raku/ch-2.p6 | 19 +++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 challenge-244/barroff/julia/ch-1.jl create mode 100644 challenge-244/barroff/perl/ch-1.pl create mode 100644 challenge-244/barroff/raku/ch-1.p6 create mode 100644 challenge-244/barroff/raku/ch-2.p6 diff --git a/challenge-244/barroff/julia/ch-1.jl b/challenge-244/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..810c4f32cf --- /dev/null +++ b/challenge-244/barroff/julia/ch-1.jl @@ -0,0 +1,14 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function count_smaller(nums::Vector{T}) where {T<:Integer} + positions = Dict(reverse(reverse.(enumerate(sort(nums))))) + [positions[x] - 1 for x in nums] +end + +@testset "reverse pairs" begin + @test count_smaller([8, 1, 2, 2, 3]) == [4, 0, 1, 1, 3] + @test count_smaller([6, 5, 4, 8]) == [2, 1, 0, 3] + @test count_smaller([2, 2, 2]) == [0, 0, 0] +end diff --git a/challenge-244/barroff/perl/ch-1.pl b/challenge-244/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..ca3ca46a1d --- /dev/null +++ b/challenge-244/barroff/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use v5.38; + +use Data::Dumper; + +sub count_smaller (@ints) { + my @inner_ints = sort(@ints); + my %positions; + while ( my ( $i, $v ) = each(@inner_ints) ) { + $positions{$v} = $i unless exists $positions{$v}; + } + my @res = @positions{@ints}; + return \@res; +} + +sub MAIN() { + if (@ARGV) { + + #| Run command line arguments + say @{ count_smaller(@ARGV) }; + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 3; + + is count_smaller( 8, 1, 2, 2, 3 ), [ 4, 0, 1, 1, 3 ], + 'works for (8, 1, 2, 2, 3)'; + is count_smaller( 6, 5, 4, 8 ), [ 2, 1, 0, 3 ], + 'works for (6, 5, 4, 8)'; + is count_smaller( 2, 2, 2 ), [ 0, 0, 0 ], 'works for (2, 2, 2)'; + } +} + +MAIN(); diff --git a/challenge-244/barroff/raku/ch-1.p6 b/challenge-244/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..022290196d --- /dev/null +++ b/challenge-244/barroff/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#!/usr/bin/env raku + +sub count-smaller(@nums --> List) { + my %positions = @nums.sort.kv.reverse.hash; + %positions{ @nums }; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is count-smaller([8, 1, 2, 2, 3]), (4, 0, 1, 1, 3), 'Works for (8, 1, 2, 2, 3)'; + is count-smaller([6, 5, 4, 8]), (2, 1, 0, 3), 'Works for (6, 5, 4, 8)'; + is count-smaller([2, 2, 2]), (0, 0, 0), 'Works for (2, 2, 2)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(Int @ints) { + my Int @nums = @ints; + say count-smaller(@nums); +} diff --git a/challenge-244/barroff/raku/ch-2.p6 b/challenge-244/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..b4633271c1 --- /dev/null +++ b/challenge-244/barroff/raku/ch-2.p6 @@ -0,0 +1,19 @@ +#!/usr/bin/env raku + +sub group-hero(@nums --> Int) { + sum(map({ (max($_) ** 2) * min($_) }, combinations(@nums)[1..*-1])) +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 1; + + is group-hero(( 2, 1, 4 )), 141, 'Works for (2, 1, 4)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(Int @ints) { + my Int @nums = @ints; + say group-hero(@nums); +} -- cgit