Created
June 25, 2021 12:01
-
-
Save megastary/58203698363084019c1d7103c20fbcbb to your computer and use it in GitHub Desktop.
Nonogram row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Jakub Šindelář [email protected] | |
# Date: 2021-06-25 | |
# Solution definition | |
function nonogramrow { | |
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[ValidateRange(0, 1)] | |
[Int32[]]$BinaryArray | |
) | |
$Result = @() | |
$PositiveLength = 0 | |
foreach ($Value in $BinaryArray) { | |
if ($Value -eq 1) { | |
$PositiveLength++ | |
} | |
else { | |
if ($PositiveLength -gt 0) { | |
$Result += $PositiveLength | |
$PositiveLength = 0 | |
} | |
} | |
} | |
if ($PositiveLength -gt 0) { | |
$Result += $PositiveLength | |
} | |
return [System.Array]$Result | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Test result with Pester | |
# Install with: Install-Module Pester -MinimumVersion 4.0.0 -Force -SkipPublisherCheck | |
# Import tested script | |
BeforeAll { | |
. $PSCommandPath.Replace('.Tests.ps1', '.ps1') | |
} | |
Describe 'nonogramrow' { | |
Context 'no parameters' { | |
It 'returns nothing or 0' { | |
nonogramrow('') | Should -BeIn @($null, 0) | |
} | |
} | |
Context '[0, 0, 0, 0, 0]' { | |
It 'returns nothing or 0' { | |
nonogramrow(0, 0, 0, 0, 0) | Should -BeIn @($null, 0) | |
} | |
} | |
Context '[1, 1, 1, 1, 1]' { | |
It 'returns 5' { | |
nonogramrow(1, 1, 1, 1, 1) | Should -Be @(5) | |
} | |
} | |
Context '[0,1, 1, 1, 1, 1, 0, 1, 1, 1, 1]' { | |
It 'returns 5, 4' { | |
nonogramrow(0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1) | Should -Be @(5, 4) | |
} | |
} | |
Context '[1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0]' { | |
It 'returns 2, 1, 3' { | |
nonogramrow(1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0) | Should -Be @(2, 1, 3) | |
} | |
} | |
Context '[0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1]' { | |
It 'returns 2, 1, 3' { | |
nonogramrow(0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1) | Should -Be @(2, 1, 3) | |
} | |
} | |
Context '[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]' { | |
It 'returns 1, 1, 1, 1, 1, 1, 1, 1' { | |
nonogramrow(1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1) | Should -Be @(1, 1, 1, 1, 1, 1, 1, 1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is my PowerShell solution to challenge https://old.reddit.com/r/dailyprogrammer/comments/o4uyzl/20210621_challenge_395_easy_nonogram_row/