I wrote it. To be honest, I'm not offline (sweat)
sleeping_monsters.py
def parse(str):
    # count each letters
    return map(lambda x:ord(x) - ord('a'), list(str.lower()))
def test(in_str, answer):
    max = len("aBcDeFgHiJkL")-1
    xs = [parse(in_str).count(i) for i in range(max+1)]
    killed = 0
    idx = 0
    while idx < max:
        if xs[idx] != 0: # i got a weapon
            for monster in range(idx+1, max+1, 2) + range(1, idx, 2):
                if xs[monster] == 0:   # no monster here
                    break
                killed += xs[monster]  # found! kill monsters!
                xs[monster] = 0
                idx += 2
        idx += 2
    if killed != int(answer):
        print "failed! " + in_str + " expect: " + answer + " got: " + str(killed)
        exit()
test( "gLDLBgBgHDaD", "6" );
        Recommended Posts