Skip to content

A small web application which accepts a POST request and returns a processed string

Notifications You must be signed in to change notification settings

jeremymaya/cut-string

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cut-string

Actions Status

Author: Kyungrae Kim

Endpoint: https://cut-string.herokuapp.com/api/docs


Prompt

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.

Example

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'

Approach

Approach using Enumerate

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 where n is the length of the input string.
  • Space Complexity: O(n). The new string will store 1/3 * n where n is the length of input string.

Approach using Join

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). Where k is either the value of a parameter or the number of elements in the parameter.
  • Space Complexity: O(n). The new string will take 1/3 * n space where n is the length of input string.

Getting Started

Clone this repository to your local machine:

git clone https://github.com/jeremymaya/cut-string.git

Install the dependencies:

pip3 install -r requirements.txt

Development Mode

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.

Open API

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 ==============================

Production

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

Credits


Change Log

  • /test v1 deployment to Heroku Completed - 27 Jul 2020

About

A small web application which accepts a POST request and returns a processed string

Topics

Resources

Stars

Watchers

Forks

Languages