diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2025-01-19 23:36:49 +0100 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2025-01-19 23:36:49 +0100 |
| commit | 871f88be7f0c898d452c5c4a160d205f93850745 (patch) | |
| tree | 4332401f9bd0ea285b4a79fe618ab6973545e790 | |
| parent | 3aa4260caf45fc1b7deea5261df4ba7c71e02c6b (diff) | |
| download | perlweeklychallenge-club-871f88be7f0c898d452c5c4a160d205f93850745.tar.gz perlweeklychallenge-club-871f88be7f0c898d452c5c4a160d205f93850745.tar.bz2 perlweeklychallenge-club-871f88be7f0c898d452c5c4a160d205f93850745.zip | |
feat: add solutions for challenge 304 from BarrOff
| -rw-r--r-- | challenge-304/barroff/julia/ch-1.jl | 13 | ||||
| -rw-r--r-- | challenge-304/barroff/julia/ch-2.jl | 16 | ||||
| -rw-r--r-- | challenge-304/barroff/nim/ch_1.nim | 24 | ||||
| -rw-r--r-- | challenge-304/barroff/nim/ch_2.nim | 22 | ||||
| -rw-r--r-- | challenge-304/barroff/pascal/ch1.lpi | 55 | ||||
| -rw-r--r-- | challenge-304/barroff/pascal/ch1.lpr | 131 | ||||
| -rw-r--r-- | challenge-304/barroff/pascal/ch1.lps | 23 | ||||
| -rw-r--r-- | challenge-304/barroff/raku/ch-1.p6 | 25 | ||||
| -rw-r--r-- | challenge-304/barroff/raku/ch-2.p6 | 22 |
9 files changed, 331 insertions, 0 deletions
diff --git a/challenge-304/barroff/julia/ch-1.jl b/challenge-304/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..b3b071650f --- /dev/null +++ b/challenge-304/barroff/julia/ch-1.jl @@ -0,0 +1,13 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function arrangeBinaries(digits::Vector{Int}, n::Int)::Bool + numberOfOnes = count(x -> x == 1, digits) + n + return numberOfOnes ≤ length(digits) - numberOfOnes + 1 +end + +@testset "arrange binary" begin + @test arrangeBinaries([1, 0, 0, 0, 1], 1) + @test !(arrangeBinaries([1, 0, 0, 0, 1], 2)) +end diff --git a/challenge-304/barroff/julia/ch-2.jl b/challenge-304/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..623b72e34d --- /dev/null +++ b/challenge-304/barroff/julia/ch-2.jl @@ -0,0 +1,16 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function maximumAverage(ints::Vector{Int}, n::Int)::Float64 + return maximum( + map( + x -> sum(ints[x:x+n-1]), + 1:length(ints)-n+1) + ) / n +end + +@testset "maximum average" begin + @test maximumAverage([1, 12, -5, -6, 50, 3], 4) == 12.75 + @test maximumAverage([5], 1) == 5 +end diff --git a/challenge-304/barroff/nim/ch_1.nim b/challenge-304/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..7bacf28ac0 --- /dev/null +++ b/challenge-304/barroff/nim/ch_1.nim @@ -0,0 +1,24 @@ +import std/unittest + +from std/math import sum +from std/sequtils import count + +# run tests with following command: +# nim r ch_1.nim + +type + Bin = range[0..1] + +proc arrangeBinary(digits: openArray[Bin], n: int): bool = + let + countOfZeros: int = count(digits, 0) - n + countOfOnes: int = len(digits) - countOfZeros + + result = countOfZeros + 1 >= countOfOnes + +suite "arrange binary": + test "[1, 0, 0, 0, 1], 1": + check(arrangeBinary([Bin(1), 0, 0, 0, 1], 1)) + + test "[1, 0, 0, 0, 1], 2": + check(not(arrangeBinary([Bin(1), 0, 0, 0, 1], 2))) diff --git a/challenge-304/barroff/nim/ch_2.nim b/challenge-304/barroff/nim/ch_2.nim new file mode 100644 index 0000000000..a4a16a8d8a --- /dev/null +++ b/challenge-304/barroff/nim/ch_2.nim @@ -0,0 +1,22 @@ +import std/[sugar, unittest] + +from std/math import sum +from std/sequtils import map, toSeq + +# run tests with following command: +# nim r ch_1.nim + +proc maximumAverage(ints: openArray[int], n: int): float = + var + s: int = 0 + + for i in 0..len(ints) - n: + s = sum(ints[i..i+n-1]) + result = max(result, s / n) + +suite "maximum average": + test "[1, 12, -5, -6, 50, 3], 4": + check(maximumAverage([1, 12, -5, -6, 50, 3], 4) == 12.75) + + test "[5], 1": + check(maximumAverage([5], 1) == 5) diff --git a/challenge-304/barroff/pascal/ch1.lpi b/challenge-304/barroff/pascal/ch1.lpi new file mode 100644 index 0000000000..902bb2d478 --- /dev/null +++ b/challenge-304/barroff/pascal/ch1.lpi @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="UTF-8"?> +<CONFIG> + <ProjectOptions> + <Version Value="12"/> + <General> + <Flags> + <MainUnitHasCreateFormStatements Value="False"/> + <MainUnitHasScaledStatement Value="False"/> + </Flags> + <SessionStorage Value="InProjectDir"/> + <Title Value="ArrangeBinary"/> + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <BuildModes> + <Item Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + <UseFileFilters Value="True"/> + </PublishOptions> + <RunParams> + <FormatVersion Value="2"/> + </RunParams> + <Units> + <Unit> + <Filename Value="ch1.lpr"/> + <IsPartOfProject Value="True"/> + </Unit> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <Target> + <Filename Value="ch1"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> + </SearchPaths> + </CompilerOptions> + <Debugging> + <Exceptions> + <Item> + <Name Value="EAbort"/> + </Item> + <Item> + <Name Value="ECodetoolError"/> + </Item> + <Item> + <Name Value="EFOpenError"/> + </Item> + </Exceptions> + </Debugging> +</CONFIG> diff --git a/challenge-304/barroff/pascal/ch1.lpr b/challenge-304/barroff/pascal/ch1.lpr new file mode 100644 index 0000000000..66fe190dd6 --- /dev/null +++ b/challenge-304/barroff/pascal/ch1.lpr @@ -0,0 +1,131 @@ +program ch1; + +{$mode objfpc}{$H+} + +uses + {$IFDEF UNIX} + cthreads, + {$ENDIF} + Classes, SysUtils, CustApp + { you can add units after this }; + +type + + { TArrangeBinary } + + TArrangeBinary = class(TCustomApplication) + protected + procedure DoRun; override; + public + constructor Create(TheOwner: TComponent); override; + destructor Destroy; override; + procedure WriteHelp; virtual; + procedure TestApplication; virtual; + function ab: Boolean; virtual; + function ab(Digits: Array of Integer; n: Integer): Boolean; virtual; + end; + +{ TArrangeBinary } + +procedure TArrangeBinary.DoRun; +var + ErrorMsg: String; +begin + // quick check parameters + ErrorMsg:=CheckOptions('ht', ['help', 'test']); + if ErrorMsg<>'' then begin + ShowException(Exception.Create(ErrorMsg)); + Terminate; + Exit; + end; + + // parse parameters + if HasOption('h', 'help') then begin + WriteHelp; + Terminate; + Exit; + end; + + if HasOption('t', 'test') then begin + TestApplication; + Terminate; + Exit; + end; + + { add your program here } + + if ParamCount > 0 then + writeln(ab); + + // stop program loop + Terminate; +end; + +constructor TArrangeBinary.Create(TheOwner: TComponent); +begin + inherited Create(TheOwner); + StopOnException:=True; +end; + +destructor TArrangeBinary.Destroy; +begin + inherited Destroy; +end; + +procedure TArrangeBinary.WriteHelp; +begin + { add your help code here } + writeln('Usage: ', ExeName, ' -h'); +end; + +function TArrangeBinary.ab: Boolean; +var + i, countOfZeros: Integer; +begin + countOfZeros := - StrToInt(Params[ParamCount]);; + for i := 1 to ParamCount - 1 do + begin + if Params[i] = '0' then + countOfZeros := countOfZeros + 1; + end; + Result := countOfZeros + 1 >= ParamCount - countOfZeros - 1; +end; + +function TArrangeBinary.ab(Digits: Array of Integer; n: Integer): Boolean; +var + i, countOfZeros: Integer; +begin + countOfZeros := -n; + for i in Digits do + begin + if i = 0 then + countOfZeros := countOfZeros + 1; + end; + Result := countOfZeros + 1 >= length(Digits) - countOfZeros; +end; + +procedure TArrangeBinary.TestApplication; +var + x: Boolean; +begin + x := ab([1, 0, 0, 0, 1], 1); + if x then + writeln('Works for: [1, 0, 0, 0, 1] and 1') + else + writeln('Fails for: [1, 0, 0, 0, 1] and 1'); + x := ab([1, 0, 0, 0, 1], 2); + if not x then + writeln('Works for: [1, 0, 0, 0, 1] and 2') + else + writeln('Fails for: [1, 0, 0, 0, 1] and 2'); +end; + +var + Application: TArrangeBinary; +begin + Application:=TArrangeBinary.Create(nil); + Application.Title:='ArrangeBinary'; + Application.Run; + Application.Free; +end. + diff --git a/challenge-304/barroff/pascal/ch1.lps b/challenge-304/barroff/pascal/ch1.lps new file mode 100644 index 0000000000..c400882cce --- /dev/null +++ b/challenge-304/barroff/pascal/ch1.lps @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<CONFIG> + <ProjectSession> + <Version Value="12"/> + <BuildModes Active="Default"/> + <Units> + <Unit> + <Filename Value="ch1.lpr"/> + <IsPartOfProject Value="True"/> + <IsVisibleTab Value="True"/> + <TopLine Value="58"/> + <CursorPos Y="91"/> + <UsageCount Value="20"/> + <Loaded Value="True"/> + </Unit> + </Units> + <JumpHistory HistoryIndex="-1"/> + <RunParams> + <FormatVersion Value="2"/> + <Modes ActiveMode=""/> + </RunParams> + </ProjectSession> +</CONFIG> diff --git a/challenge-304/barroff/raku/ch-1.p6 b/challenge-304/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..928994ba5e --- /dev/null +++ b/challenge-304/barroff/raku/ch-1.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub arrange-binary(@digits where .all ~~ /^ (0|1) $/, Int $n --> Bool) { + my %counts; + %counts{$_} for @digits; + return %counts{1} + $n ≤ %counts{0} + 1 - $n + 1; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is arrange-binary([1, 0, 0, 0, 1], 1), True, + 'works for "[1, 0, 0, 0, 1] and 1"'; + is arrange-binary([1, 0, 0, 0, 1], 2), False, + 'works for "[1, 0, 0, 0, 1] and 2"'; +} + +#| Take user provided numbers like 2 1 0 0 0 1 +multi sub MAIN(Int $n, *@digits where .all ~~ /^ (0|1) $/) { + say arrange-binary(@digits, $n); +} diff --git a/challenge-304/barroff/raku/ch-2.p6 b/challenge-304/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..0efee249ca --- /dev/null +++ b/challenge-304/barroff/raku/ch-2.p6 @@ -0,0 +1,22 @@ +#!/usr/bin/env raku + +use v6.d; + +sub maximum-average(Int $n, @digits where .elems ≥ $n) { + max(map({ sum(@digits[$_ .. $_ + $n - 1]) }, 0..@digits.elems - $n)) ÷ $n; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is maximum-average(4, [1, 12, -5, -6, 50, 3]), 12.75, + 'works for "[1, 12, -5, -6, 50, 3] and 4"'; + is maximum-average(1, [5]), 5, 'works for "[5] and 1"'; +} + +#| Take user provided numbers like 2 1 0 0 0 1 +multi sub MAIN(Int $n, *@digits) { + say maximum-average($n, @digits); +} |
