的powershell,174个 170 168 167字节
$a=@{}
$args|% t*y|?{$_-45}|%{if(!$a.$_){$a.$_="","","$_"}$a.$_[++$i-gt4]+=$i}
0,2,1|%{$r=$_
-join($a.Keys|sort|%{$a.$_[$r]}|% p*ht(1+($a|% v*|%{$_|% l*h}|sort)[-1]))}
少打高尔夫的测试脚本:
$f = {
$a=@{} # a hash table for a result
$args|% toCharArray|?{$_-45}|%{ # for each digit from argument strings except a '-'
if(!$a.$_){$a.$_="","","$_"} # create an array if there are no values for the current digit
$a.$_[++$i-gt4]+=$i # append the character to the relative row (0 - top, 1 - bottom) and increment position number
} # the result is a hash table in which the key is a char of a digit and the value is an array of string
# for example, first lines for the first test case:
# @{
# [char]48: ("2","5","0")
# [char]49: ("3","","1")
# [char]50: ("1","67","2")
# ...
# }
$l=1+($a|% Values|%{$_|% Length}|sort)[-1] # calc the maximum width of strings
0,2,1|%{ # for each number 0,2,1
$r=$_ # store it as row number
-join(
$a.Keys|sort| # for each keys (digit of the dates) in the sorted order
%{$a.$_[$r]}| # push to pipe the relative string
% padRight $l # for which to execute the 'padright' method to pad with spaces
) # and finally join the row
}
}
@(
,("2013-02-27",
"2 3 1 4 ",
"0 1 2 3 7",
"5 67 8")
,("2015-12-24",
"2 3 1 4",
"0 1 2 4 5",
" 5 67 8 ")
,("2222-11-11",
" 1234 ",
"1 2 ",
"5678 ")
,("1878-02-08",
" 1 3 24 ",
"0 1 2 7 8 ",
"57 6 8 ")
,("2061-02-22",
"2 4 1 3 ",
"0 1 2 6 ",
"5 678 ")
,("3564-10-28",
" 1 4 2 3 ",
"0 1 2 3 4 5 6 8 ",
"6 5 7 8 ")
,("1111-11-11",
"1234 ",
"1 ",
"5678 ")
,("0123-12-30",
"1 2 3 4 ",
"0 1 2 3 ",
"8 5 6 7 ")
) | % {
$d, $expected = $_
$result = &$f $d
$d
$j=0
$result|%{
"$($_.trimEnd() -eq $expected[$j].TrimEnd()): |$_|" # Vertical bars only to see trailing and leading spaces
$j++
}
}
输出(竖线仅用于查看尾随和前导空格):
2013-02-27
True: |2 3 1 4 |
True: |0 1 2 3 7 |
True: |5 67 8 |
2015-12-24
True: |2 3 1 4 |
True: |0 1 2 4 5 |
True: | 5 67 8 |
2222-11-11
True: | 1234 |
True: |1 2 |
True: |5678 |
1878-02-08
True: | 1 3 24 |
True: |0 1 2 7 8 |
True: |57 6 8 |
2061-02-22
True: |2 4 1 3 |
True: |0 1 2 6 |
True: |5 678 |
3564-10-28
True: | 1 4 2 3 |
True: |0 1 2 3 4 5 6 8 |
True: |6 5 7 8 |
1111-11-11
True: |1234 |
True: |1 |
True: |5678 |
0123-12-30
True: |1 2 3 4 |
True: |0 1 2 3 |
True: |8 5 6 7 |