python language please, practice with regular expressions and string processing
ID: 3721251 • Letter: P
Question
python language please, practice with regular expressions and string processing
Part II: Determine a Package's Destination (10 points) Write a function package destination ), which takes two arguments in the following order: 1. package.code: a string that might represent a valid shipping code on a package 2. country.codes: a dictionary that matches a country's code (a string) to the name of a country A validly formatted address is defined by the following components, in the given order: 1. three digits CSE 101 -Spring 2018 Lab #13 Page 3 2. a dash 3. three digits 4. a dash 5. one uppercase letter 6. one digit If the string is validly formatted, the function extracts the three-digit country code at the start of the string and uses it as a key in the country-codes dictionary. The country name associated with the key is returned by the function. If the package.code is invalidly formatted, or it is validly formatted but the extracted country code is not in the dictionary, the function returns the string error Hint: after verifying that the argument string matches the expected pattern, use the split ) method to extract the part of the string needed for the dictionary lookupExplanation / Answer
import re def package_destination(package_code,country_codes): pattern=r'^d{3}-d{3}-[A-Z]d{1}$' if re.search(pattern,package_code): code=package_code.split('-')[0] if code in country_codes: return country_codes[code] else: return 'error' else: return 'error'
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.