Author: Kyungrae Kim
Endpoint: https://cut-string.herokuapp.com/api/docs
Write a small web application in Python/Ruby/Node. The application only needs to do the following:
- Accept a POST request to the route “/test”, which accepts one argument “string_to_cut”
- Return a JSON object with the key “return_string” and a string containing every third letter from the original string.
If you POST
{"string_to_cut": "hellothere"}
it will return:
{"return_string": "ltr"}
Note: To see expected behavior you can test against a current working example with the command:
curl -X POST https://cut-string.herokuapp.com/test --data '{"string_to_cut": "hellothere"}' -H 'Content-Type: application/json'
The first approach is to use enumerate to access the index of the input string and build a new string.
string = "hellothere"
s = ""
for i, letter in enumerate(string, start=1):
if i % 3 == 0:
s += letter
return s
Complexity Analysis
- Time Complexity:
O(n)
. We need to iterate every character from the string wheren
is the length of the input string. - Space Complexity:
O(n)
. The new string will store1/3 * n
wheren
is the length of input string.
Since strings in Python are immutable, a new string needs to be created to solve the problem. This continual copying from the initial approach can lead to significant inefficiencies in Python programs.
We can optimize the above approach using s = "".join(list)
string = "hellothere"
slist = input[2::3]
s = "".join(slist)
return s
Complexity Analysis
- Time Complexity:
O(k)
. Wherek
is either the value of a parameter or the number of elements in the parameter. - Space Complexity:
O(n)
. The new string will take1/3 * n
space wheren
is the length of input string.
Clone this repository to your local machine:
git clone https://github.com/jeremymaya/cut-string.git
Install the dependencies:
pip3 install -r requirements.txt
Start the application in development mode with the following command:
FLASK_ENV=development flask run
Test the functionality of the endpoint running at localhost:5000
with the following command:
curl -X POST http://127.0.0.1:5000/test --data '{"string_to_cut": "hellothere"}' -H 'Content-Type: application/json'
The expected output of the above command is:
{
"return_string": "ltr"
}
Alternatively, test the functionality of the endpoint running at http://127.0.0.1:5000/api/docs/ by clicking the Try it out
button.
Run the automated test with the following command while the server is running:
pytest
The expected output of the above command is:
test/test_endpoints.py .............. [100%]
============================== 14 passed in 0.84s ==============================
Run the application in production with the following command:
uwsgi --ini app.ini --need-app
The application is currently deployed on Heroku with the following endpoint: https://cut-string.herokuapp.com/test
Run the automated test on the deployed Heroku with the following command:
pytest --host https://cut-string.herokuapp.com
- Data Science Blog - REST API Development with Flask
- Python - PerformanceTips
- Python - TimeComplexity
- Python Central - How to Slice Lists/Arrays and Tuples in Python
- pythonise.com - Working with JSON data | Learning Flask Ep. 9
- pytest - Parametrizing fixtures and test functions
- uWSGI - Running python webapps on Heroku with uWSGI
- Heroku Dev Center - Deploying Python Applications with Gunicorn
- /test v1 deployment to Heroku Completed - 27 Jul 2020