본문 바로가기

Python_Matter/[Check_IO]Home

Between Markers

Quiz>

You are given a string and two markers (the initial and final). 

You have to find a substring enclosed between these two markers. 

But there are a few important conditions:

 

The initial and final markers are always different.

If there is no initial marker, then the first character should be considered the beginning of a string.

If there is no final marker, then the last character should be considered the ending of a string.

If the initial and final markers are missing then simply return the whole string.

If the final marker comes before the initial marker, then return an empty string.

Input: Three arguments. All of them are strings. The second and third arguments are the initial and final markers.

 

Output:

A string.

 

Example:

between_markers('What is >apple<', '>', '<') == 'apple'
between_markers('No[/b] hi', '[b]', '[/b]') == 'No'

 

How it is used: 

for parsing texts

 

Precondition:

can't be more than one final marker and can't be more than one initial. Marker can't be an empty string

 

def between_markers(text: str, begin: str, end: str) -> str:
    """
        returns substring between two given markers
    """
    # your code here
    return ''


if __name__ == '__main__':
    print('Example:')
    print(between_markers('What is >apple<', '>', '<'))

    # These "asserts" are used for self-checking and not for testing
    assert between_markers('What is >apple<', '>', '<') == "apple", "One sym"
    assert between_markers("<head><title>My new site</title></head>",
                           "<title>", "</title>") == "My new site", "HTML"
    assert between_markers('No[/b] hi', '[b]', '[/b]') == 'No', 'No opened'
    assert between_markers('No [b]hi', '[b]', '[/b]') == 'hi', 'No close'
    assert between_markers('No hi', '[b]', '[/b]') == 'No hi', 'No markers at all'
    assert between_markers('No <hi>', '>', '<') == '', 'Wrong direction'
    print('Wow, you are doing pretty good. Time to check it!')

 

Solve>

1. 시작 텍스트가 문자열에 있으면 시작 인덱스 번호는 find를 통해 찾은 문자열의 위치 + 시작 텍스트의 원소 갯수

def between_markers(text: str, begin: str, end: str):
    if begin in text:
        begin_index = text.find(begin) + len(begin)

 

2. 그 외에 경우에는 시작 번호는 0

def between_markers(text: str, begin: str, end: str):
    else:
        begin_index = 0


3. 끝 텍스트가 문자열에 있으면 find를 통해 찾은 문자열의 위치가 끝 인덱스 번호

def between_markers(text: str, begin: str, end: str):
    if end in text:
        end_index = text.find(end)

 

4. 그 외에 경우에는 문자열의 원소 갯수

def between_markers(text: str, begin: str, end: str):
    else:
        end_index = len(text)

 

5. 이렇게 찾아낸 인덱스번호를 통해 슬라이싱 작업

def between_markers(text: str, begin: str, end: str):
    return text[begin_index:end_index]

 

Code>

def between_markers(text: str, begin: str, end: str):
    if begin in text:
        begin_index = text.find(begin) + len(begin)
    else:
        begin_index = 0
        
    if end in text:
        end_index = text.find(end)
    else:
        end_index = len(text)
        
    return text[begin_index:end_index]

 

Example>

if __name__ == '__main__':
    print('Example:')
    print(between_markers('What is >apple<', '>', '<'))

    # These "asserts" are used for self-checking and not for testing
    assert between_markers('What is >apple<', '>', '<') == "apple", "One sym"
    assert between_markers("<head><title>My new site</title></head>",
                           "<title>", "</title>") == "My new site", "HTML"
    assert between_markers('No[/b] hi', '[b]', '[/b]') == 'No', 'No opened'
    assert between_markers('No [b]hi', '[b]', '[/b]') == 'hi', 'No close'
    assert between_markers(
        'No hi', '[b]', '[/b]') == 'No hi', 'No markers at all'
    assert between_markers('No <hi>', '>', '<') == '', 'Wrong direction'
    print('Wow, you are doing pretty good. Time to check it!')

 

Result>

Example:

apple

Wow, you are doing pretty good. Time to check it!

'Python_Matter > [Check_IO]Home' 카테고리의 다른 글

Right to Left  (0) 2020.04.14
Even the Last  (0) 2020.04.12
Sum Numbers  (0) 2020.04.12
Split List  (0) 2020.04.12
First Word  (0) 2020.04.12
Between Markers  (0) 2020.04.12