Problem p02873 (python)

Fixed submission:
S = input()
N = len(S) + 1
 
A = [0] * N
 
if S[0] == '<':
    A[0] = 0

if S[-1] == '>':
    A[-1] = 0

for i in range(1, N-1):
    if S[i-1] == '>' and S[i] == '<':
        A[i] = 0

for i in range(N-1):
    if S[i] == '<':
        A[i+1] = A[i] + 1

for i in range(N-1)[::-1]:
    if S[i] == '>':
        A[i] = max(A[i], A[i+1] + 1)

print(sum(A))

Buggy submission (verdict: Runtime Error):
#AGC-040
S = input()
A = [0] * (len(S) + 1)

def check_left(n, S):
    count = 0
    for i in reversed(range(0, n)):
        if S[i] == "<":
            count += 1
        else:
            break
    return count

def check_right(n, S):
    count = 0
    for i in range(n, len(S)):
        if S[i] == ">":
            count += 1
        else:
            break
    return count

A[0] = max(check_left(0, S), check_right(0, S))

for i in range(1, len(S)+1):
    if A[i-1] != 0:
        if S[i-1] == "<":
            A[i] = A[i-1] + 1
        else:
            if S[i] == "<":
                A[i] = 0
            else:
                A[i] = max(check_left(i, S), check_right(i, S))
    else:
        A[i] = max(check_left(i, S), check_right(i, S))

print(sum(A))

Buggy submission (verdict: Runtime Error):
#AGC-040
S = input()
A = [0] * (len(S) + 1)

def check_left(n, S):
    count = 0
    for i in reversed(range(0, n)):
        if S[i] == "<":
            count += 1
        else:
            break
    return count

def check_right(n, S):
    count = 0
    for i in range(n, len(S)):
        if S[i] == ">":
            count += 1
        else:
            break
    return count

A[0] = max(check_left(i, S), check_right(i, S))

for i in range(1, len(S)+1):
    if A[i-1] != 0:
        if S[i-1] == "<":
            A[i] = A[i-1] + 1
        else:
            if S[i] == "<":
                A[i] = 0
            else:
                A[i] = max(check_left(i, S), check_right(i, S))
    else:
        A[i] = max(check_left(i, S), check_right(i, S))

print(sum(A))

Buggy submission (verdict: Runtime Error):
#AGC-040
S = input()
A = [0] * (len(S) + 1)

def check_left(n, S):
    count = 0
    for i in reversed(range(0, n)):
        if S[i] == "<":
            count += 1
        else:
            break
    return count

def check_right(n, S):
    count = 0
    for i in range(n, len(S)):
        if S[i] == ">":
            count += 1
        else:
            break
    return count

A[0] = max(check_left(i, S), check_right(i, S))

for i in range(1, len(S)+1):
    if A[i-1] != 0:
        if S[i-1] == "<":
            A[i] = A[i-1] + 1
        else: #elif S[i-1] == ">"
            if S[i] == "<":
                A[i] = 0
            else:
                A[i] = max(check_left(i, S), check_right(i, S))
    else:
        A[i] = max(check_left(i, S), check_right(i, S))

print(sum(A))

Buggy submission (verdict: Runtime Error):
#AGC-040
S = input()
A = [0] * (len(S) + 1)

def check_left(n, S):
    count = 0
    for i in reversed(range(0, n)):
        if S[i] == "<":
            count += 1
        else:
            break
    return count

def check_right(n, S):
    count = 0
    for i in range(n, len(S)):
        if S[i] == ">":
            count += 1
        else:
            break
    return count

for i in range(len(S)+1):
    if i > 0 and A[i-1] != 0:
        if S[i-1] == ">":
            if S[i+1] == "<":
                A[i] = 0
            else:
                A[i] = A[i-1] - 1
        else:
            A[i] = A[i-1] + 1
    else:
        A[i] = max(check_left(i, S), check_right(i, S))
    
print(sum(A))

Buggy submission (verdict: Runtime Error):
# とりあえずランレングスhttps://www.kabegiwablog.com/entry/2018/04/09/100000
# 引数を配列に格納
# num = "><>>><<><<<<<>>><"
num = input()

# 変数初期化
count = 1
result = ''

# 配列全体のループ
for i in range(len(num)):
    try:
        if num[i] == num[i+1]:
           count = count + 1
        else:
            result = result + str(count) + str(num[i])
            count = 1
    except:
        # 最後の文字セットがOutOfRangeになるので結果に出力
        result = result + str(count) + str(num[i])
        
def sum_numbers(n):
    if n ==0:
        return 0
    return n + sum_numbers(n-1)

ans = 0
num_list = []
char_list = []

for idx, char in enumerate(result):
    if idx % 2 == 1:
        char_list.append(char)
    else:
        num_list.append(int(char))
if len(num_list) == 1:
    ans = sum_numbers(num_list[0])
else:
	if char_list[0] == "<":
	    if len(char_list) % 2 == 1:
	        for i in range(0, len(num_list)-1, 2):
	#             print(num_list[i], num_list[i+1])
	            if num_list[i] < num_list[i+1]:
	                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
	#             print("上", temp_sum)
	                ans += temp_sum
	            else:
	                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
	#             print("下", temp_sum)
	                ans += temp_sum
	#             print(ans)
	        ans += sum_numbers(num_list[-1])
	    else:
	        for i in range(0, len(num_list), 2):
	#             print(num_list[i], num_list[i+1])
	            if num_list[i] < num_list[i+1]:
	                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
	#             print("上", temp_sum)
	                ans += temp_sum
	            else:
	                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
	#             print("下", temp_sum)
	                ans += temp_sum
	#             print(ans)

	if char_list[0] == ">":
	#     print("test",len(char_list)-1 % 2)
	    if (len(char_list)-1) % 2 == 1:
	        for i in range(1, len(num_list)-1, 2):
	#             print(num_list[i], num_list[i+1])
	            if num_list[i] < num_list[i+1]:
	                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
	                ans += temp_sum
	            else:
	                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
	                ans += temp_sum
	        ans += sum_numbers(num_list[-1])
	    else:
	        for i in range(1, len(num_list), 2):
	#             print(num_list[i], num_list[i+1])
	            if num_list[i] < num_list[i+1]:
	                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
	                ans += temp_sum
	            else:
	                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
	                ans += temp_sum
	    ans += sum_numbers(num_list[0])

print(ans)

Buggy submission (verdict: Runtime Error):
# とりあえずランレングスhttps://www.kabegiwablog.com/entry/2018/04/09/100000
# 引数を配列に格納
# num = "><>>><<><<<<<>>><"
num = input()

# 変数初期化
count = 1
result = ''

# 配列全体のループ
for i in range(len(num)):
    try:
        if num[i] == num[i+1]:
           count = count + 1
        else:
            result = result + str(count) + str(num[i])
            count = 1
    except:
        # 最後の文字セットがOutOfRangeになるので結果に出力
        result = result + str(count) + str(num[i])
        
def sum_numbers(n):
    if n ==0:
        return 0
    return n + sum_numbers(n-1)

ans = 0
num_list = []
char_list = []

for idx, char in enumerate(result):
    if idx % 2 == 1:
        char_list.append(char)
    else:
        num_list.append(int(char))
        
if char_list[0] == "<":
    if len(char_list) % 2 == 1:
        for i in range(0, len(num_list)-1, 2):
#             print(num_list[i], num_list[i+1])
            if num_list[i] < num_list[i+1]:
                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
#             print("上", temp_sum)
                ans += temp_sum
            else:
                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
#             print("下", temp_sum)
                ans += temp_sum
#             print(ans)
        ans += sum_numbers(num_list[-1])
    else:
        for i in range(0, len(num_list), 2):
#             print(num_list[i], num_list[i+1])
            if num_list[i] < num_list[i+1]:
                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
#             print("上", temp_sum)
                ans += temp_sum
            else:
                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
#             print("下", temp_sum)
                ans += temp_sum
#             print(ans)

if char_list[0] == ">":
#     print("test",len(char_list)-1 % 2)
    if (len(char_list)-1) % 2 == 1:
        for i in range(1, len(num_list)-1, 2):
#             print(num_list[i], num_list[i+1])
            if num_list[i] < num_list[i+1]:
                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
                ans += temp_sum
            else:
                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
                ans += temp_sum
        ans += sum_numbers(num_list[-1])
    else:
        for i in range(1, len(num_list), 2):
#             print(num_list[i], num_list[i+1])
            if num_list[i] < num_list[i+1]:
                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
                ans += temp_sum
            else:
                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
                ans += temp_sum
    ans += sum_numbers(num_list[0])

print(ans)