본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 71

반응형

Q>

There are four substring missions that were born all in one day and you shouldn’t be needed more than one day to solve them.

(하루에 모두 태어난 네 개의 하위 문자열 임무가 있으며이를 해결하기 위해 하루 이상을 필요로하지 않아야합니다.)

All of those mission can be simply solved by brute force, but is it always the best way to go?

(모든 임무는 짐승들에 의해 간단히 해결 될 수 있지만, 항상 최선의 방법입니까?)

(you might not have access to all of those missions yet, but they are going to be available with more opened islands on the map).

(아직 모든 임무에 액세스 할 수는 없지만지도에 더 많은 섬이있는 경우 사용할 수 있습니다).

This mission is the first one of the series.

(이 임무는이 시리즈의 첫 번째 임무입니다.)

Here you should find the length of the longest substring that consists of the same letter.

(여기서 동일한 문자로 구성된 가장 긴 하위 문자열의 길이를 찾아야합니다.)

For example, line "aaabbcaaaa" contains four substrings with the same letters "aaa", "bb","c" and "aaaa".

(예를 들어, "aaabbcaaaa"행에는 "aaa", "bb", "c"및 "aaaa"와 같은 4 개의 하위 문자열이 있습니다.)

The last substring is the longest one which makes it an answer.

(마지막 부분 문자열은 대답을 만드는 가장 긴 부분 문자열입니다.)

 

Input: String.

 

Output: Int.

 

Example:

long_repeat('sdsffffse') == 4
long_repeat('ddvvrwwwrggg') == 3

 

A>

def long_repeat(line):
    """
        length the longest substring that consists of the same char
    """
    # your code here
    cha = []
    if len(line) == 1:
        long = 1
    else:
        for i in range(len(line) - 1):
            if line[i] != line[i + 1]:
                cha.append(1)
            elif i == 0:
                cha.append(1)
                cha[-1] += 1
            else:
                cha[-1] += 1
            i += 1
        if len(cha) == 1:
            long = cha[0]
        elif len(cha) == 0:
            long =0
        else:
            long = max(cha)
    return long

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert long_repeat('sdsffffse') == 4, "First"
    assert long_repeat('ddvvrwwwrggg') == 3, "Second"
    assert long_repeat('abababaab') == 2, "Third"
    assert long_repeat('') == 0, "Empty"
    print('"Run" is good. How is "Check"?')

 

O>

"Run" is good. How is "Check"?

Process finished with exit code 0

 

S>

https://py.checkio.org/

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 73  (0) 2019.06.19
Python Learn the basics Quiz 72  (0) 2019.06.19
Python Learn the basics Quiz 70  (0) 2019.06.15
Python Learn the basics Quiz 69  (0) 2019.06.15
Python Learn the basics Quiz 68  (0) 2019.06.15