to site top page

「Pythonでは飯食えない」という判定は回避できるかもしれない

in Python.非効率的なやり方ですが.mahjong.pyは170分くらいで書いたので実質180分ギリギリ.ギリギリでこんなもんです……orz 残りの10分は,二週間程前に書いて使ってた組合せのscriptで要した時間.

雑感

source

# -vim- fileencoding: utf-8
#mahjong.py

import combination as cmb

def extblock(hand, hf=False):
    if hf: block = [i for i in cmb.cmbs(hand,2) if i == i[0]*2]
    else:
        block = []
    block += [i for i in cmb.cmbs(hand,3) if i == i[0]*3 or i in "123456789"]
    block = sorted(list(set(block)))
    result = []
    for i in block:
        t = list(hand)
        for j in i:
            t.remove(j)
        result.append([i, "".join(t)])
    return result

def handparts(hand):
    f7 = [i for i in set(hand) if hand.count(i) == 2]
    if len(f7) == 6:
        return [[i*2 for i in f7] + [i for i in set(hand) if hand.count(i) == 1]]
    else:
        result = extblock(hand)
        while len(result[0][-1]) > 4:
            result = [r[:-1] + e for r in result for e in extblock(r[-1])]
        gbuf = []
        for r in result:
            buf = []
            for e in extblock(r[-1], True):
                if e[-1] in "123456789" or e[-1] in "13243546576879" or e[-1] == e[-1][0]*2:
                    buf.append(r[:-1] + e)
            gbuf += buf
        result = list(set([str(r) for r in [sorted([int(i) for i in g]) for g in gbuf]]))
        buf = [i[1:-1].split(", ") for i in result]
        return buf

def disp(blocks):
    s = "(" + ")(".join([b for b in blocks if len(b) == 3]) + ")"
    hn = [b for b in blocks if len(b) != 3]
    if len(hn) == 1:
        print(s + "[" + hn[0] + "]")
    else:
        if len(hn) == 7:
            s = "(" + ")(".join([b for b in blocks if len(b) == 2]) + ")"
            print(s + "[" + "".join([b for b in blocks if len(b) == 1]) + "]")
        elif hn == [hn[0][0]*2, hn[1][0]*2]:
            print(s + "(" + hn[0] + ")[" + hn[1] + "]")
            print(s + "(" + hn[1] + ")[" + hn[0] + "]")
        else:
            for h in hn:
                if h == h[0]*2:
                    s += "(" + h + ")"
                else:
                    w = "[" + h + "]"
            print(s + w)

if __name__ == "__main__":
    hand = ["1122334455778", "1112224588899", "1122335556799", "1112223335559",
            "1223344888999", "1112345678999", "1111222233699"]
    for h in hand:
        print(h, ":")
        for hp in handparts(h):
            disp(hp)

以下はcombination.py

# -vim- fileencoding: utf-8

def cmbn(elements, length, dup=False):
    if length < 1 or len(elements) < length and dup == False:
        return []
    elif len(elements) == 1 and dup == True:
        return [elements[0] * (10 ** length - 1) // 9]
    elif length == 1:
        return elements
    elif len(elements) == length and dup == False:
        val = elements[0]
        for i in elements[1:]:
            val = 10 * val + i
        return [val]
    else:
        lst = []
        if dup == False:
            for i in cmbn(elements[1:],length - 1, dup):
                lst.append(elements[0] * 10 ** (length - 1) + i)
        else:
            for i in cmbn(elements, length - 1, dup):
                lst.append(elements[0] * 10 ** (length - 1) + i)
        for i in cmbn(elements[1:],length, dup):
            lst.append(i)
        return lst

def cmbs(elements, length, dup=False):
    if length < 1 or len(elements) < length and dup == False:
        return []
    elif len(elements) == 1 and dup == True:
        return [elements[0] * length]
    elif length == 1:
        return elements
    elif len(elements) == length and dup == False:
        return ["".join(elements)]
    else:
        lst = []
        if dup == False:
            for i in cmbs(elements[1:],length - 1, dup):
                lst.append(elements[0] + i)
        else:
            for i in cmbs(elements, length - 1, dup):
                lst.append(elements[0] + i)
        for i in cmbs(elements[1:],length, dup):
            lst.append(i)
        return lst

can't load my result

前後の記事

最近の記事(5件分)

する事

そのうち記事にするかもリスト

欲しい本