본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 110

반응형

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).

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

It is the fourth and the last mission of the series.

(이 시리즈의 네 번째이자 마지막 사명입니다.)

But if in the first mission you needed to find repeating letters, then in this one you should find a repeating sequence inside the substring.

(그러나 첫 번째 미션에서 반복되는 글자를 찾으려면이 부분 문자열에서 반복되는 시퀀스를 찾아야합니다.)

I have an example for you: in a string "abababc" - "ab" is a sequence that repeats more than once, so the answer will be "ababab"

(나는 당신을위한 모범을 보입니다 : 문자열 "abababc"에서 - "ab"는 두 번 이상 반복되는 시퀀스이므로 응답은 "ababab"이 될 것입니다.)

 

Input: String.

 

Output: String

.

Example:

repeat_inside('aaaaa') == 'aaaaa'
repeat_inside('aabbff') == 'aa'
repeat_inside('aababcc') == 'abab'
repeat_inside('abc') == ''
repeat_inside('abcabcabab') == 'abcabc'

 

A>

def repeat_inside(line):
    """
        first the longest repeating substring
    """
    # your code here
    result = ''
    for i in range(len(line)):
        for j in range(len(line) - i):
            s = line[i:i + j + 1]
            for k in range(2, len(line) // len(s) + 1):
                ls = s * k
                if ls in line and len(ls) > len(result):
                    result = ls

    return  result

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert repeat_inside('aaaaa') == 'aaaaa', "First"
    assert repeat_inside('aabbff') == 'aa', "Second"
    assert repeat_inside('aababcc') == 'abab', "Third"
    assert repeat_inside('abc') == '', "Forth"
    assert repeat_inside('abcabcabab') == 'abcabc', "Fifth"
    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 112  (0) 2019.07.02
Python Learn the basics Quiz 111  (0) 2019.07.02
Python Learn the basics Quiz 109  (0) 2019.06.28
Python Learn the basics Quiz 108  (0) 2019.06.27
Python Learn the basics Quiz 107  (0) 2019.06.27