diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-07-07 22:35:50 +0200 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-07-07 22:35:50 +0200 |
| commit | b200bac9b6facb92ed2f8628c3a887b4498cb896 (patch) | |
| tree | 0602b3cb540cdb0a9b8098a7de60116b04e1c8ef /challenge-276 | |
| parent | f522709dab87be43f3b916321df600b44c3fd3c3 (diff) | |
| download | perlweeklychallenge-club-b200bac9b6facb92ed2f8628c3a887b4498cb896.tar.gz perlweeklychallenge-club-b200bac9b6facb92ed2f8628c3a887b4498cb896.tar.bz2 perlweeklychallenge-club-b200bac9b6facb92ed2f8628c3a887b4498cb896.zip | |
feat: add solutions for challenge 276 from BarrOff
Diffstat (limited to 'challenge-276')
| -rw-r--r-- | challenge-276/barroff/julia/ch-1.jl | 18 | ||||
| -rw-r--r-- | challenge-276/barroff/julia/ch-2.jl | 18 | ||||
| -rw-r--r-- | challenge-276/barroff/raku/ch-1.p6 | 22 | ||||
| -rw-r--r-- | challenge-276/barroff/raku/ch-2.p6 | 26 |
4 files changed, 84 insertions, 0 deletions
diff --git a/challenge-276/barroff/julia/ch-1.jl b/challenge-276/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..07e6f3d9c1 --- /dev/null +++ b/challenge-276/barroff/julia/ch-1.jl @@ -0,0 +1,18 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function complete_day(hours::Vector{T})::Int where {T<:Integer} + length( + filter( + x -> x % 24 == 0, + [hours[x] + hours[y] for x in 1:length(hours)-1 for y in x+1:length(hours)] + ) + ) +end + +@testset "complete day" begin + @test complete_day([12, 12, 30, 24, 24]) == 2 + @test complete_day([72, 48, 24, 5]) == 3 + @test complete_day([12, 18, 24]) == 0 +end diff --git a/challenge-276/barroff/julia/ch-2.jl b/challenge-276/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..59b8655030 --- /dev/null +++ b/challenge-276/barroff/julia/ch-2.jl @@ -0,0 +1,18 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function maximum_frequency(ints::Vector{T})::Int where {T<:Integer} + numbercounts = Dict{T,Int}() + map(x -> numbercounts[x] = get(numbercounts, x, 0) + 1, ints) + most_occurrences = maximum(values(numbercounts)) + return count( + x -> numbercounts[x] == most_occurrences, + keys(numbercounts) + ) * most_occurrences +end + +@testset "maxumum frequency" begin + @test maximum_frequency([1, 2, 2, 4, 1, 5]) == 4 + @test maximum_frequency(collect(1:5)) == 5 +end diff --git a/challenge-276/barroff/raku/ch-1.p6 b/challenge-276/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..c47d11ed8a --- /dev/null +++ b/challenge-276/barroff/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#!/usr/bin/env raku + +use v6.d; + +sub complete-day(@hours --> Int) { + grep({ sum($_) mod 24 == 0 }, @hours.combinations: 2).elems; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is complete-day([12, 12, 30, 24, 24]), 2, 'works for [12, 12, 30, 24, 24]'; + is complete-day([72, 48, 24, 5]), 3, 'works for [72, 48, 24, 5]'; + is complete-day([12, 18, 24]), 0, 'works for [12, 18, 24]'; +} + +#| Take user provided number like 1024 512 256 128 64 +multi sub MAIN(*@hours) { + say complete-day(@hours); +} diff --git a/challenge-276/barroff/raku/ch-2.p6 b/challenge-276/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..1d8745ad25 --- /dev/null +++ b/challenge-276/barroff/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + +use v6.d; + +sub maximum-frequency(@ints --> Int) { + my %ints-bag = Bag(@ints); + my $maximum-occurrence = max(values(%ints-bag)); + grep( + { %ints-bag{$_} == $maximum-occurrence }, + keys(%ints-bag) + ).elems * $maximum-occurrence; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is maximum-frequency([1, 2, 2, 4, 1, 5]), 4, 'works for [1, 2, 2, 4, 1, 5]'; + is maximum-frequency([1, 2, 3, 4, 5]), 5, 'works for [1, 2, 3, 4, 5]'; +} + +#| Take user provided number like 1024 512 256 128 64 +multi sub MAIN(*@ints) { + say maximum-frequency(@ints); +} |
