Created
October 16, 2025 13:32
-
-
Save buvanenko/6a3ec844a933d5ceb6342ad523e6f3fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def longest_pattern(s: str, pattern : str = "youit") -> int: | |
| """ | |
| Находит длину самой длинной непрерывной подпоследовательности, | |
| которая повторяет шаблон "youit" | |
| """ | |
| # pattern - базовый шаблон | |
| plen = len(pattern) # длина шаблона (5) | |
| max_len = 0 # максимальная найденная длина | |
| cur_len = 0 # текущая длина подходящей цепочки | |
| pos = 0 # позиция, какую букву шаблона мы сейчас ждём | |
| for ch in s: | |
| if ch == pattern[pos]: | |
| # символ совпал с ожидаемым в шаблоне | |
| cur_len += 1 | |
| pos = (pos + 1) % plen # двигаемся по шаблону. остаток от деления не даёт словить IndexError. | |
| elif ch == 'y': | |
| # несовпадение, но текущая буква — новое начало шаблона | |
| cur_len = 1 | |
| pos = 1 # следующая ожидаемая — 'o' | |
| else: | |
| # вообще не подходит — сбрасываем | |
| cur_len = 0 | |
| pos = 0 | |
| max_len = max(max_len, cur_len) | |
| return max_len | |
| # Тесты из условия | |
| tests = [ | |
| "youityou", | |
| "bbbbbybbb", | |
| "xyz", | |
| "youiyouit", | |
| "t", | |
| "y", | |
| "youitxyouityouit", | |
| "yoyoyoy", | |
| "abcyouityouityouityouityouityouityoui" | |
| ] | |
| # Ожидаемые результаты | |
| expected = [8, 1, 1, 5, 0, 1, 10, 2, 34] | |
| # Проверим всё и красиво выведем | |
| for s, exp in zip(tests, expected): | |
| result = longest_pattern(s) | |
| print(f"{s:<45} → {result:>3} (ожидалось {exp})") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment