のんびり座りたい strikes back 2017.11.22 解答例

問題はここから
http://nabetani.sakura.ne.jp/hena/ordf07chairs/

```py3:yhpgf07.py
#!env python3
# -*- coding:utf-8 -*-
# http://nabetani.sakura.ne.jp/hena/ordf07chairs/

SMALL_LOVER = "ABCDEFGHIJKLM"
BIG_LOVER = "NOPQRSTUVWXYZ"
SEAT_NUM = [1,3,5,7,9,8,6,4,2]

def solve(data):
    result = ["-"]*len(SEAT_NUM)

    for d in data:
        if d.islower():
            result[result.index(d.upper())] = "-"
        else:
            if sitDownIfBothSideEmpty(d, getOrder(d), result):
                continue
            elif sitDownIfSideEmpty(d, getOrder(d), result):
                continue
            elif sitDown(d, getOrder(d), result):
                continue
    return "".join(result)

def searchSeatIndex(seat):
    return SEAT_NUM.index(seat)

def getOrder(d):
    if d in SMALL_LOVER:
         return range(1,len(SEAT_NUM)+1)
    else:
        return reversed(range(1,len(SEAT_NUM)+1))

def calcSeatIndex(i):
    center = SEAT_NUM.index(i)
    left = (center-1)%len(SEAT_NUM)
    right = (center+1)%len(SEAT_NUM)
    return left, center, right


def sitDownIfBothSideEmpty(d, order, field):
    for i in order:
        left, center, right = calcSeatIndex(i)
        if field[left] == "-" and field[center] == "-" and field[right] == "-":
            field[center] = d
            return True
    return False


def sitDownIfSideEmpty(d, order, field):
    for i in order:
        left, center, right = calcSeatIndex(i)
        if field[center] == "-" and (field[left] == "-" or field[right] == "-"):
            field[center] = d
            return True
    return False

def sitDown(d, order, field):
    for i in order:
        seat = SEAT_NUM.index(i)
        if field[seat] == "-":
            field[seat] = d
            return True
    return False

def test(data, expect):
    result = solve(data)
    if result == expect:
        print("成功: " + result + ":" + expect )
    else:
        print("失敗: " + result + ":" + expect )

test("NABETanI", "I-E--T-B-")
test( "A", "A--------" )
test( "Aa", "---------" )
test( "Z", "----Z----" )
test( "Zz", "---------" )
test( "AaB", "B--------" )
test( "ABa", "-------B-" )
test( "ABCDEFGHI", "AGCEIDHBF" )
test( "OPQRSTUVW", "WSQUOTPVR" )
test( "F", "F--------" )
test( "L", "L--------" )
test( "Mm", "---------" )
test( "JQ", "J---Q----" )
test( "ACP", "A---P--C-" )
test( "GgS", "----S----" )
test( "USLE", "L-E-U-S--" )
test( "XJZY", "J-Y-X-Z--" )
test( "NnJXx", "J--------" )
test( "AJjQa", "----Q----" )
test( "HGThgQ", "----T-Q--" )
test( "NJRErI", "J-E-N--I-" )
test( "ZzWwDYG", "D---Y--G-" )
test( "ZGgKQHM", "K-H-Z-Q-M" )
test( "OZYSAsHP", "A-Y-OPZ-H" )
test( "VNnEISAW", "E-S-VWAI-" )
test( "HhYAKkCOE", "A-O-Y-EC-" )
test( "KAkHJTSWV", "H-JWTSVA-" )
test( "WNTYKHZhMQ", "KMTQWZN-Y" )
test( "YyJHFGfgTh", "J---T----" )
test( "NRWPLBZAOpC", "LBWONZRAC" )
test( "IORiTMXHUCF", "MCTFOURXH" )
test( "AEeHGBEJUNZn", "AZGEUB-HJ" )
test( "GgKGSOsZTLYS", "K-OYZTSGL" )
test( "VZvPOpWPMzHGF", "MGO-W-HFP" )
test( "CMmcUuLTWADFQ", "LFA-TQW-D" )
test( "HUGZMLzlNgTLDd", "H-N-U-MTL" )
test( "CFKEkHeJShUDTf", "C-UTSJ--D" )
test( "QJjGPMAICHKqWcY", "GIMHWKPYA" )
test( "HhIiQCIBiUMZcVv", "--B-QZU-M" )
test( "VMvEmFOPANIBUiVb", "F-PUONAEV" )
test( "EDCJUINdLPVnFpGe", "-VCFUJGLI" )
test( "FGQINTEnLMAlBbPpV", "FMITQAVGE" )
test( "WSVCORUMXsvNBuTtT", "CXBTWRNOM" )
test( "HJTjRVIQAXqDGxCtUh", "-AVCUGRDI" )
test( "ZzHXIEKQYUuBTxLlCq", "HTEYC-KIB" )
test( "RGDCAEBVrgFbTJUePjc", "-T-FUVADP" )
test( "WBIRSALCVvsbwalYBJP", "B-R-YPCIJ" )
test( "HKFZGEPAIphLRarAOaHo", "LHFIZ-GKE" )
test( "WFNwBfbSAXCZzMDJUcKx", "AM-JSUNDK" )


```