Print a Wavy String Line-by-Line
MATL, 16 bytes
Zv3L)t?yn:)2$S}i
Try it online! Or verify all test cases.
Explanation
Consider inputs 5
, 'programmingpuzzles'
.
Zv % Input, implicit: number n. Symmetric range
% STACK: [1 2 3 4 5 4 3 2 1]
3L % Push [1 -1+1j]. When used as an index, this means 1:end-1
% STACK: [1 2 3 4 5 4 3 2 1], [1 -1+1j]
) % Index. Removes last element
% STACK: [1 2 3 4 5 4 3 2]
t % Duplicate
% STACK: [1 2 3 4 5 4 3 2], [1 2 3 4 5 4 3 2]
? % If non-empty and non-zero
% STACK: [1 2 3 4 5 4 3 2]
y % Implict input: string s. Duplicate from below
% STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 'programmingpuzzles'
n % Number of elements
% STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 18
: % Range
% STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], [1 2 3 ··· 17 18]
) % Index modularly
% STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 1 2]
2$S % Two-input sort: stably sorts first input as given by the second
% STACK: 'piermnlsomgzgapzru'
} % Else. This branch is entered when n=1. The stack contains an empty array
% STACK: []
i % Take input
% STACK: [], [], 'programmingpuzzles'
% End, implicit
% Display stack, implicit. Empty arrays are not displayed
J, 54, 29, 27 26 bytes
-1 byte thanks to hoosierEE
([\:#@[$[:}:|@i:@<:@]) ::[
Try it online!
R, 68 bytes
function(s,n)intToUtf8(unlist(split(utf8ToInt(s),-(n:(2.9-n)-1)^2)))
Try it online!
- -10 bytes thanks to @Giuseppe
- -17 bytes because I was silly
- -9 bytes and
n=1
case fixed thanks to @J.Doe - -3 bytes thanks to @JayCe