master
 1<<-DOC
 2You have a list of dishes. 
 3Each dish is associated with a list of ingredients used to prepare it.
 4You want to group the dishes by ingredients, 
 5so that for each ingredient you'll be able to find all the dishes that contain it
 6(if there are at least 2 such dishes).
 7
 8Return an array where each element is a list with the first element equal to the name of the
 9ingredient and all of the other elements equal to the names of dishes that contain this ingredient.
10The dishes inside each list should be sorted lexicographically.
11The result array should be sorted lexicographically by the names of the ingredients in its elements.
12
13Example
14
15For
16  dishes = [["Salad", "Tomato", "Cucumber", "Salad", "Sauce"],
17            ["Pizza", "Tomato", "Sausage", "Sauce", "Dough"],
18            ["Quesadilla", "Chicken", "Cheese", "Sauce"],
19            ["Sandwich", "Salad", "Bread", "Tomato", "Cheese"]]
20the output should be
21  groupingDishes(dishes) = [["Cheese", "Quesadilla", "Sandwich"],
22                            ["Salad", "Salad", "Sandwich"],
23                            ["Sauce", "Pizza", "Quesadilla", "Salad"],
24                            ["Tomato", "Pizza", "Salad", "Sandwich"]]
25For
26  dishes = [["Pasta", "Tomato Sauce", "Onions", "Garlic"],
27            ["Chicken Curry", "Chicken", "Curry Sauce"],
28            ["Fried Rice", "Rice", "Onions", "Nuts"],
29            ["Salad", "Spinach", "Nuts"],
30            ["Sandwich", "Cheese", "Bread"],
31            ["Quesadilla", "Chicken", "Cheese"]]
32the output should be
33  groupingDishes(dishes) = [["Cheese", "Quesadilla", "Sandwich"],
34                            ["Chicken", "Chicken Curry", "Quesadilla"],
35                            ["Nuts", "Fried Rice", "Salad"],
36                            ["Onions", "Fried Rice", "Pasta"]]
37Input/Output
38
39[time limit] 4000ms (rb)
40[input] array.array.string dishes
41
42An array of dishes. 
43dishes[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.
44Both the dish name and the ingredient names consist of English letters and spaces.
45It is guaranteed that all dish names are different.
46It is also guaranteed that ingredient names for one dish are also pairwise different.
47
48Guaranteed constraints:
491  dishes.length  500,
502  dishes[i].length  10,
511  dishes[i][j].length  50.
52
53[output] array.array.string
54
55The array containing the grouped dishes.
56DOC
57
58describe "#grouping_dishes" do
59  def grouping_dishes(dishes)
60    ingredients = {}
61    dishes.each do |menu|
62      dish = menu[0]
63      menu[1..-1].each do |ingredient|
64        ingredients[ingredient] ||= []
65        ingredients[ingredient].push(dish)
66      end
67    end
68
69    result = []
70    ingredients.each do |(ingredient, dishes)|
71      next if dishes.count <= 1
72      result.push(dishes.sort.unshift(ingredient))
73    end
74    result.sort!
75  end
76
77  [
78    { dishes: [["Salad","Tomato","Cucumber","Salad","Sauce"], ["Pizza","Tomato","Sausage","Sauce","Dough"], ["Quesadilla","Chicken","Cheese","Sauce"], ["Sandwich","Salad","Bread","Tomato","Cheese"]], x: [["Cheese","Quesadilla","Sandwich"], ["Salad","Salad","Sandwich"], ["Sauce","Pizza","Quesadilla","Salad"], ["Tomato","Pizza","Salad","Sandwich"]] },
79    { 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"]], x: [["Cheese","Quesadilla","Sandwich"], ["Chicken","Chicken Curry","Quesadilla"], ["Nuts","Fried Rice","Salad"], ["Onions","Fried Rice","Pasta"]] },
80    { dishes: [["Pasta","Tomato Sauce","Onions","Garlic"], ["Chicken Curry","Chicken","Curry Sauce"], ["Fried Rice","Rice","Onion","Nuts"], ["Salad","Spinach","Nut"], ["Sandwich","Cheese","Bread"], ["Quesadilla","Chickens","Cheeseeee"]], x: [] },
81    { dishes: [["First","a","b","c","d","e","f","g","h","i"], ["Second","i","h","g","f","e","x","c","b","a"]], x: [["a","First","Second"], ["b","First","Second"], ["c","First","Second"], ["e","First","Second"], ["f","First","Second"], ["g","First","Second"], ["h","First","Second"], ["i","First","Second"]] },
82  ].each do |x|
83    it do
84      result = grouping_dishes(x[:dishes])
85      expect(result).to match_array(x[:x])
86    end
87  end
88end