BASHA TECH
[HackerRank][Python] 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.
Example
mealCost = 100
tipPercent = 15
taxPercent = 8
A tip of 15% * 100 = 15, and the taxes are 8% * 100 = 8. Print the value 123 and return from the function.
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.
Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result.
Input Format
There are 3 lines of numeric input:
The first line has a double, mealCost (the cost of the meal before tax and tip).
The second line has an integer, tipPercent (the percentage of mealCost being added as tip).
The third line has an integer, taxPercent (the percentage of mealCost being added as tax).
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'solve' function below.
#
# The function accepts following parameters:
# 1. DOUBLE meal_cost
# 2. INTEGER tip_percent
# 3. INTEGER tax_percent
#
def solve(meal_cost, tip_percent, tax_percent):
# calculate tip amount
tip = meal_cost * tip_percent / 100
# calculate tax amount
tax = meal_cost * tax_percent / 100
# calculate total cost
total_cost = meal_cost + tip + tax
# round total cost to nearest integer
total_cost = round(total_cost)
# print total cost
print(total_cost)
if __name__ == '__main__':
meal_cost = float(input().strip())
tip_percent = int(input().strip())
tax_percent = int(input().strip())
solve(meal_cost, tip_percent, tax_percent)
This code takes in three inputs: the meal cost, tip percent, and tax percent. It then calculates the tip amount, tax amount, and total cost by applying the formulas given in the task. Finally, it rounds the total cost to the nearest integer using the round function and prints the result.
'Activity > Coding Test' 카테고리의 다른 글
[HackerRank][Python] Class vs Instance (0) | 2023.04.12 |
---|---|
[HackerRank][Python] Intro to Conditional Statements (0) | 2023.04.11 |
[HackerRank][Python] Data Types (0) | 2023.04.11 |
[HackerRank][Python] (0) | 2023.04.11 |
[HackerRank][Java] Simple Array (0) | 2023.04.07 |