How to resolve the algorithm FizzBuzz step by step in the PowerShell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm FizzBuzz step by step in the PowerShell programming language
Table of Contents
Problem Statement
Write a program that prints the integers from 1 to 100 (inclusive).
But:
The FizzBuzz problem was presented as the lowest level of comprehension required to illustrate adequacy.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm FizzBuzz step by step in the PowerShell programming language
Source code in the powershell programming language
for ($i = 1; $i -le 100; $i++) {
if ($i % 15 -eq 0) {
"FizzBuzz"
} elseif ($i % 5 -eq 0) {
"Buzz"
} elseif ($i % 3 -eq 0) {
"Fizz"
} else {
$i
}
}
$txt=$null
1..100 | ForEach-Object {
switch ($_) {
{ $_ % 3 -eq 0 } { $txt+="Fizz" }
{ $_ % 5 -eq 0 } { $txt+="Buzz" }
$_ { if($txt) { $txt } else { $_ }; $txt=$null }
}
}
1..100 | ForEach-Object {
$s = ''
if ($_ % 3 -eq 0) { $s += "Fizz" }
if ($_ % 5 -eq 0) { $s += "Buzz" }
if (-not $s) { $s = $_ }
$s
}
1..100 | % {write-host("$(if(($_ % 3 -ne 0) -and ($_ % 5 -ne 0)){$_})$(if($_ % 3 -eq 0){"Fizz"})$(if($_ % 5 -eq 0){"Buzz"})")}
filter fizz-buzz{
@(
$_,
"Fizz",
"Buzz",
"FizzBuzz"
)[
2 *
($_ -match '[05]$') +
($_ -match '(^([369][0369]?|[258][147]|[147][258]))$')
]
}
1..100 | fizz-buzz
(1..100 -join "`n") + "`nFizzBuzz" -replace '(?ms)(^([369]([369]|(?=0|$))|[258][147]|[147]([28]|(?=5))))(?=[05]?$.*(Fizz))|(((?<=[369])|[^369])0+|((?<=[147\s])|[^147\s])5)(?=$.*(Buzz))|FizzBuzz', '$5$9'
You may also check:How to resolve the algorithm Odd word problem step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Test a function step by step in the F# programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Maze generation step by step in the Phix programming language
You may also check:How to resolve the algorithm Word ladder step by step in the Java programming language