diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-06-02 22:41:57 +0200 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-06-02 22:41:57 +0200 |
| commit | 02d36cf8af1f61fa1b6a39c1d539c910643bb14e (patch) | |
| tree | f9a28796b03b5d2cb06aaddc8d0a8c21f9168a6c | |
| parent | 5ae5dae1606d672f5123901550717cdb5ec72921 (diff) | |
| download | perlweeklychallenge-club-02d36cf8af1f61fa1b6a39c1d539c910643bb14e.tar.gz perlweeklychallenge-club-02d36cf8af1f61fa1b6a39c1d539c910643bb14e.tar.bz2 perlweeklychallenge-club-02d36cf8af1f61fa1b6a39c1d539c910643bb14e.zip | |
feat: add solutions for challenge 271 from BarrOff
| -rw-r--r-- | challenge-271/barroff/julia/ch-1.jl | 23 | ||||
| -rw-r--r-- | challenge-271/barroff/julia/ch-2.jl | 25 | ||||
| -rw-r--r-- | challenge-271/barroff/raku/ch-1.p6 | 25 | ||||
| -rw-r--r-- | challenge-271/barroff/raku/ch-2.p6 | 25 |
4 files changed, 98 insertions, 0 deletions
diff --git a/challenge-271/barroff/julia/ch-1.jl b/challenge-271/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..3300517152 --- /dev/null +++ b/challenge-271/barroff/julia/ch-1.jl @@ -0,0 +1,23 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function maximum_ones(m::Matrix{T})::Int where {T<:Integer} + findmax(sum(m; dims=2))[1] +end + +@testset "maximum ones" begin + @test maximum_ones( + [0 1; + 1 0 + ]) == 1 + @test maximum_ones( + [0 0 0; + 1 0 1 + ]) == 2 + @test maximum_ones( + [0 0; + 1 1; + 0 0 + ]) == 2 +end diff --git a/challenge-271/barroff/julia/ch-2.jl b/challenge-271/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..854b0608be --- /dev/null +++ b/challenge-271/barroff/julia/ch-2.jl @@ -0,0 +1,25 @@ +using Base: bitcount +#!/usr/bin/env julia + +using Test: @test, @testset + +function sort_by_1_bits(ints::Vector{T})::Vector{T} where {T<:Integer} + bc = Dict{T,Int}( + x => sum( + y -> parse(Int, y), + split( + string(x, base=2), + "" + ) + ) for x in ints + ) + sort( + collect(keys(bc)), + lt=(x, y) -> bc[x] == bc[y] ? x < y : bc[x] < bc[y] + ) +end + +@testset "sort by 1 bits" begin + @test sort_by_1_bits(collect(0:8)) == [0, 1, 2, 4, 8, 3, 5, 6, 7] + @test sort_by_1_bits([2^x for x in 10:-1:6]) == [2^x for x in 6:10] +end diff --git a/challenge-271/barroff/raku/ch-1.p6 b/challenge-271/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..7799ad28ca --- /dev/null +++ b/challenge-271/barroff/raku/ch-1.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub maximum-ones(@matrix --> Int) { + my @sums = map({ .sum }, @matrix); + 1 + @sums.first: * == max(@sums), :k; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is maximum-ones([ [0, 1], + [1, 0], + ]), 1, 'works for matrix one'; + is maximum-ones([ [0, 0, 0], + [1, 0, 1], + ]), 2, 'works for matrix two'; + is maximum-ones([ [0, 0], + [1, 1], + [0, 0], + ]), 2, 'works for matrix three'; +} diff --git a/challenge-271/barroff/raku/ch-2.p6 b/challenge-271/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..83a1b9f5d6 --- /dev/null +++ b/challenge-271/barroff/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub sort-by_1-bits(@ints --> Positional) { + my (%bc, @res); + map({ push(%bc{$_.base(2).comb.sum}, $_) }, @ints); + map({ @res.append(sort(%bc{$_})) }, sort(keys(%bc))); + return @res; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is sort-by_1-bits([0..8]), [0, 1, 2, 4, 8, 3, 5, 6, 7], 'works for [0..8]'; + is sort-by_1-bits([1024, 512, 256, 128, 64]), [64, 128, 256, 512, 1024], + 'works for [1024, 512, 256, 128, 64]'; +} + +#| Take user provided number like 1024 512 256 128 64 +multi sub MAIN(*@ints) { + say sort-by_1-bits(@ints); +} |
