Last active
September 4, 2024 15:30
-
-
Save SamErde/39eadf55c255a01d15e9affb9b60cbdf to your computer and use it in GitHub Desktop.
A little PowerShell "math" challenge to display rows of numbers that perform sequential addition
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
<# | |
Description: Start with a row of numbers from 0-10. In 10 successive rows, automatically generate the following: | |
- Skip the first index, having one fewer number than the previous row | |
- Add together the [i] and [i-1] numbers from the previous row to get the value of [i] in the current row | |
The final output should look like this: | |
0 1 2 3 4 5 6 7 8 9 10 | |
1 3 5 7 9 11 13 15 17 19 | |
4 8 12 16 20 24 28 32 36 | |
12 20 28 36 44 52 60 68 | |
32 48 64 80 96 112 128 | |
80 112 144 176 208 240 | |
192 256 320 384 448 | |
448 576 704 832 | |
1024 1280 1536 | |
2304 2816 | |
5120 | |
#> | |
<# ============================================== | |
Sam's Solution Using a Hash Table of Arrays | |
Description: "I've been using hash tables lately, so I stuck with the idea of filling "rows" with each calculated | |
row. If it's not clear, $RowItem is just a nested "for $i in" loop for the items within each row. | |
============================================== #> | |
$BaseRow = [System.Collections.Generic.List[int16]]::new() | |
$BaseRow = @(1..10) | |
$BaseRowCount = $BaseRow.Count | |
$Rows = [ordered]@{} | |
$Rows.Add('Row0', $BaseRow) | |
for ($i = 1; $i -lt $BaseRowCount; $i++) { | |
$PreviousRowName = "Row$($i-1)" | |
$ThisRowName = "Row$i" | |
$ThisRow = [System.Collections.Generic.List[int16]]::new() | |
for ($RowItem = 1; $RowItem -le ($BaseRowCount - $i); $RowItem++) { | |
$ThisRow.Add( | |
$Rows[$PreviousRowName][$RowItem] + $Rows[$PreviousRowName][($RowItem - 1)] | |
) | |
} | |
$Rows.Add($ThisRowName, $ThisRow) | |
} | |
# Final desired output | |
(1..$BaseRowCount) | ForEach-Object { Write-Host "$_`t" -NoNewline } | |
Write-Host "`n$('='*78)" | |
foreach ($key in ($Rows.Keys)) { | |
$Tabs = [int]$($Key.Replace('Row', '')) | |
Write-Host "$("`t"*$Tabs)$($Rows[$Key] -join "`t"")" | |
} | |
<# ======================================== | |
Dale's Fast Solution Using a 2D Array | |
Description: "I used short variable names, so it looks worse then it is, but its basically just 2 loops that do the | |
addition, then another 2 loops that do the display. The only difference between the set of loops is the first set | |
skips the first cell++(per iteration), but the second set loops through all the cells." | |
#> | |
$BaseRow = @(1..10) | |
$BaseRowCount = $BaseRow.Count | |
$A = New-Object 'object[,]' $BaseRowCount, $BaseRowCount | |
# Theres a better way to do this, but Im being lazy. | |
$u = 0 | |
While ($u -lt $BaseRowCount) { | |
$A[0, $u] = $BaseRow[$u] | |
$u++ | |
} | |
For ($i = 1; $i -lt $BaseRowCount; $i++) { | |
For ($j = $i; $j -lt $BaseRowCount; $j++) { | |
$a[$i, $j] = $a[($i - 1), ($j - 1)] + $a[($i - 1), ($j)] | |
} | |
} | |
For ($i = 0; $i -lt $BaseRowCount; $i++) { | |
For ($j = 0; $j -lt $BaseRowCount; $j++) { | |
Write-Host -NoNewline $a[$i, $j] | |
Write-Host -NoNewline "`t" | |
} | |
Write-Host '' | |
} | |
<# =============================== | |
Mike's Fast Partial Solution | |
=============================== #> | |
$j = $i = 0 | |
$f = $x = 0..10 | |
while ($j + 1 -lt ($x | Measure-Object -Maximum).Maximum) { | |
$h = 10 - $j * -1 | |
$f = $f[$h..-1] | |
while ($i -lt ($f | Measure-Object -Maximum).Maximum) { | |
$f[$i] + $f[$i + 1]; $i++ | |
} | |
$j++ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment