DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
figueroadavid profile image
David Figueroa

Powershell

It will automatically center to the specified line width, and requires that the line be at least as wide as the diamond itself.

function show-diamond {
    param(
        [ValidateScript( {$_ -ge 11} )]
        [int]$LineWidth = 11
    )

    $range = [System.Collections.Generic.List[int]]::new()
    (1..11).Where{$_ % 2 -gt 0} | ForEach-Object { $range.Add($_) }
    (9..1).Where{$_ % 2 -gt 0} | ForEach-Object { $range.Add($_) }

    $output = [System.Collections.Generic.List[string]]::new()
    for ($i = 0; $i -lt $range.Count; $i++) {
        $MidPoint = [math]::Round(($LineWidth - $range[$i]) / 2, [System.MidpointRounding]::AwayFromZero)
        $String = '{0}{1}' -f (' ' * $MidPoint), ('*' * $range[$i])
        $output.Add($string)
    }
    $output
}

David F.