In this assignment, you'll design and implement one of the string manipulation functions that is commonly asked during interviews.
- Design and implement a function to reverse each word in a sentence. For example, if the function is called with input of "I can be an engineer", the function should return a string object with the value "I nac eb na reenigne". Note that the whitespace between words is preserved.
- Share and explain the time and space complexities for your solution.
- At minimum, your implementation should pass the basic tests.
The Ruby version of this assignment involved reversing words in a sentence in place. Unlike Ruby strings, Python strings are immutable, which means they can't be changed after they are initialized. Because strings cannot be modified in place, a new string must be created whenever a change must be made to a string.
Note: Do not use Python provided functionality for reversed()
, reverse()
, or string slicing ("string"[::-1]
). You may use len()
.
Install pytest
pip install pytest
To run all tests, navigate to the same directory as the test file and run:
pytest test_reverse_words.py
Alternatively, to exit instantly on first error or failed test, run:
pytest -x test_reverse_words.py
See pytest documentation for more information about pytest
.