Skip to content

Instantly share code, notes, and snippets.

@megastary
Created June 25, 2021 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megastary/58203698363084019c1d7103c20fbcbb to your computer and use it in GitHub Desktop.
Save megastary/58203698363084019c1d7103c20fbcbb to your computer and use it in GitHub Desktop.
Nonogram row
# Author: Jakub Šindelář megastary@houby-studio.eu
# 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
}
# 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)
}
}
}
@megastary
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment