The objective of this challenge is to find the total cost of a meal given the mealCost, tipPercent, and taxPercent. The total cost of the meal, rounded to the nearest dollar, can be stored in the variable totalCost.
Sample Input:
12.00
20
8
Sample Output:
15 #12.00 + 12.00*(20/100) + 12.00*(8/100) = round(15.36) = 15
For this problem, HackerRank provides the user the following code and asks them to complete the solve function. The comments below help to explain these lines of code.
#!/bin/python3
# a "shebang line" that defines where the interpreter is located
# (lets the operating system know it's a python script)
import math #imports math library which contains math functions like sqrt(x) or pow(x,y)
import os #os library provides functions for interacting with the operating system
import random #random library allows you to generate random number samples
import re #re library provides functions to search a string for a match or pattern
import sys
# sys library provides info on interaction with the host system (e.g. version, execution)
# This is the function they would like you to complete. It needs to calculate the total meal
# cost given the parameters: meal_cost, tip_percent, tax_percent.
def solve(meal_cost, tip_percent, tax_percent):
if __name__ == '__main__':
#__name__ is a built-in variable which is set to "__main__" if it is being run directly as
# the main program, as it is here. Otherwise, if the module is imported, __name__ is set to the
# name of the current module. For example, if your file is called foo.py, if it's run as
# the main program, then __name__ is set to "__main__". If it's imported, the main program
# imports it as import foo, which is equivalent to setting __name__ = "foo"
meal_cost = float(input())
# takes a user input, converts it from a string to a float, stores it in meal_cost
tip_percent = int(input())
# takes a user input, converts it from a string to a integer, stores it in tip_percent
tax_percent = int(input())
# takes a user input, converts it from a string to a integer, stores it in tip_percent
solve(meal_cost, tip_percent, tax_percent)
# calls the function (has the function run with the three arguments:
# meal_cost, tip_percent, tax_percent)
Inside the function, we just need to calculate the total cost as meal_cost + meal_cost(tip_percent/100) + meal_cost(tax_percent/100). With meal_cost = 12.00, tip_percent = 20, and tax_percent = 8, this generates 15.36, a float. Just converting to an integer will always round it down, rather than “to the nearest dollar”.
int(15.36)
int(15.89)
15
15
Instead, you can first round it to 0 decimal places using round(x,0)
round(15.36, 0)
round(15.89, 0)
15.0
16.0
Then convert the rounded float to an integer.
int(round(15.36, 0))
int(round(15.89, 0))
15
16
Sample Solution:
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the solve function below.
def solve(meal_cost, tip_percent, tax_percent):
print(int(round(meal_cost + meal_cost*(tip_percent/100) + meal_cost*(tax_percent/100), 0)))
if __name__ == '__main__':
meal_cost = float(input())
tip_percent = int(input())
tax_percent = int(input())
solve(meal_cost, tip_percent, tax_percent)