30 Days of Code: HackerRank Solutions for Python (Day 2)

Susanna Han
1 min readJan 18, 2021

The one-stop spot for beginners.

Day 2: Operators

Task
Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal’s total cost. Round the result to the nearest integer.

Function Description
Complete the solve function in the editor below.

Solve has the following parameters:

  • int meal_cost: the cost of food before tip and tax
  • int tip_percent: the tip percentage
  • int tax_percent: the tax percentage

Returns The function returns nothing. Print the calculated value, rounded to the nearest integer.

# Three calculations are needed to find the total cost of the meal given the percpective percentages.1. How much is paid in tax. 
2. How much is paid in tip.
3. The total cost including tax and tip.

In order to find how much was spent we take the given percentage multiply by the meal_cost and divide it by 100. We do this to obtain both tip and tax amounts.

Basic Python Operators can be found here: https://www.tutorialspoint.com/python/python_basic_operators.htm\

Solution:

# Complete the solve function below.def solve(meal_cost, tip_percent, tax_percent):    tax = tax_percent * meal_cost/100
tip = tip_percent * meal_cost/100
total_cost = meal_cost + tax + tip
print (round(total_cost))

Thanks for stopping by. If you want to see more solutions share and clap for more!

--

--