Creating URLs dynamically is a common task when dealing with web applications and APIs. Building these URLs correctly ensures that the desired endpoint is reached with the appropriate parameters.
To build a URL, we define a function buildURL
that takes a base URL (baseURL
), an endpoint (endpoint
), and a list of parameters (params
) as a list of key-value pairs. The function concatenates the base URL and endpoint, appends a ?
character, and adds the parameters in the format key=value
separated by the &
character. If the params
list contains a page
attribute, it should be placed at the end of the URL. Otherwise, the parameters should be built into the URL in the order given in params
.
Function Description
Complete the function buildURL
in the editor.
buildURL
has the following parameters:
- 1.
String baseURL
: the base URL - 2.
String endpoint
: the endpoint to be appended to the base URL - 3.
List
: a list of key-value pairs representing the parameters> params
Returns
String: the complete URL constructed from the inputs
Example 1:
Input: baseURL = "https://api.example.com", endpoint = "/data", params = [["type", "user"], ["page", "2"], ["id", "123"]]
Output: "https://api.example.com/data?type=user&id=123&page=2"
Explanation:For example, given
baseURL = "https://api.example.com"
,endpoint = "/data"
, andparams = [("type", "user"), ("page", "2"), ("id", "123")]
, the function should returnhttps://api.example.com/data?type=user&id=123&page=2
. Note that the page parameter is moved to the end to ensure it is correctly interpreted by the API.
πβπ¦Ί

input:
output: