PowerShell, 102 байти
param($s,$i)$l=$s.Length;$s+' '*($i%$l)-split"(.{$i})"|?{$_}|%{$r=-not$r;@($_,($_[$l..0]-join''))[$r]}
Запрошено наступним чином (якщо збережено у файлі CodeGolf55051.ps1
)
.\CodeGolf55051.ps1 -s '1234567890' -i 4
Попередні спроби
(довший або недійсний)
PowerShell, 110 байт
param($s,$i)$l=$s.Length;$s+' '*(($i-$l%$i)%$i)-split"(.{$i})"|?{$_}|%{$r=-not$r;@($_,($_[$l..0]-join''))[$r]}
PowerShell, 111 байт
param($s,$i)$s+' '*(($i-$s.Length%$i)%$i)-split"(.{$i})"|?{$_}|%{$r=-not$r;@($_,($_[$_.length..0]-join''))[$r]}
Пояснення
param($s,$i) #Take input text (s) and column width (i)
$s #take the user entered string
+ ' ' * (($i - $s.Length % $i) % $i) #add the least number of spaces required to make its length divisible by i
-split"(.{$i})" #break it into chunks of i characters in length
| ?{$_} #skip any blank lines created in the process
| %{ #for each line
$r = -not $r; # toggle a boolean value
@( # define an array
$_ # index 0 = the string going forwards
,($_[$_.length..0] -join '') # index 1 = the string reversed (by taking each character from the last to the first, then joining them)
)[$r] # if our boolean value is false take the forward string (index 0), if true take the backwards one (index 1)
} #next
PowerShell, 180 байт
param($s,$i)$x="`${0}|{1}|`${2}";$w=$x;1..$i|%{$w=$w-f$_,$x,($i+$_)};$w=$w-replace'\||\${.*}';$r=$w-replace'\$\d+','(.)?';$s+' '*($i-$s.Length%$i)-replace$r,$w-split"(.{$i})"|?{$_}
PowerShell, 196 байт
param($s,$i)$x="{0}|{1}|{2}";$w=$x;1..$i|%{$w=$w-f$_,$x,($i+$_)};$w=$w-replace'(\d+)','$$$1'-replace'\||{.*}';$r=$w-replace'\$\d+','(.)?';$s+' '*($i-$s.Length%$i)-replace$r,$w-split"(.{$i})"|?{$_}
Пояснення
param ($s, $i) #Take input text (s) and column width (i)
$x = "{0}|{1}|{2}" #Define string format which takes 3 entries and pipe delimits them
$w = $x #initialise our replacement regex with this format
1..$i | %{ #for 1 to the specified column width
$w = $w -f $_, $x, ($i + $_) #update the regex 1|{...}|5, 1|2|{...}|6|5, etc
} #resulting in w = 1|2|3|4|{...}|8|7|6|5
$w = $w -replace '(\d+)', '$$$1' #now prefix the numbers with a dollar (so they're regex replacement variables)
-replace '\||{.*}' #and remove the delimiters and superfluous `{...}` left from our middle insertion routine
$r = $w -replace '\$\d+', '(.)?' #then create the match pattern by replacing the variables with optional single character captures
$s #now take the user entered string
+ ' ' * ($i - $s.Length % $i) #add the least number of spaces required to make its length divisible by i
-replace $r, $w #perform a replacement using the regex match and replace patterns created above
-split "(.{$i})" #then split the string into blocks of length i
| ?{$_} #removing any blank lines created in the process
( {...}
у вищезазначених коментарях насправді {0}|{1}|{2}
; я вказую {...}
на покращення читабельності.
Powershell, 120 байт
(недійсне)
param($s,$i)$s + ' ' * ($i-$s.Length%$i) -replace '(.{4})?(.)(.)(.)(.)',"`$1`n`$5`$4`$3`$2`n" -split "`n" | ?{$_ -ne ""}