def extract_name(s): """ Returns the first name of the person in e-mail address s. We assume (see the precondition below) that the e-mail address is in one of two forms: [email protected] [email protected] where first and last correspond to the person's first and last name. Names are not empty, and contain only letters. Everything after the @ is guaranteed to be exactly as shown. The function preserves the capitalization of the e-mail address. Examples: extract_name('*****@*****.**') returns 'john' extract_name('*****@*****.**') returns 'maggie' extract_name('*****@*****.**') returns 'Bob' Parameter s: The e-mail address to extract from Precondition: s is in one of the two address formats described above """ # You must use an if-else statement in this function. if 'com' in s: idx = introcs.find_str(s, '.') idx_ = introcs.find_str(s, '@') first_name = s[idx + 1:idx_] else: idx = introcs.find_str(s, '.') first_name = s[:idx] return first_name
def first_vowel(s): """ Returns the position of the first vowel in s; it returns -1 if there are no vowels. We define the vowels to be the letters 'a','e','i','o', and 'u'. The letter 'y' counts as a vowel only if it is not the first letter in the string. Examples: first_vowel('hat') returns 1 first_vowel('grrm') returns -1 first_vowel('sky') returns 2 first_vowel('year') returns 1 Parameter s: the string to search Precondition: s is a nonempty string with only lowercase letters """ result = len(s) vowels = ['a', 'e', 'i', 'o', 'u'] for x in vowels: if x in s: result = min(result, introcs.find_str(s, x)) if not x in s and "y" in s and introcs.rfind_str(s, "y") != 0: result = introcs.rfind_str(s, "y") return result if result != len(s) else -1
def get_src(json): """ Returns the src value in the response to a currency query. Given a JSON string provided by the web service, this function returns the string inside string quotes (") immediately following the substring '"src"'. For example, if the json is '{"success": true, "src": "2 United States Dollars", "dst": "1.772814 Euros", "error": ""}' then this function returns '2 United States Dollars' (not '"2 United States Dollars"'). On the other hand if the json is '{"success":false,"src":"","dst":"","error":"Source currency code is invalid."}' then this function returns the empty string. The web server does NOT specify the number of spaces after the colons. The JSON '{"success":true, "src":"2 United States Dollars", "dst":"1.772814 Euros", "error":""}' is also valid (in addition to the examples above). Parameter json: a json string to parse Precondition: json a string provided by the web service (ONLY enforce the type) """ assert type(json) == str, 'Precondition violation' idx = introcs.find_str(json, "src") src = first_inside_quotes(json[idx + 5:]) return src
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
def valid_format(s): """ Returns True if s is a string in 12-format <hours>:<min> AM/PM Example: valid_format('2:45 PM') returns True valid_format('2:45PM') returns False valid_format('14:45') returns False valid_format('14:45 AM') returns False valid_format(245) returns False Parameter s: the candidate time to format Precondition: NONE (s can be any value) """ if type(s) != str: return False colon = introcs.find_str(s, ':') if colon == -1: return False elif not introcs.isdigit(s[:colon]) or not introcs.isdigit( s[colon + 1:colon + 3]): return False hrs = int(s[:colon]) mins = int(s[colon + 1:colon + 3]) if hrs < 1 or hrs > 12 or mins >= 60: return False return s[colon + 3:] == ' AM' or s[colon + 3:] == ' PM'
def has_error(json): """ Returns True if the response to a currency query encountered an error. Given a JSON string provided by the web service, this function returns True if the query failed and there is an error message. For example, if the json is '{"success":false,"src":"","dst":"","error":"Source currency code is invalid."}' then this function returns True (It does NOT return the error message 'Source currency code is invalid'). On the other hand if the json is '{"success": true, "src": "2 United States Dollars", "dst": "1.772814 Euros", "error": ""}' then this function returns False. The web server does NOT specify the number of spaces after the colons. The JSON '{"success":true, "src":"2 United States Dollars", "dst":"1.772814 Euros", "error":""}' is also valid (in addition to the examples above). Parameter json: a json string to parse Precondition: json a string provided by the web service (ONLY enforce the type) """ assert type(json) == str, 'Precondition violation' idx = introcs.find_str(json, "error") error = first_inside_quotes(json[idx + 6:]) result = bool(error) return result
def time_to_minutes(s): """ Returns the number of minutes since midnight Examples: time_to_minutes('2:45 PM') returns 885 time_to_minutes('9:05 AM') returns 545 time_to_minutes('12:00 AM') returns 0 Parameter s: string representation of the time Precondition: s is a string in 12-format '<hours>:<min> AM/PM' """ assert valid_format(s) == True #find the positions pos1 = introcs.find_str(s, ":") pos2 = introcs.find_str(s, " ") hour = str(s[:pos1]) time = s[pos2 + 1:] currMin = int(s[pos1 + 1:pos2]) # assert type(s) == str # assert 7 <= len(s) <= 8 # assert currMin <= 60 # assert 1 <= int(hour) <= 12 # assert introcs.find_str(s,":") != -1 # assert introcs.find_str(s," ") != -1 # convert hour to minutes hour = str(s[:pos1]) time = s[pos2 + 1:] currMin = int(s[pos1 + 1:pos2]) if time == 'PM' and hour != '12': hour = int(hour) hour += 12 min = int(hour) * 60 currMin += min elif time == 'AM' and hour == '12': hour = 0 min = int(hour) * 60 currMin += min else: min = int(hour) * 60 currMin += min return currMin
def first_vowel(s): """ Returns the position of the first vowel in s; it returns -1 if there are no vowels. We define the vowels to be the letters 'a','e','i','o', and 'u'. The letter 'y' counts as a vowel only if it is not the first letter in the string. Examples: first_vowel('hat') returns 1 first_vowel('grrm') returns -1 first_vowel('sky') returns 2 first_vowel('year') returns 1 Parameter s: the string to search Precondition: s is a nonempty string with only lowercase letters """ result = len(s) # In case there is no 'a' if 'a' in s: result = introcs.find_str(s, 'a') if 'e' in s[:result]: result = introcs.find_str(s, 'e') if 'i' in s[:result]: result = introcs.find_str(s, 'i') if 'o' in s[:result]: result = introcs.find_str(s, 'o') if 'u' in s[:result]: result = introcs.find_str(s, 'u') if 'y' in s[1:result]: result = introcs.find_str(s, 'y') return -1 if (result == len(s) and 'y' not in s[1:]) else result
def get_dst(json): """ Returns the dst value in the response to a currency query. Given a JSON string provided by the web service, this function returns the string inside string quotes (") immediately following the substring '"dst"'. For example, if the json is '{"success": true, "src": "2 United States Dollars", "dst": "1.772814 Euros", "error": ""}' then this function returns '1.772814 Euros' (not '"1.772814 Euros"'). On the other hand if the json is '{"success":false,"src":"","dst":"","error":"Source currency code is invalid."}' then this function returns the empty string. The web server does NOT specify the number of spaces after the colons. The JSON '{"success":true, "src":"2 United States Dollars", "dst":"1.772814 Euros", "error":""}' is also valid (in addition to the examples above). Parameter json: a json string to parse Precondition: json a string provided by the web service (ONLY enforce the type) """ pos1 = introcs.find_str(json, '"') pos2 = introcs.find_str(json, '"', pos1 + 1) pos3 = introcs.find_str(json, '"', pos2 + 1) pos4 = introcs.find_str(json, '"', pos3 + 1) pos5 = introcs.find_str(json, '"', pos4 + 1) pos6 = introcs.find_str(json, '"', pos5 + 1) pos7 = introcs.find_str(json, '"', pos6 + 1) pos8 = introcs.find_str(json, '"', pos7 + 1) pos9 = introcs.find_str(json, '"', pos8 + 1) pos10 = introcs.find_str(json, '"', pos9 + 1) return json[pos9 + 1:pos10]
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 type(s) == str pos1 = introcs.find_str(s, '"') assert pos1 >= 0 pos2 = introcs.find_str(s, '"', pos1 + 1) assert pos2 >= pos1 + 1 return s[pos1 + 1:pos2]
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 type(s) == str, "Invalide type" # assert introcs.find_str(s, ' ') == True index = introcs.find_str(s, ' ') assert index >= 0 after = s[index + 1:] return after
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
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
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 type(s) == str, "Input must be a string" # assert introcs.find_str(s, ' ') == True index = introcs.find_str(s, ' ') assert index >= 0 before = s[0:index] return before
def iscurrency(currency): """ Returns True if currency is a valid (3 letter code for a) currency. It returns False otherwise. Parameter currency: the currency code to verify Precondition: currency is a nonempty string with only letters """ assert type(currency) == str and len( currency) == 3, 'Precondition violation' response = service_response(currency, currency, 1) idx = introcs.find_str(response, "error") errormessage = first_inside_quotes(response[idx + len("error") + 1:]) result = not bool(errormessage) return result
def iseurofloat(s): """ Returns True if s is a float in European format. Returns False otherwise. In European format, a comma is used in place of a decimal point. So '12,5' stands for 12.5, '0,12' stands for 0.12 and so. Formally, a string is in European format if it is of the form <d1>,<d2> where d1 and d2 are ints (and d2 >= 0). See https://en.wikipedia.org/wiki/Decimal_separator for more information. This function does not recognize floats in scientific notation (e.g. '1e-2'). Examples: iseurofloat('12,5') returns True iseurofloat('-12,5') returns True iseurofloat('12') returns False iseurofloat('12,-5') returns False iseurofloat(',5') returns False iseurofloat('apple') returns False iseurofloat('12,5.3') returns False iseurofloat('12,5,3') returns False iseurofloat('1e-2') returns False Parameter s: The string to check Precondition: s is a string """ # You MAY NOT use conditionals anywhere in this function. result = False pos1 = introcs.find_str(s, ",") try: assert pos1 != -1 before_comma = s[:pos1] after_comma = s[pos1 + 1:] assert int(after_comma) >= 0 0 > int(before_comma) > 0 and int(after_comma) > 0 result = True except: result = False return result
def has_error(json): """ Returns True if the response to a currency query encountered an error. Given a JSON string provided by the web service, this function returns True if the query failed and there is an error message. For example, if the json is '{"success":false,"src":"","dst":"","error":"Source currency code is invalid."}' then this function returns True (It does NOT return the error message 'Source currency code is invalid'). On the other hand if the json is '{"success": true, "src": "2 United States Dollars", "dst": "1.772814 Euros", "error": ""}' then this function returns False. The web server does NOT specify the number of spaces after the colons. The JSON '{"success":true, "src":"2 United States Dollars", "dst":"1.772814 Euros", "error":""}' is also valid (in addition to the examples above). Parameter json: a json string to parse Precondition: json a string provided by the web service (ONLY enforce the type) """ pos1 = introcs.find_str(json, '"') pos2 = introcs.find_str(json, '"', pos1 + 1) pos3 = introcs.find_str(json, '"', pos2 + 1) pos4 = introcs.find_str(json, '"', pos3 + 1) pos5 = introcs.find_str(json, '"', pos4 + 1) pos6 = introcs.find_str(json, '"', pos5 + 1) pos7 = introcs.find_str(json, '"', pos6 + 1) pos8 = introcs.find_str(json, '"', pos7 + 1) pos9 = introcs.find_str(json, '"', pos8 + 1) pos10 = introcs.find_str(json, '"', pos9 + 1) pos11 = introcs.find_str(json, '"', pos10 + 1) pos12 = introcs.find_str(json, '"', pos11 + 1) pos13 = introcs.find_str(json, '"', pos12 + 1) pos14 = introcs.find_str(json, '"', pos13 + 1) finalVal = len(json[pos13 + 1:pos14]) return finalVal > 0