Last active
February 15, 2023 10:54
-
-
Save preston-56/fe1d10271d4e7bc8f3954a7786dfe1e2 to your computer and use it in GitHub Desktop.
test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # define a method `solution(a, b, c)` that calculates the total cost of buying all the fruits in a basket. | |
| # The method takes in three parameters: | |
| # a: an array of strings representing the names of the fruits in the basket. | |
| # b: an array of integers representing the prices of each fruit, in the same order as the a array. | |
| # c: a hash that maps the names of the fruits to their corresponding discount percentages. | |
| # The method should return a single integer - the total cost of all the fruits in the basket after applying the discounts. | |
| # Sample input | |
| # a = ["apple", "orange", "banana", "apple", "banana"] | |
| # b = [50, 40, 30] | |
| # c = { "apple" => 10, "orange" => 20, "banana" => 5 } | |
| # Expected output : 179 | |
| # puts solution(a, b, c) | |
| a = ["apple", "orange", "banana", "apple", "banana"] | |
| b = [50, 40, 30] | |
| c = { "apple" => 10, "orange" => 20, "banana" => 5 } | |
| def solution(a, b, c) | |
| total_cost = 0 | |
| a.each do |fruit| | |
| price = b[a.index(fruit)] | |
| #Assign the value of c[fruit] to the variable 'discount.' | |
| #If the value of c[fruit] does not exist, then discount is set to 0. | |
| discount = c[fruit] || 0 | |
| total_cost += price * (1- discount / 100.0) | |
| end | |
| puts total_cost.to_i | |
| end | |
| puts solution(a, b, c) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment