c!t&n:q3_*ts_b+5M4$XdZ!cZ{Zv
输入是一个字符串单元格数组,其中逗号作为可选的分隔符:
{'programming' 'puzzles' 'and' 'code' 'golf'}
要么
{'programming', 'puzzles', 'and', 'code', 'golf'}
在线尝试!或验证所有测试用例:1,2,3,4。
说明
考虑以下输入作为示例:
{'aaaa' 'bb' 'ccc'}
您可以查看部分结果(堆栈内容),该注释%
在代码的任何位置插入注释符号。例如,在第四个功能()之后查看堆栈内容&n
。
c % Input cell array of strings implicitly. Convert to 2D char array,
% right-padding with spaces
% STACK: ['aaaa'; 'bb '; 'ccc']
! % Transpose
% STACK: ['abc'
'abc'
'a c'
'a ']
t % Duplicate
% STACK: ['abc'
'abc'
'a c'
'a '],
['abc'
'abc'
'a c'
'a '],
&n % Number of rows and of columns
% STACK: ['abc'
'abc'
'a c'
'a '], 4, 3
:q % Range, subtract 1
% STACK: ['abc'
'abc'
'a c'
'a '], 4, [0 1 2]
3_* % Multiply by -3
% STACK: ['abc'
'abc'
'a c'
'a '], 4, [0 -3 -6]
ts_ % Duplicate, sum, negate
% STACK: ['abc'
'abc'
'a c'
'a '], 4, [0 -3 -6], 9
b % Bubble up in stack
% STACK: ['abc'
'abc'
'a c'
'a '], [0 -3 -6], 9, 4
+ % Add
% STACK: ['abc'
'abc'
'a c'
'a '], [0 -3 -6], 13
5M % Push second input of last function again
% STACK: ['abc'
'abc'
'a c'
'a '], [0 -3 -6], 13, 4
4$Xd % Buld numerical sparse matrix from the above four arguments. The
% columns of the first input argument will be the diagonals of the
% result, with indices given bu the second input (negative is below
% main diagonal). The matrix size is the third and fourth arguments
% STACK: [97 0 0 0
0 97 0 0
0 0 97 0
98 0 0 97
0 98 0 0
0 0 32 0
99 0 0 32
0 99 0 0
0 0 99 0
0 0 0 32
0 0 0 0
0 0 0 0
0 0 0 0]
Z!c % Convert from sparse to full, and then to char. Character 0 is
% displayed as space
% STACK: ['a '
' a '
' a '
'b a'
' b '
' '
'c '
' c '
' c '
' '
' '
' '
' ']
Z{ % Split into cell array, with each row in a cell
% STACK: {'a ', ' a ', ' a ', 'b a', ' b ', ' ', 'c ', ' c ', ' c ', ' ', ' ', ' ', ' '}
Zv % Deblank: remove trailing space from each string. Implicitly display,
% each string on a different line. Empty strings do not generate
% a newline
% STACK: {'a ', ' a', ' a', 'b a', ' b', '', 'c', ' c', ' c', '', '', '', ''}