Sorry for pasting all the code here....but though have a look..


def isCapable(s, able):
    #finds if a beam is capable of withstanding "able"
    charge = 1
    damage = 0
    for step in s:
        if step == "S":
            damage += charge
        if step == "C":
            charge *= 2
    if damage <= able:
        return True
    else:
        return False


def main(able, b):
    if isCapable(b, able):
        #if no swaps are required
        return 0
    swaps = 0
    beam = [s for s in b]
    n = len(beam)
    for i in range(n-1, -1, -1):
        for j in range(n-1, -1, -1):
            if beam[j]=="S" and beam[j-1]=="C":
                temp = beam[j]
                beam[j] = beam[j-1]
                beam[j-1] = temp
                swaps += 1               
            if isCapable(beam, able):
                return swaps
    if isCapable(beam, able) == False:
        #if all swaps are done, but still there's no way 
        return "IMPOSSIBLE"
    return swaps


if __name__ == "__main__":
    t = int(input())
    output = []
    while t > 0:
        rec = input().split()
        o = main(   int(rec[0]), rec[1]    )
        output.append(o)
        t -=  1
    for i in range(len(output)):
        print("Case #" + str(i+1) + ": " + str(output[i]))
    
    

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/7cf5e3cf-955c-4e5a-8d74-647bb2fc5d7a%40googlegroups.com.

Reply via email to