Skip to content

Instantly share code, notes, and snippets.

@vital-edu
Last active August 13, 2020 01:33
Show Gist options
  • Save vital-edu/23dd6b21bce1efb523a91efdf23bfe92 to your computer and use it in GitHub Desktop.
Save vital-edu/23dd6b21bce1efb523a91efdf23bfe92 to your computer and use it in GitHub Desktop.
Python 3 Challenge
# Python 3
from urllib.parse import urlencode
def gen_url(query_params={}, include_auth=True):
uri = 'http://google.com'
auth_key = 'default_value'
if include_auth:
query_params['default_param'] = auth_key
if not query_params:
return uri
encoded_query = urlencode(query_params)
return f'{uri}?{encoded_query}'
assert gen_url(include_auth=False) == 'http://google.com'
assert gen_url(include_auth=True) == 'http://google.com?default_param=default_value'
assert gen_url(query_params={'param_a': 'a'}, include_auth=True) == 'http://google.com?param_a=a&default_param=default_value'
assert gen_url(query_params={'param_a': 'a'}, include_auth=False) == 'http://google.com?param_a=a'
assert gen_url(include_auth=False) == 'http://google.com', 'this should be equal to the first assert. It should be idempotent.'
print('Congratulations! You solved it! Please explain it to me.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment