Skip to main content
2 of 5
added 2 characters in body
fuandon
  • 311
  • 1
  • 7

##PowerShell(168)##

Amateur PowerShell'r back with another attempt at golF! Hope you don't mind! At the very least these questions are fun, and a learning experience to boot. Assuming n has been inputted, we have:

$x=@('0','1');for($a=1;$a-lt$n;$a++){$x+=$x[($x.length-1)..0];$i=[Math]::Floor(($x.length-1)/2);0..$i|%{$x[$_]='0'+$x[$_]};($i+1)..($x.length-1)|%{$x[$_]='1'+$x[$_]}}$x

Because the PowerShell I'm working with is only 2.0, I can't use any bit shifting cmdlets which might make for shorter code. So I took advantage of a method described here, flipping the array and adding it to itself, appending a 0 to the front of the top half, and a 1 to the bottom half.

fuandon
  • 311
  • 1
  • 7