관리 메뉴

Jun Hyuk Kim's Blog

Fixing code in steps and combining 본문

Coding Problems

Fixing code in steps and combining

junhyuk1229 2022. 12. 21. 11:28

I can get my desired result by spliting the process to small parts.

Just know that the problem I got is to check how many sub-strings contains exactly Ex: 'IOIOI'

So when I tried to make the whole code at once I got errors constantly. So I tried changing the question into parts and coding them without worrying about the time complexity.

import sys


P_num = int(sys.stdin.readline().rstrip())
str_len = int(sys.stdin.readline().rstrip())
str_arr = sys.stdin.readline().rstrip()


# iterate through the string to get array of P_nums
check_arr = []
temp_num = temp_int = 0
while temp_int < str_len:
    if temp_int + 1 < str_len and str_arr[temp_int:temp_int + 2] == "IO":
        check_arr.append(str_arr[temp_int:temp_int + 2])
        temp_int += 1
    elif str_arr[temp_int] == 'I':
        check_arr.append(str_arr[temp_int])
    elif str_arr[temp_int] == 'O':
        check_arr.append(str_arr[temp_int])
    temp_int += 1

output_arr = []
temp_num = 0
for temp_str in check_arr:
    if temp_str == "IO":
        temp_num += 1
    else:
        if temp_str == "I" and temp_num > 0:
            output_arr.append(temp_num)
        if temp_str == "O" and temp_num > 1:
            output_arr.append(temp_num - 1)
        temp_num = 0
if temp_num > 1:
    output_arr.append(temp_num - 1)
output_num = 0
for temp_num in output_arr:
    if temp_num >= P_num:
        output_num += temp_num - P_num + 1
print(output_num)

First I iterate through the string and divide the string into a list. Then I get an array containing numbers that tell me the length of the "IO" string. Then that list is used to find the output number. and this worked. Even if this code was slower than what I can make, it helped me understand my code better and reassured me that the solution I thought of worked.

 

Input:

1
13
OOIOIOIOIIOII

Output:
['O', 'O', 'IO', 'IO', 'IO', 'I', 'IO', 'I', 'I']
[3, 1]
4

 

But I knew that this can be done without iterating through 3 lists. So then I combined the 2 loops at the bottom.

import sys


P_num = int(sys.stdin.readline().rstrip())
str_len = int(sys.stdin.readline().rstrip())
str_arr = sys.stdin.readline().rstrip()


# iterate through the string to get array of P_nums
check_arr = []
temp_num = temp_int = 0
while temp_int < str_len:
    if temp_int + 1 < str_len and str_arr[temp_int:temp_int + 2] == "IO":
        check_arr.append(str_arr[temp_int:temp_int + 2])
        temp_int += 1
    elif str_arr[temp_int] == 'I':
        check_arr.append(str_arr[temp_int])
    elif str_arr[temp_int] == 'O':
        check_arr.append(str_arr[temp_int])
    temp_int += 1
print(check_arr)
temp_num = 0
output_num = 0
for temp_str in check_arr:
    if temp_str == "IO":
        temp_num += 1
    else:
        if temp_str == "I" and temp_num >= P_num:
            output_num += temp_num - P_num + 1
        if temp_str == "O" and temp_num >= P_num:
            output_num += temp_num - P_num
        temp_num = 0
if temp_num >= P_num:
    output_num += temp_num - P_num
print(output_num)

So now I merged the 2 loops together saving time. This was really easy to do. I checked if there were no errors and then I merged everything together.

import sys


P_num = int(sys.stdin.readline().rstrip())
str_len = int(sys.stdin.readline().rstrip())
str_arr = sys.stdin.readline().rstrip()

# result number(sub-string number)
output_num = 0
# temp number of "IO" sub-string and temp index position for str_arr
temp_num = temp_index = 0
# loop through the full string
while temp_index < str_len:
    # if the next sub-string is "IO"
    if temp_index + 1 < str_len and str_arr[temp_index:temp_index + 2] == "IO":
        # add 1 to the output sub-string
        temp_num += 1
        # add 1 to the index count making the loop skip the "IO" at once
        temp_index += 1
    # if the next sub-string is not "IO"
    else:
        # if the next char is 'I' then we evaluate the output with "~IOI" ending in mind
        if str_arr[temp_index] == 'I' and temp_num >= P_num:
            output_num += temp_num - P_num + 1
        # if the next char is 'O' then we evaluate the output with "~IOO" ending in mind
        if str_arr[temp_index] == 'O' and temp_num >= P_num:
            # Ex "IOIOIOO": Here the temp_num is 3 because there are 3 "IO"s
            # there are 2 "IOI"s in this string making the output_num += 2
            output_num += temp_num - P_num
        # if the next sub-string is not "IO" set "IO" count to 0
        temp_num = 0
    # add 1 to index
    temp_index += 1
# if the string ends with a "IO" chain
if temp_num >= P_num:
    # Ex "IOIOIO": Here the temp_num is 3 because there are 3 "IO"s
    # there are 2 "IOI"s in this string making the output_num += 2
    output_num += temp_num - P_num
print(output_num)

This is the answer I got. It is hard to tell how it works even with the comments telling every step, but when I see the code at the very top I know how it works. From this I learned that even if there is a faster method I should first visualize the parts into simple chunks. This can help me understand the code structure and tells me if I got the solution right.