Powershell,148145字节
这是一个很好的挑战!
param($s)9..1|?{($p+=$s-match$_)}|%{"$_,^; \. , v ;\. ,/ ; \., \;\^|\\|/|v, "-split';'|%{$x=$s-replace'\.|\d',' '
$s=$s-replace($_-split',')}
$x}
少打高尔夫的测试脚本:
$f = {
param($s)
9..1|?{($p+=$s-match$_)}|%{ # loop digits form 9 downto 1, execute to the end as soon as a suitable digit met
$s=$s-replace$_,'^' # replace current digit with '^'
$s=$s-replace' \. ',' v ' # replace ' . ' with ' v '
$s=$s-replace'\. ','/ ' # replace '. ' with '/ '
$s=$s-replace' \.',' \' # replace ' .' with ' \'
$s-replace'\.|\d',' ' # replace all dots and digits with ' ' and push to output. Don't store this replacement
$s=$s-replace'\^|\\|/|v',' ' # prepeare to the next step: replace ^ \ / and v to space
}
# Example:
# $s="...4...3...3.."
# 4 : $s="...^...3...3.." output: " ^ "
# 4 : $s="... ...3...3.."
# 3 : $s="../ \..^...^.." output: " / \ ^ ^ "
# 3 : $s=".. .. ... .."
# 2 : $s="./ \/ \./ \." output: " / \/ \ / \ "
# 2 : $s=". . ."
# 1 : $s="/ v \" output: "/ v \"
# 1 : $s=" "
}
@(
,("1",
"^")
,("11",
"^^")
,("1.2.",
" ^ ",
"^/ \")
,(".2.3..",
" ^ ",
" ^/ \ ",
"/ \")
,(".2..3..",
" ^ ",
" ^ / \ ",
"/ v \")
,("...4...3...3..",
" ^ ",
" / \ ^ ^ ",
" / \/ \ / \ ",
"/ v \")
,("...4...3...33..4..4....2.3.22.3..5...22...333.222.3..",
" ^ ",
" ^ ^ ^ / \ ",
" / \ ^ ^^ / \/ \ ^ ^/ \ ^^^ ^ ",
" / \/ \ / v \ ^/ \^^/ \^^ / \^^^/ \ ",
"/ v \/ \/ \")
,(".2..3..6.....5...3......1..3..4....2.",
" ^ ",
" / \ ^ ",
" / \ / \ ^ ",
" ^ \/ \ ^ ^ / \ ",
" ^ / v \ / v \ ^ ",
"/ v \ ^/ \/ \")
) | % {
$s,$expected = $_
$result = &$f $s
"$result"-eq"$expected"
$s
$result
}
输出:
True
1
^
True
11
^^
True
1.2.
^
^/ \
True
.2.3..
^
^/ \
/ \
True
.2..3..
^
^ / \
/ v \
True
...4...3...3..
^
/ \ ^ ^
/ \/ \ / \
/ v \
True
...4...3...33..4..4....2.3.22.3..5...22...333.222.3..
^
^ ^ ^ / \
/ \ ^ ^^ / \/ \ ^ ^/ \ ^^^ ^
/ \/ \ / v \ ^/ \^^/ \^^ / \^^^/ \
/ v \/ \/ \
True
.2..3..6.....5...3......1..3..4....2.
^
/ \ ^
/ \ / \ ^
^ \/ \ ^ ^ / \
^ / v \ / v \ ^
/ v \ ^/ \/ \