Problem p02954 (python)

Fixed submission:
s = input()
ans = []

def latterCount(idx, s):
    if len(s) == 0:
        return 0
    if s[idx] != 'L':
        return 0

    count = 1
    for i in range(idx+1, len(s)):
        if s[i] == 'L' and s[i] == s[i-1]:
            count += 1
        else:
            break
    return count

def formerCount(idx, s):
    if len(s) == 0:
        return 0
    if s[idx] != 'R':
        return 0

    count = 1
    for i in reversed(range(idx)):
        if s[i] == 'R' and s[i] == s[i+1]:
            count += 1
        else:
            break
    return count

s = s.replace('RL', 'W')

#print(s)

for i in range(len(s)):
    if s[i] == 'R' or s[i] == 'L':
        ans.append(0)
    else:
        if i > 0:
            rCount = formerCount(i-1, s)
        else:
            rCount = 0
            #        print('rCount: ' + str(rCount))
        if i < len(s) - 1:
            lCount = latterCount(i+1, s)
        else:
            lCount = 0
#        print('lCount: ' + str(lCount))
        first = rCount // 2 + 1 + (lCount // 2 + lCount % 2)
        second = (rCount // 2 + rCount % 2) + 1 + lCount // 2
        ans.append(first)
        ans.append(second)

print(' '.join([str(i) for i in ans]))

Buggy submission (verdict: Runtime Error):
s = input()
ans = []

def latterCount(idx, s):
    if len(s) == 0:
        return 0
    if s[idx] != 'L':
        return 0

    count = 1
    for i in range(idx+1, len(s)):
        if s[i] == 'L' and s[i] == s[i-1]:
            count += 1
        else:
            break
    return count

def formerCount(idx, s):
    if len(s) == 0:
        return 0
    if s[idx] != 'R':
        return 0

    count = 1
    for i in reversed(range(idx)):
        if s[i] == 'R' and s[i] == s[i+1]:
            count += 1
        else:
            break
    return count

s = s.replace('RL', 'W')

#print(s)

for i in range(len(s)):
    if s[i] == 'R' or s[i] == 'L':
        ans.append(0)
    else:
        if i > 0:
            rCount = formerCount(i-1, s)
        else:
            rCount = 0
            #        print('rCount: ' + str(rCount))
        if i < len(s) - 1:
            lCount = latterCount(i+1, s)
        else:
            lCount = 0
#        print('lCount: ' + str(lCount))
	first = rCount // 2 + 1 + (lCount // 2 + lCount % 2)
        second = (rCount // 2 + rCount % 2) + 1 + lCount // 2
        ans.append(first)
        ans.append(second)

print(' '.join([str(i) for i in ans]))

Buggy submission (verdict: Runtime Error):
s = input()
ans = []

def latterCount(idx, s):
    if s[idx] != 'L':
        return 0

    count = 1
    for i in range(idx+1, len(s)):
        if s[i] == 'L' and s[i] == s[i-1]:
            count += 1
        else:
            break
    return count

def formerCount(idx, s):
    if s[idx] != 'R':
        return 0

    count = 1
    for i in reversed(range(idx)):
        if s[i] == 'R' and s[i] == s[i+1]:
            count += 1
        else:
            break
    return count

s = s.replace('RL', 'W')

#print(s)

for i in range(len(s)):
    if s[i] == 'R' or s[i] == 'L':
        ans.append(0)
    else:
        rCount = formerCount(i-1, s)
#        print('rCount: ' + str(rCount))
        lCount = latterCount(i+1, s)
#        print('lCount: ' + str(lCount))
        first = rCount // 2 + 1 + (lCount // 2 + lCount % 2)
        second = (rCount // 2 + rCount % 2) + 1 + lCount // 2
        ans.append(first)
        ans.append(second)

print(' '.join([str(i) for i in ans]))