본문 바로가기

Python_Matter/[Check_IO]Home

Second Index

반응형

Quiz>

You are given two strings and you have to find an index of the second occurrence of the second string in the 

first one.

 

Let's go through the first example where you need to find the second occurrence of "s" in a word "sims". 

It’s easy to find its first occurrence with a function index or find which will point out that "s" is the first symbol in 

a word "sims" and therefore the index of the first occurrence is 0. 

But we have to find the second "s" which is 4th in a row and that means that the index of the second occurrence

(and the answer to a question) is 3.

 

Input:

Two strings.

 

Output:

Int or None

 

Example:

second_index("sims", "s") == 3
second_index("find the river", "e") == 12
second_index("hi", " ") is None

 

def second_index(text: str, symbol: str) -> [int, None]:
    """
        returns the second index of a symbol in a given text
    """
    # your code here
    return 0


if __name__ == '__main__':
    print('Example:')
    print(second_index("sims", "s"))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert second_index("sims", "s") == 3, "First"
    assert second_index("find the river", "e") == 12, "Second"
    assert second_index("hi", " ") is None, "Third"
    assert second_index("hi mayor", " ") is None, "Fourth"
    assert second_index("hi mr Mayor", " ") == 5, "Fifth"
    print('You are awesome! All tests are done! Go Check it!')

 

Solve>

1. 찾는 문자열이 2개 이상 있는지 확인한다.

   count()는 부분 문자열 세는 메소드이다.

def second_index(text: str, symbol: str):
    if text.count(symbol) < 2:
        return None

 

2. 첫번째 인덱스를 찾는다.

def second_index(text: str, symbol: str):
    first = text.find(symbol)

 

3. 두번째는 첫번째 인덱스 다음부터 찾아야되므로 + 1이다.

   이를 반환하면 된다.

def second_index(text: str, symbol: str):
    return text.find(symbol, first + 1)

 

Code>

def second_index(text: str, symbol: str):
    if text.count(symbol) < 2:
        return None
        
    first = text.find(symbol)
    
    return text.find(symbol, first + 1)

 

Example>

if __name__ == '__main__':
    print('Example:')
    print(second_index("sims", "s"))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert second_index("sims", "s") == 3, "First"
    assert second_index("find the river", "e") == 12, "Second"
    assert second_index("hi", " ") is None, "Third"
    assert second_index("hi mayor", " ") is None, "Fourth"
    assert second_index("hi mr Mayor", " ") == 5, "Fifth"
    print('You are awesome! All tests are done! Go Check it!')

 

Result>

Example:

3

You are awesome! All tests are done! Go Check it!

반응형

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

Digits Multiplication  (0) 2020.04.15
Bigger Price  (0) 2020.04.14
Days Between  (0) 2020.04.14
Right to Left  (0) 2020.04.14
Even the Last  (0) 2020.04.12