# 简单标签解析器

9

``````#!/usr/bin/python

def pars(inpu):
outp = ""
stac = []
i = 0
for x in inpu:
lowr = x.lower()
if x == lowr:
stac.append(x)
outp += "(" + x
i = i + 1
else:
while len(stac) > 1 and stac[len(stac) - 1] != lowr:
outp += ")"
stac.pop()
i = i - 1
if len(stac) > 0:
outp += ")"
stac.pop()
i = i - 1
outp += ")" * i
return outp

tests = [
("aAaAbB", "(a)(a)(b)"),
("abBcdDCA", "(a(b)(c(d)))"),
("bisSsIB", "(b(i(s)(s)))"),
("aAabc", "(a)(a(b(c)))"),
("abcdDA", "(a(b(c(d))))"),
("abcAaA", "(a(b(c)))(a)"),
("acAC", "(a(c))"),
("ABCDEFG", ""),
("AbcBCabA", "(b(c))(a(b))")
]

for case, expe in tests:
actu = pars(case)
print "%s: C: [%s] E: [%s] A: [%s]" % (["FAIL", "PASS"][expe == actu], case, expe, actu)
``````

Ming-Tang

4

MtnViewMark

1

## Golfscript，54个字符

``````{[]:|\{.96>{.|+:|;40\}{32+|?).')'*\|>:|;}if}%|,')'*}:\$
``````

``````;["aAaAbB" "abBcdDCA" "bisSsIB" "aAabc" "abcdDA" "abcAaA" "acAC" "aAB" "abAB" "AbcBCabA"]{.' '\\$n}%

aAaAbBaAaAbB (a)(a)(b)
abBcdDCA (a(b)(c(d)))
bisSsIB (b(i(s)(s)))
aAabc (a)(a(b(c)))
abcdDA (a(b(c(d))))
abcAaA (a(b(c)))(a)
acAC (a(c))
aAB (a)
abAB (a(b))
AbcBCabA (b(c))(a(b))
``````

6

``````s@(d:z)§c|c>'^'=toEnum(fromEnum c-32):s++'(':[c]|d<'='=s|d==c=z++")"|1<3=(z++")")§c
p=tail.foldl(§)"\$".(++"\$")
``````

``````> runTests
Pass: aAbaAB parsed correctly as (a)(b(a))
Pass: abcA parsed correctly as (a(b(c)))
Pass: aAB parsed correctly as (a)
Pass: abAB parsed correctly as (a(b))
Pass: aAaAbB parsed correctly as (a)(a)(b)
Pass: abBcdDCA parsed correctly as (a(b)(c(d)))
Pass: bisSsIB parsed correctly as (b(i(s)(s)))
Pass: aAabc parsed correctly as (a)(a(b(c)))
Pass: abcdDA parsed correctly as (a(b(c(d))))
Pass: abcAaA parsed correctly as (a(b(c)))(a)
Pass: acAC parsed correctly as (a(c))
Pass: AbcBCabA parsed correctly as (b(c))(a(b))
``````

• 编辑：（113→111）使用了`@`FUZxxl建议的模式

FUZxxl 2011年

4

# 适用于TI-83 +的Z80机器代码，41字节

`11XXXX131AFE61380F6FE53E28CD9DB47DCD9DB4188EE1BDC03E29CD9DB4189BEF4504E5214CE1C9`

XXXX（包括3-6）是您要解析的字符串的16位地址，减去1个字节。

`¹XX≤¯•⟙8𝑭o↥>(ˣïÑ}ˣïÑ≠á↑γ∊>)ˣïÑ≠Ì⬆︎E𝑤↥!₄L↑Φ`

（大约，因为TI计算器有自己的字符集。）

2

## Windows PowerShell中，142 146 147 152 156 169

``````{\$s=''
-join([char[]]"\$args "|%{if(90-ge\$_){')'*((\$x=\$s.indexOf("\$_".ToLower())+1)+\$s.Length*!\$x)
\$s=\$s.substring(\$x)}else{"(\$_"
\$s="\$_\$s"}})}
``````

``````\$tests = ("aAaAbB","(a)(a)(b)"),("abBcdDCA","(a(b)(c(d)))"),("bisSsIB","(b(i(s)(s)))"),("aAabc","(a)(a(b(c)))"),("abcdDA","(a(b(c(d))))"),("abcAaA", "(a(b(c)))(a)"),("acAC","(a(c))")
"function f " + ((gc ./tags.ps1)-join"`n") | iex
\$tests | %{
\$result = f \$_[0]
("FAIL: \$(\$_[0]):\$(\$_[1]) - \$result", 'PASS')[\$result -ceq \$_[1]]
}
``````

2

# 蟒- 114 113 153 192 174 159个字符

``````from sys import *
s="";c=a=argv[1]
for f in a:
o=c.find;p=f.lower
if '@'<f<'\\':
\td=o(f)-o(p())
\ts+=")"*d
\tc=(c[:o(p())]+c[o(f)+1:])
else:s+=("("+f)
print s
``````

1

gnibbler

1

1
`for i in range(d):s+=")"`可以重写为`s+=")"*d`。您有174个字符。
cemper93 2011年

@cemper-好一点。我整天都做“ _” * 80，打高尔夫球时却忘了它。...另外，感谢@gnibbler的建议！
2011年

cemper93 2011年