Ejemplo n.º 1
0
def exchange(src, dst, amt):
    """
    Returns the amount of currency received in the given exchange.

    In this exchange, the user is changing amt money in currency src to the currency 
    dst. The value returned represents the amount in currency currency_to.

    The value returned has type float.

    Parameter src: the currency on hand
    Precondition src is a string for a valid currency code

    Parameter dst: the currency to convert to
    Precondition dst is a string for a valid currency code

    Parameter amt: amount of currency to convert
    Precondition amt is a float or int
    """

    assert type(src) == str and len(src) == 3, 'Precondition violation'
    assert introcs.count_str(src, " ") == 0, 'Precondition violation'
    assert type(dst) == str and len(dst) == 3, 'Precondition violation'
    assert introcs.count_str(dst, " ") == 0, 'Precondition violation'
    assert type(amt) == float or type(amt) == int, 'Precondition violation'

    response = service_response(src, dst, amt)
    idx = introcs.find_str(response, "dst")
    convert_to = first_inside_quotes(response[idx + len("dst") + 1:])
    amt_received = before_space(convert_to)
    value = float(amt_received)
    return value
Ejemplo n.º 2
0
def after_space(s):
    """
    Returns the substring of s after the first space

    Example: after_space('Hello World') returns 'World'

    Parameter s: the string to slice
    Precondition: s is a string with at least one space in it
    """

    assert introcs.count_str(s, " ") != 0, 'Precondition violation'

    idx = introcs.find_str(s, ' ')
    sub = s[idx + 1:]
    return sub
Ejemplo n.º 3
0
def service_response(src, dst, amt):
    """
    Returns a JSON string that is a response to a currency query.

    A currency query converts amt money in currency src to the currency dst. The response 
    should be a string of the form

        '{"success": true, "src": "<src-amount>", dst: "<dst-amount>", error: ""}'

    where the values src-amount and dst-amount contain the value and name for the src 
    and dst currencies, respectively. If the query is invalid, both src-amount and 
    dst-amount will be empty, and the error message will not be empty.

    There may or may not be spaces after the colon.  To test this function, you should
    chose specific examples from your web browser.

    Parameter src: the currency on hand
    Precondition src is a nonempty string with only letters

    Parameter dst: the currency to convert to
    Precondition dst is a nonempty string with only letters

    Parameter amt: amount of currency to convert
    Precondition amt is a float or int
    """

    assert type(src) == str and len(src) == 3, 'Precondition violation'
    assert introcs.count_str(src, " ") == 0, 'Precondition violation'
    assert type(dst) == str and len(dst) == 3, 'Precondition violation'
    assert introcs.count_str(dst, " ") == 0, 'Precondition violation'
    assert type(amt) == float or type(amt) == int, 'Precondition violation'

    url = 'https://ecpyfac.ecornell.com/python/currency/fixed?src=' + src + '&dst='
    url += dst + '&amt=' + str(amt) + '&key=' + APIKEY
    json = introcs.urlread(url)
    return json
Ejemplo n.º 4
0
def before_space(s):
    """
    Returns the substring of s up to, but not including, the first space.

    Example: before_space('Hello World') returns 'Hello'

    Parameter s: the string to slice
    Precondition: s is a string with at least one space in it
    """

    assert introcs.count_str(s, " ") != 0, 'Precondition violation'

    idx = introcs.find_str(s, ' ')
    sub = s[:idx]
    return sub
Ejemplo n.º 5
0
def first_inside_quotes(s):
    """
    Returns the first substring of s between two (double) quote characters

    Note that the double quotes must be part of the string.  So "Hello World" is a 
    precondition violation, since there are no double quotes inside the string.

    Example: first_inside_quotes('A "B C" D') returns 'B C'
    Example: first_inside_quotes('A "B C" D "E F" G') returns 'B C', because it only 
    picks the first such substring.

    Parameter s: a string to search
    Precondition: s is a string with at least two (double) quote characters inside
    """
    assert introcs.count_str(s, '"') >= 2, 'Precondition violation'

    idx1 = introcs.index_str(s, '"')
    idx2 = introcs.index_str(s, '"', idx1 + 1, len(s))
    sub = s[(idx1 + 1):idx2]
    return sub