Free Python course. Sign Up for tracking progress →

Python: Edge cases

The my_substr() function you implemented in the last lesson contains quite a few errors. It passed the test because it had no edge cases. The function worked with normal arguments. Now let's imagine we passed it these length options:

  • 0
  • Negative number
  • A number that exceeds the actual size of the string

The my_substr() function isn't designed for this. The code will run in different situations with different combinations of conditions and data. You can't be sure that the arguments passed to it will always be correct, so you have to consider all cases.

Edge cases are a common cause of logic errors in programs. There's always something that programmers forget to take into account. These errors often don't manifest themselves immediately and may not lead to visible problems for a long time.

The program may continue to work, but at some point someone might notice an error in the results. It can some time be a result of Python having dynamic typing.

As you gain more experience, you'll learn to deal with these errors.

Here's an extended version of the my_substr() function. It takes three arguments: a string, an index, and the length of the substring to be extracted. The function returns a substring of the specified length, starting from the index passed. Call examples:

string = 'If I look back I am lost'
print(my_substr(string, 0, 1))  # => 'I'
print(my_substr(string, 3, 6))  # => 'I look'

What edge cases to consider:

  • If the extracted substring has a negative length
  • The index given is negative
  • The index given exceeds the boundary of the entire string
  • The substring length together with the given index exceeds the boundary of the whole string

When the function is implemented, each edge case will be a separate piece of code. It'll most likely be implemented with if.

If you want to write my_substr() in a way that's protected against these cases, it's worth implementing a separate function that the arguments are valid

Instructions

Implement a predicate function called is_arguments_for_substr_correct() that takes three arguments:

  1. the string
  2. the index from where the extraction will begin
  3. the length of the substring to be extracted

The function returns False if at least one of the conditions is true:

  • The extracted substring has a negative length
  • The index given is negative
  • The index given exceeds the boundary of the entire string
  • The substring length together with the given index exceeds the boundary of the whole string

Otherwise, the function returns True.

Don't forget that indices start with 0, so the index of the last element is “string length minus 1”.

Call example:

string = 'Sansa Stark'
print(is_arguments_for_substr_correct(string, 2, -3))   # => False
print(is_arguments_for_substr_correct(string, -1, 3))   # => False
print(is_arguments_for_substr_correct(string, 4, 100))  # => False
print(is_arguments_for_substr_correct(string, 10, 10))  # => False
print(is_arguments_for_substr_correct(string, 11, 1))   # => False
print(is_arguments_for_substr_correct(string, 3, 3))    # => True
print(is_arguments_for_substr_correct(string, 0, 5))    # => True
The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.