diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-11-10 23:47:28 +0100 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-11-10 23:47:28 +0100 |
| commit | b31a61c4c8ab3d6fa5f9c148c7633b2565f05a23 (patch) | |
| tree | efca98335fd33d5933fa806030c77adb67e4bd60 | |
| parent | edd302b3027d93e8bac504487cbabaca862e906a (diff) | |
| download | perlweeklychallenge-club-b31a61c4c8ab3d6fa5f9c148c7633b2565f05a23.tar.gz perlweeklychallenge-club-b31a61c4c8ab3d6fa5f9c148c7633b2565f05a23.tar.bz2 perlweeklychallenge-club-b31a61c4c8ab3d6fa5f9c148c7633b2565f05a23.zip | |
feat: add solutions for challenge 294 from BarrOff
| -rw-r--r-- | challenge-294/barroff/julia/ch-1.jl | 27 | ||||
| -rw-r--r-- | challenge-294/barroff/nim/ch_1.nim | 37 | ||||
| -rw-r--r-- | challenge-294/barroff/raku/ch-1.p6 | 42 |
3 files changed, 106 insertions, 0 deletions
diff --git a/challenge-294/barroff/julia/ch-1.jl b/challenge-294/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..b84f2f1dae --- /dev/null +++ b/challenge-294/barroff/julia/ch-1.jl @@ -0,0 +1,27 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function consecutive_sequence(ints::Vector{T})::Int where {T<:Integer} + (mn, mx) = extrema(ints) + spots = falses(mx + 1 - mn) + map(x -> spots[x+1-mn] = 1, ints) + longestSeq = 1 + currentSeq = 0 + for x in spots + if x == 1 + currentSeq += 1 + elseif currentSeq != 0 + longestSeq = max(longestSeq, currentSeq) + currentSeq = 0 + end + end + longestSeq = max(longestSeq, currentSeq) + return longestSeq == 1 ? -1 : longestSeq +end + +@testset "consecutive sequence" begin + @test consecutive_sequence([10, 4, 20, 1, 3, 2]) == 4 + @test consecutive_sequence([0, 6, 1, 8, 5, 2, 4, 3, 0, 7]) == 9 + @test consecutive_sequence([10, 20, 30]) == -1 +end diff --git a/challenge-294/barroff/nim/ch_1.nim b/challenge-294/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..580e40c99d --- /dev/null +++ b/challenge-294/barroff/nim/ch_1.nim @@ -0,0 +1,37 @@ +import std/unittest + +from std/sequtils import minmax, repeat + +# run tests with following command: +# nim c -r ch_1.nim + +proc consequtive_sequence[T: SomeInteger](ints: openArray[T]): int = + let + (mn, mx) = minmax(ints) + var + currentSeq = 0 + longestSeq = 1 + spots: seq[bool] = repeat(false, mx + 1 - mn) + + for x in ints: + spots[x - mn] = true + for x in spots: + if x: + inc(currentSeq) + elif currentSeq != 0: + longestSeq = max(longestSeq, currentSeq) + currentSeq = 0 + + longestSeq = max(longestSeq, currentSeq) + return if longestSeq == 1: -1 else: longestSeq + + +suite "consecutive sequence": + test "[10, 4, 20, 1, 3, 2]": + check(consequtive_sequence([10, 4, 20, 1, 3, 2]) == 4) + + test "[0, 6, 1, 8, 5, 2, 4, 3, 0, 7]": + check(consequtive_sequence([0, 6, 1, 8, 5, 2, 4, 3, 0, 7]) == 9) + + test "[10, 30 , 20]": + check(consequtive_sequence([10, 30, 20]) == -1) diff --git a/challenge-294/barroff/raku/ch-1.p6 b/challenge-294/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..567ea3750b --- /dev/null +++ b/challenge-294/barroff/raku/ch-1.p6 @@ -0,0 +1,42 @@ +#!/usr/bin/env raku + +use v6.d; + +sub consecutive-sequence(@ints --> Int) { + my ($minimum, $maximum) = @ints.minmax.minmax; + my Bool @spots[$maximum + 1 - $minimum]; + @spots[0..@spots.elems - 1] = (loop { False }); + map({ @spots[$_ - $minimum] = True }, @ints); + my Int $longest-seq = 1; + my Int $current-seq = 0; + for @spots { + if $_ { + $current-seq++; + } elsif $current-seq ≠ 0 { + $longest-seq = max($current-seq, $longest-seq); + $current-seq = 0; + } + } + $longest-seq = max($longest-seq, $current-seq); + return $longest-seq == 1 ?? -1 !! $longest-seq; + + +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is consecutive-sequence([10, 4, 20, 1, 3, 2]), 4, + 'Works for ([10, 4, 20, 1, 3, 2])'; + is consecutive-sequence([0, 6, 1, 8, 5, 2, 4, 3, 0, 7]), 9, + 'Works for ([0, 6, 1, 8, 5, 2, 4, 3, 0, 7])'; + is consecutive-sequence([10, 20, 30]), -1, 'Works for ([10, 20, 30])'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(*@ints) { + say consecutive-sequence(@ints); +} + |
