Problem p02873 (python)

Fixed submission:
S = input()
def rle_encode(data):
    encoding = []
    prev_char = ''
    count = 1

    if not data: return ''

    for char in data:
        # If the prev and current characters
        # don't match...
        if char != prev_char:
            # ...then add the count and character
            # to our encoding
            if prev_char:
                encoding.append([int(count), prev_char])
            count = 1
            prev_char = char
        else:
            # Or increment our counter
            # if the characters do match
            count += 1
    else:
        # Finish off the encoding
        encoding.append([int(count), prev_char])
        return encoding

S_rle = list(rle_encode(S))
if len(S_rle) == 1:
  print(sum(range(S_rle[0][0]+1)))
  exit()
total = 0
if S_rle[0][1] == '>':
  head = S_rle[0]
  S_rle.pop(0)
  total += sum(range(head[0]+1))
else:
  head = []
if S_rle[-1][1] == '<':
  tail = S_rle[-1]
  S_rle.pop(-1)
  total += sum(range(tail[0]+1))
else:
  tail = []
body = S_rle[:]
for i in range(len(body)//2):
  num1, num2 = body[2*i][0], body[2*i+1][0]
  total += sum(range(max(num1, num2)+1)) + sum(range(min(num1, num2)))
print(total)

Buggy submission (verdict: Runtime Error):
S = input()
def rle_encode(data):
    encoding = []
    prev_char = ''
    count = 1

    if not data: return ''

    for char in data:
        # If the prev and current characters
        # don't match...
        if char != prev_char:
            # ...then add the count and character
            # to our encoding
            if prev_char:
                encoding.append([int(count), prev_char])
            count = 1
            prev_char = char
        else:
            # Or increment our counter
            # if the characters do match
            count += 1
    else:
        # Finish off the encoding
        encoding.append([int(count), prev_char])
        return encoding

S_rle = list(rle_encode(S))
total = 0
if S_rle[0][1] == '>':
  head = S_rle[0]
  S_rle.pop(0)
  total += sum(range(head[0]+1))
else:
  head = []
if S_rle[-1][1] == '<':
  tail = S_rle[-1]
  S_rle.pop(-1)
  total += sum(range(tail[0]+1))
else:
  tail = []
body = S_rle[:]
for i in range(len(body)//2):
  num1, num2 = body[2*i][0], body[2*i+1][0]
  total += sum(range(max(num1, num2)+1)) + sum(range(min(num1, num2)))
print(total)

Buggy submission (verdict: Runtime Error):
S = input()
def rle_encode(data):
    encoding = []
    prev_char = ''
    count = 1

    if not data: return ''

    for char in data:
        # If the prev and current characters
        # don't match...
        if char != prev_char:
            # ...then add the count and character
            # to our encoding
            if prev_char:
                encoding.append([int(count), prev_char])
            count = 1
            prev_char = char
        else:
            # Or increment our counter
            # if the characters do match
            count += 1
    else:
        # Finish off the encoding
        encoding.append([int(count), prev_char])
        return encoding

S_rle = list(rle_encode(S))
total = 0
if S_rle[0][1] == '>':
  head = S_rle[0]
  S_rle.pop(0)
  total += sum(range(head[0]))
else:
  head = []
if S_rle[-1][1] == '<':
  tail = S_rle[-1]
  S_rle.pop(-1)
  total += sum(range(tail[0]))
else:
  tail = []
body = S_rle[:]
for i in range(len(body)//2):
  num1, num2 = body[2*i][0], body[2*i+1][0]
  total += sum(range(max(num1, num2)+1)) + sum(range(min(num1, num2)))
print(total)