わたしのスキルで飯は食えるか?

makeplex salon:あなたのスキルで飯は食えるか? 史上最大のコーディングスキル判定 (1/2) - ITmedia エンタープライズ

この問題に挑戦してみた。1時間くらい

#coding: sjis

# 待ち
def wait(s,t):
    if len(t)==1:
        print s+"[%s]"%t[0]
    if len(t)==2 and abs(t[0]-t[1])<=2:
        print s+"[%s%s]"%(t[0],t[1])

# 頭
def eye(s,t,i):
    if i>9:
        wait(s,t)
    else:
        eye(s,t,i+1)
        if t.count(i)>=2:
            tmp = t[:]
            tmp.remove(i)
            tmp.remove(i)
            wait(s+"(%s%s)"%(i,i),tmp)

# 順子
def sheung(s,t,i):
    if i>7:
        eye(s,t,1)
    else:
        sheung(s,t,i+1)
        if ( i   in t and
             i+1 in t and
             i+2 in t ):
            tmp = t[:]
            tmp.remove(i  )
            tmp.remove(i+1)
            tmp.remove(i+2)
            sheung(s+"(%s%s%s)"%(i,i+1,i+2),tmp,i)
        

# 刻子
def pong(s,t,i):
    if i>9:
        sheung(s,t,1)
    else:
        pong(s,t,i+1)
        if t.count(i)>=3:
            tmp = t[:]
            tmp.remove(i)
            tmp.remove(i)
            tmp.remove(i)
            pong(s+"(%s%s%s)"%(i,i,i),tmp,i)

# 七対子
def seven(t):
    num = [t.count(i) for i in range(1,10)]
    if num.count(2)==6 and num.count(1)==1:
        s = ""
        for i in range(1,10):
            if t.count(i)==2:
                s += "(%s%s)"%(i,i)
            if t.count(i)==1:
                s += "[%s]"%i
        print s
    
def solve(s):
    tile = [int(c) for c in s]
    seven(tile)
    pong("",tile,1)

Q = [
    "1112224588899",
    "1122335556799",
    "1112223335559",
    "1223344888999",
    "1112345678999",
    "1122335778899",
    "1122335778888",
]

for q in Q:
    print q
    solve(q)

七対子という役があること、ただし、ルール上同じ牌が4枚あったときは七対子にならない((11)(22)(33)[5](77)(88)(88)はダメ)ことあたりがハマリどころだろうか。