Problem p02891 (python)

Fixed submission:
from math import floor

word = input()
num = int(input())
length=len(word)

char_before= ''
sum=0
subsum =0
count= 0
stack=[]

for index in range(length):
    char_now=word[index]
    if index == 0 :
        char_before = char_now
        char_first=char_now
        count = 1
        if length==1:
            char_last=char_now
            stack.append(count)
        continue
    
    if char_before == char_now :
        count = count + 1
    else:
        char_before = char_now
        stack.append(count)
        count = 1
    
    if index == length-1:
        char_last=char_now
        stack.append(count)

for i in stack:
    subsum= subsum + floor(i/2)
    
if (stack[0]%2)==1 and (stack[-1]%2)==1 and char_last==char_first and len(stack)>=2:
    sum =(subsum+1)*num -1
elif len(stack)==1:
    sum = floor(length*num/2)
else:
    sum =(subsum)*num

print(sum)

Buggy submission (verdict: Runtime Error):
from math import floor

word = input()
num = int(input())
length=len(word)

char_before= ''
sum=0
subsum =0
count= 0
stack=[]

for index in range(length):
    char_now=word[index]
    if index == 0 :
        char_before = char_now
        char_first=char_now
        count = 1
        continue
    
    if char_before == char_now :
        count = count + 1
    else:
        char_before = char_now
        stack.append(count)
        count = 1
    
    if index == length-1:
        char_last=char_now
        stack.append(count)
        
for i in stack:
    subsum= subsum + floor(i/2)
    
if (stack[0]%2)==1 and (stack[-1]%2)==1 and char_last==char_first and len(stack)>=2:
    sum =(subsum+1)*num -1
elif len(stack)==1:
    sum = floor(length*num/2)
else:
    sum =(subsum)*num

print(sum)

Buggy submission (verdict: Runtime Error):
from math import floor

word = input()
num = int(input())
length=len(word)
charlist_size=num*length

string_generator=(word[i] for i in range(length))

char_before= ''
sum=0
subsum =0
count= 0
stack=[]

for index in range(length):
    char_now=next(string_generator)
    if index == 0 :
        char_before = char_now
        char_first=char_now
        count = 1
        continue
    
    if char_before == char_now :
        count = count + 1
    else:
        char_before = char_now
        stack.append(count)
        count = 1
    
    if index == length-1:
        char_last=char_now
        stack.append(count)
        
for i in stack:
    subsum= subsum + floor(i/2)
    
if (stack[0]%2)==1 and (stack[-1]%2)==1 and char_last==char_first and len(stack)>=2:
    sum =(subsum+1)*num -1
elif len(stack)==1:
    sum = floor(length*num/2)
else:
    sum =(subsum)*num

print(sum)

Buggy submission (verdict: Runtime Error):
from math import floor

word = input()
num = int(input())
length=len(word)
charlist_size=num*length

string_generator=(word[i] for i in range(length))

char_before= ''
sum=0
subsum =0
count= 0
stack=[]

for index in range(length):
    char_now=next(string_generator)
    if index == 0 :
        char_before = char_now
        char_first=char_now
        count = 1
        continue
    
    if char_before == char_now :
        count = count + 1
    else:
        char_before = char_now
        stack.append(count)
        count = 1
    
    if index == length-1:
        char_last=char_now
        stack.append(count)
        
for i in stack:
    subsum= subsum + floor(i/2)
    
if (stack[0]%2)==1 and (stack[-1]%2)==1 and char_last==char_first:
    sum =(subsum+1)*num -1
else:
    sum =(subsum)*num

print(sum)

Buggy submission (verdict: Runtime Error):
from math import floor

word = input()
num = int(input())
string = word*num
charlist_size=num*len(word)


char_before= ''
sum=0
count= 0

for index in range(charlist_size):
    if index == 0 :
        char_before = string[index]
        count = 1
        continue
    
    if char_before == string[index] :
        count = count + 1
    else:
        char_before = string[index]
        sum = sum+ floor(count/2)
        count = 1
    
    if index == charlist_size-1:
        sum = sum+ floor(count/2)
    
print(sum)