Currencies¶
Save all functions into the same file named currencies.py.
We define a first string format as:
amount currency
where amount does not contain decimals, currency always
contains three characters and where the separation between amount
and currency is always a single whitespace. Example: ‘235 EUR’
We define a second string format as:
employee_type:amount currency
where employee_type is a single character meaning: ‘D’ (managing
director), ‘P’ (project manager) or ‘T’ (technician), amount does
not contain decimals, currency always contains three characters
and where the separation between employee_type and amount is
always a colon and the separation between amount and currency
is a single whitespace. Example: ‘D:230 EUR’
Write function
extract_currency()that takes a string in the first format, and returns a new string with the currency. If the given string is empty, it returns also an empty string. Example:>>> extract_currency('235 EUR') 'EUR' >>> extract_currency('1029 USD') 'USD' >>> extract_currency('') ''
Note
More tests are provided in file
test-currencies1.txtWrite function
extract_amount()that takes a string in the first format, and returns the corresponding amount (int). If the given string is empty, it returns -1. Example:>>> extract_amount('235 EUR') 235 >>> extract_amount('1029 USD') 1029 >>> extract_amount('') -1
Note
More tests are provided in file
test-currencies2.txtWrite function
employee_cost()that takes a string in the second format, and returns afloatwith the cost amount in euros, taking into account that when the employee is a managing director, the returning cost is 0.0. There are only two possible currencies other than euros, ‘USD’ and ‘AUD’ and the fix conversion to be applied will be: 1USD = 0.9EUR and 1AUD = 0.65EUR. This function must call the two previous functionsextract_currency()andextract_amount(). Example:>>> round(employee_cost('D:230 EUR'),1) 0.0 >>> round(employee_cost('P:120 USD'),1) 108.0 >>> round(employee_cost('T:110 AUD'),1) 71.5
Note
More tests are provided in file
test-currencies3.txtWrite function
convert_cost()that takes a string in the second format, and returns another string with the following format:amount EURwhere
amountis the integer part of the amount in the given string, converted to euros, taking into account that when the employee is a managing director, the returning cost is 0.0. There are only two possible currencies other than euros, ‘USD’ and ‘AUD’ and the fix conversion to be applied will be: 1USD = 0.9EUR and 1AUD = 0.65EUR. This function must call the previous oneemployee_cost(). Example:>>> convert_cost('P:120 USD') '108 EUR' >>> convert_cost('P:110 AUD') '71 EUR' >>> convert_cost('D:230 EUR') '0 EUR'
Note
More tests are provided in file
test-currencies4.txt
Solution
A solution of these functions is provided in file
currencies.py