diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-21 20:41:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-21 20:41:30 +0100 |
| commit | 38d787e09c4617459640bd62436f20c9cccdc35e (patch) | |
| tree | bcf869d5305e3845f067ee57f9fc809e877ba360 | |
| parent | cd6d19859a9a273da53acc967be5d90819cae2e8 (diff) | |
| parent | 8c4a5abbd2036bbbe79bcaa7a23f6c9071365ceb (diff) | |
| download | perlweeklychallenge-club-38d787e09c4617459640bd62436f20c9cccdc35e.tar.gz perlweeklychallenge-club-38d787e09c4617459640bd62436f20c9cccdc35e.tar.bz2 perlweeklychallenge-club-38d787e09c4617459640bd62436f20c9cccdc35e.zip | |
Merge pull request #9966 from BarrOff/barroff-265
feat: add solutions for challenge 265 from BarrOff
| -rw-r--r-- | challenge-265/barroff/julia/ch-1.jl | 19 | ||||
| -rw-r--r-- | challenge-265/barroff/raku/ch-1.p6 | 26 |
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-265/barroff/julia/ch-1.jl b/challenge-265/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..918418d8dd --- /dev/null +++ b/challenge-265/barroff/julia/ch-1.jl @@ -0,0 +1,19 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function thirtythree_appearance(ints::Vector{T})::T where {T<:Integer} + third = length(ints) / 3 + thirds = filter(x -> count(y -> y == x, ints) ≥ third, unique(ints)) + if length(thirds) > 0 + return minimum(thirds) + end + return NaN + +end + +@testset "33% appearance" begin + @test thirtythree_appearance([1, 2, 3, 3, 3, 3, 4, 2]) == 3 + @test thirtythree_appearance([1, 1]) == 1 + @test thirtythree_appearance([1, 2, 3]) == 1 +end diff --git a/challenge-265/barroff/raku/ch-1.p6 b/challenge-265/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..72d697d65c --- /dev/null +++ b/challenge-265/barroff/raku/ch-1.p6 @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + +use v6.d; + +sub thirtythree-appearance(@ints) { + my %int-bag = @ints.Bag; + my $third = @ints.elems ÷ 3; + my @smallest-appearances = grep({ %int-bag{$_} ≥ $third }, %int-bag.keys); + return @smallest-appearances.min.Int if @smallest-appearances; + return Any; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is thirtythree-appearance([1,2,3,3,3,3,4,2]), 3, 'works for (1,2,3,3,3,3,4,2)'; + is thirtythree-appearance([1, 1]), 1, 'works for (1, 1)'; + is thirtythree-appearance([1, 2, 3]), 1, 'works for (1, 2, 3)'; +} + +#| Take user provided number like 10 1 111 24 1000 +multi sub MAIN(*@ints) { + say thirtythree-appearance(@ints); +} |
