Last active
February 12, 2021 18:37
-
-
Save yovasx2/bde8913159e687bdc647322c26541b2b to your computer and use it in GitHub Desktop.
screening interview: get all the combinatiosn of the menu that get the target budget
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
# "Fruit": 2.15, | |
# "Fries": 2.75, | |
# "Salad": 3.35, | |
# "Wings": 3.55, | |
# "Mozzarella": 4.20, | |
# "Plate": 5.80, | |
# 4.30 -> [["Fruit", "Fruit"]] | |
# possibleOrders(5.50) -> [["Fries", "Fries"], ["Fruit", "Salad"]] | |
def solution(menu, target) | |
array = convert_to_i(menu) | |
target = (target*100).to_i | |
@result = [] | |
for i in (0...array.length) | |
if(array[i][1] < target) | |
check(array, array[i][1], [array[i][0]], target) | |
end | |
end | |
@result | |
end | |
def check(array, total, elements, target) | |
for i in (0...array.length) | |
sum = total | |
sum += array[i][1] | |
if(sum > target) | |
next | |
end | |
if(sum == target) | |
@result << elements + [array[i][0]] | |
next | |
end | |
if(sum < target) | |
check(array, sum, elements + [array[i][0]], target) | |
end | |
end | |
end | |
def convert_to_i(menu) | |
menu.to_a.map { |e| [e[0], (e[1]*100).to_i]} | |
end | |
menu ={ | |
"Fruit": 2.15, | |
"Fries": 2.75, | |
"Salad": 3.35, | |
"Wings": 3.55, | |
"Mozzarella": 4.20, | |
"Plate": 5.80 | |
} | |
target = 5.50 | |
puts solution(menu, target).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment