In: Computer Science
Write the following easy Python functions:
1) Write the function named roundDollars(). The function has one input, a String named amountStr which consists of a dollar-formatted amount, such as "$ 1,256.86". The returned value is an int representing the number of rounded "dollars" in the amount, (1257 in the sample shown here). You will need to scrub, format and parse the input, then use arithmetic to determine how many rounded dollars the amount contains.
roundDollars("$ 1,256.86") → 1257
roundDollars("$ 0.42") → 0
roundDollars("$375.00") → 3752
2) Write the function named lineItem() that formats an item on a receipt. The function has two parameters, a String description and a double price. Return a formatted string where description is left-aligned in a column 15 wide and the price is right-aligned in a column 10 wide with two decimals. Thousands should be separated by commas but there should be no dollar sign. For example:
lineItem("Toaster", 24) → "Toaster 24.00"
lineItem("Orange Juice", 3.59) → "Orange Juice 3.59"
lineItem("Carbon Fiber Bike", 1129) → "Carbon Fiber Bi 1,129.00"
3) Write the function named change() that returns a list of coins, formatted in String form, returned from a purchase. The function has two parameters, the amountTendered and the purchasePrice, both doubles. Your function calculates the change due, and then formats the number of dollars, quarters (.25), dimes (.10), nickels (.05) and pennies (.01) due. Each number will be an int and it will be formatted in a column 5 wide. (In other words, your output will have exactly 5 columns and your returned String will be 25 characters long.
change(20, 3.89) → ' 16 0 1 0 1'
change(100, 38.12) → ' 61 3 1 0 3'
change(.25,.20) → ' 0 0 0 1 0'
1. ROUND DOLLARS
Please refer to the following code for the solution
# declaring Function def roundDollars(amountStr): # Removing , from the amount str amountStr = amountStr.replace(',', '') # Fetching string after $ amountStr = float(amountStr[1:]) # Rounding Off return round(amountStr) # Driver code to run Program print(roundDollars('$1,256.86')) print(roundDollars("$ 0.42")) print(roundDollars("$375.00"))
Output:-
1257
0
375
===============================================================================
Refer to the images for better understanding
======================================================
2. Line Item
Please refer to the following code
# declaring Function def lineItem(description, price): # conversion to float and comma seperation price="{0:,.2f}".format(price) # returning in desired return description +" " + price # Driver code to run Program print(lineItem('Toaster' ,24)) print(lineItem("Orange Juice", 3.59)) print(lineItem("Carbon Fiber Bike", 1129))
Output:-
Toaster 24.00
Orange Juice 3.59
Carbon Fiber Bike 1,129.00
============================================================
Please refer to the following images for better understanding:-
=========================================================
3. Change
Please refer to the following code:-
# declaring Function def change(amountTendered, purchasedPrice): amount_to_be_returned = amountTendered-purchasedPrice # dollor find dollor = int(amount_to_be_returned) left = amount_to_be_returned - dollor left = float("{0:.2f}".format(left)) # quater find quaters = int(left / 0.25) left = left - 0.25 *quaters # conversion to precision left = float("{0:.2f}".format(left)) # dimes find dimes = int(float(left) / 0.10) left = left - 0.10 * dimes left = float("{0:.2f}".format(left)) # nickels find nickels = int(float(left) / 0.05) left = left - nickels * 0.05 left = float("{0:.2f}".format(left)) # pennies find pennies= int(float(left) /.01) # desired output return str(dollor)+' ' +str(quaters)+ ' ' +str(dimes)+' '+str(nickels)+ ' '+ str(pennies) # Driver code to run Program print(change(20, 3.89)) print(change(100, 38.12)) print(change(.25,.20))
Output:-
16 0 1 0 1
61 3 1 0 3
0 0 0 1 0
=============================================
Please refer to the following images for better understanding