Skip to content

Instantly share code, notes, and snippets.

@jpoechill
Last active May 23, 2017 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpoechill/e167693f96c24e410dc5f8e4b16031cc to your computer and use it in GitHub Desktop.
Save jpoechill/e167693f96c24e410dc5f8e4b16031cc to your computer and use it in GitHub Desktop.
// Codefights Prompt
You have a list of dishes. Each dish is associated with a list of ingredients used to prepare it. You want to to group the dishes by ingredients, so that for each ingredient you'll be able to find all the dishes that contain it (if there are at least 2 such dishes).
Return an array where each element is a list with the first element equal to the name of the ingredient and all of the other elements equal to the names of dishes that contain this ingredient. The dishes inside each list should be sorted lexicographically. The result array should be sorted lexicographically by the names of the ingredients in its elements.
Example
For
dishes = [["Salad", "Tomato", "Cucumber", "Salad", "Sauce"],
["Pizza", "Tomato", "Sausage", "Sauce", "Dough"],
["Quesadilla", "Chicken", "Cheese", "Sauce"],
["Sandwich", "Salad", "Bread", "Tomato", "Cheese"]]
the output should be
groupingDishes(dishes) = [["Cheese", "Quesadilla", "Sandwich"],
["Salad", "Salad", "Sandwich"],
["Sauce", "Pizza", "Quesadilla", "Salad"],
["Tomato", "Pizza", "Salad", "Sandwich"]]
For
dishes = [["Pasta", "Tomato Sauce", "Onions", "Garlic"],
["Chicken Curry", "Chicken", "Curry Sauce"],
["Fried Rice", "Rice", "Onions", "Nuts"],
["Salad", "Spinach", "Nuts"],
["Sandwich", "Cheese", "Bread"],
["Quesadilla", "Chicken", "Cheese"]]
the output should be
groupingDishes(dishes) = [["Cheese", "Quesadilla", "Sandwich"],
["Chicken", "Chicken Curry", "Quesadilla"],
["Nuts", "Fried Rice", "Salad"],
["Onions", "Fried Rice", "Pasta"]]
Input/Output
[time limit] 4000ms (js)
[input] array.array.string dishes
An array of dishes. dishes[i] for each valid i contains information about the ith dish: the first element of dishes[i] is the name of the dish and the following elements are the ingredients of that dish. Both the dish name and the ingredient names consist of English letters and spaces. It is guaranteed that all dish names are different. It is also guaranteed that ingredient names for one dish are also pairwise different.
Guaranteed constraints:
1 ≤ dishes.length ≤ 500,
2 ≤ dishes[i].length ≤ 10,
1 ≤ dishes[i][j].length ≤ 50.
[output] array.array.string
The array containing the grouped dishes.
// My solution 7/8 Tests
function groupingDishes(dishes) {
var dishNames = []
var allIngredients = {};
for (var i = 0; i < dishes.length; i++) {
dishNames.push(dishes[i][0])
for (var t = 1; t < dishes[i].length; t ++) {
if ( allIngredients.hasOwnProperty(dishes[i][t]) ) {
allIngredients[dishes[i][t]]++
} else {
allIngredients[dishes[i][t]] = 1
}
}
}
for (var w in allIngredients) {
if (allIngredients[w] < 2) {
delete allIngredients[w]
}
}
var keys = Object.keys(allIngredients).sort()
var dishesResult = []
for (var t in keys) {
var dishLine = []
var _dishIngredients = []
dishLine.push(keys[t])
for (var q = 0; q < dishes.length; q++) {
if (dishes[q].includes(keys[t])) {
_dishIngredients.push(dishes[q][0]);
}
}
_dishIngredients.sort();
for (var e in _dishIngredients) {
dishLine.push(_dishIngredients[e]);
}
dishesResult.push(dishLine)
}
console.log(dishesResult)
return(dishesResult)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment