Skip to content

Python: Пограничные случаи. Немного нелогичные условия #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MaratSafiullinYabble opened this issue Mar 17, 2025 · 0 comments · May be fixed by #335
Assignees

Comments

@MaratSafiullinYabble
Copy link

Решение учителя выглядит так:

def is_arguments_for_substr_correct(string, index, length):
    if index < 0:
        return False
    elif length < 0:
        return False
    elif index > len(string) - 1:
        return False
    elif index + length > len(string):
        return False
    return True

Но если допустимая длина может быть ноль, то и индекс может совпадать с длиной строки. Вычисление подстроки только от этого не упадет. elif index > len(string):
И вообще это условие в таком виде оказывается лишним и можно переписать код как:

def is_arguments_for_substr_correct(string, index, length):
    if index < 0:
        return False
    elif length < 0:
        return False
    elif index + length > len(string):
        return False
    return True

Это мелочь, но об нее вполне можно споткнуться.

@MaratSafiullinYabble MaratSafiullinYabble linked a pull request Mar 17, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants