Skip to content

Commit 48ce285

Browse files
committed
✨ add challenge-23 solution
1 parent 5182c69 commit 48ce285

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Reto 23: La comida de navidad
2+
3+
## Problema
4+
5+
¡Santa 🎅 está organizando una gran cena navideña 🫓 y quiere asegurarse de que todos los platos sean únicos y variados!
6+
7+
Te da una lista de platos navideños donde cada elemento consiste en una lista de strings que comienza con el nombre del plato, seguido de todos los ingredientes utilizados para su preparación.
8+
9+
Tienes que escribir **una función que agrupe los platos por ingredientes siempre que haya al menos 2 platos que los contengan.**
10+
11+
Así que devolvemos un array de arrays donde la primera posición es el nombre del ingrediente y el resto los nombres de los platos.
12+
13+
Tanto la lista de ingredientes como los platos deben estar **ordenados alfabéticamente.**
14+
15+
```js
16+
const dishes = [
17+
["christmas turkey", "turkey", "sauce", "herbs"],
18+
["cake", "flour", "sugar", "egg"],
19+
["hot chocolate", "chocolate", "milk", "sugar"],
20+
["pizza", "sauce", "tomato", "cheese", "ham"],
21+
]
22+
23+
organizeChristmasDinner(dishes)
24+
25+
/*
26+
27+
"sauce" está en 2 platos: "christmas turkey" y "pizza".
28+
"sugar" está en 2 platos: "cake" y "hot chocolate".
29+
El resto de ingredientes solo aparecen en un plato, por lo que no los mostramos.
30+
31+
Enseñamos primero "sauce" porque alfabéticamente está antes que "sugar".
32+
Y los platos de cada ingrediente también están ordenados alfabéticamente.
33+
34+
[
35+
["sauce", "christmas turkey", "pizza"],
36+
["sugar", "cake", "hot chocolate"]
37+
]
38+
*/
39+
```
40+
41+
Ten en cuenta que:
42+
43+
- Todos los nombres de los platos son diferentes.
44+
- Los nombres de los ingredientes para un plato dado son distintos entre sí.
45+
- Si no hay ingredientes repetidos, devolvemos un array vacío.
46+
47+
## Mi solución
48+
49+
```js
50+
function organizeChristmasDinner(dishes) {
51+
const ingredients = {};
52+
53+
for (const [dishName, ...dishIngredients] of dishes) {
54+
for (const ingredient of dishIngredients) {
55+
ingredients[ingredient] = [
56+
...(ingredients[ingredient] ?? []),
57+
dishName,
58+
];
59+
}
60+
}
61+
62+
const output = Object.entries(ingredients)
63+
.filter(([, dishList]) => dishList.length > 1)
64+
.map(([ingredient, dishList]) => [ingredient, ...dishList.sort()])
65+
.sort();
66+
67+
return output;
68+
}
69+
```

2023/23-la-comida-de-navidad/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable no-restricted-syntax */
2+
function organizeChristmasDinner(dishes) {
3+
const ingredients = {};
4+
5+
for (const [dishName, ...dishIngredients] of dishes) {
6+
for (const ingredient of dishIngredients) {
7+
ingredients[ingredient] = [
8+
...(ingredients[ingredient] ?? []),
9+
dishName,
10+
];
11+
}
12+
}
13+
14+
const output = Object.entries(ingredients)
15+
.filter(([, dishList]) => dishList.length > 1)
16+
.map(([ingredient, dishList]) => [ingredient, ...dishList.sort()])
17+
.sort();
18+
19+
return output;
20+
}
21+
22+
module.exports = organizeChristmasDinner;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const organizeChristmasDinner = require('./index');
2+
3+
describe('23 => La comida de navidad', () => {
4+
const testCases = [
5+
{
6+
input: [
7+
['christmas turkey', 'turkey', 'sauce', 'herbs'],
8+
['cake', 'flour', 'sugar', 'egg'],
9+
['hot chocolate', 'chocolate', 'milk', 'sugar'],
10+
['pizza', 'sauce', 'tomato', 'cheese', 'ham'],
11+
],
12+
output: [
13+
['sauce', 'christmas turkey', 'pizza'],
14+
['sugar', 'cake', 'hot chocolate'],
15+
],
16+
},
17+
{
18+
input: [
19+
['fruit salad', 'apple', 'banana', 'orange'],
20+
['berry smoothie', 'blueberry', 'banana', 'milk'],
21+
['apple pie', 'apple', 'sugar', 'flour'],
22+
],
23+
output: [
24+
['apple', 'apple pie', 'fruit salad'],
25+
['banana', 'berry smoothie', 'fruit salad'],
26+
],
27+
},
28+
{
29+
input: [
30+
['gingerbread', 'flour', 'ginger', 'sugar'],
31+
['glazed ham', 'ham', 'honey', 'sugar', 'vinegar'],
32+
['roast chicken', 'chicken', 'rosemary', 'thyme', 'garlic'],
33+
['vegetable soup', 'carrot', 'potato', 'onion', 'garlic'],
34+
['fruit punch', 'apple juice', 'orange juice', 'sugar'],
35+
],
36+
output: [
37+
['garlic', 'roast chicken', 'vegetable soup'],
38+
['sugar', 'fruit punch', 'gingerbread', 'glazed ham'],
39+
],
40+
},
41+
{
42+
input: [
43+
['pumpkin pie', 'pumpkin', 'cinnamon', 'sugar', 'flour'],
44+
['mashed potatoes', 'potatoes', 'butter', 'milk'],
45+
['cinnamon rolls', 'flour', 'cinnamon', 'butter', 'sugar'],
46+
['turkey stuffing', 'bread crumbs', 'celery', 'onion', 'butter'],
47+
],
48+
output: [
49+
['butter', 'cinnamon rolls', 'mashed potatoes', 'turkey stuffing'],
50+
['cinnamon', 'cinnamon rolls', 'pumpkin pie'],
51+
['flour', 'cinnamon rolls', 'pumpkin pie'],
52+
['sugar', 'cinnamon rolls', 'pumpkin pie'],
53+
],
54+
},
55+
{
56+
input: [
57+
['chicken alfredo', 'chicken', 'pasta', 'parmesan'],
58+
['parmesan chicken', 'chicken', 'parmesan', 'bread crumbs'],
59+
['pasta salad', 'pasta', 'olive oil', 'tomato'],
60+
['tomato soup', 'tomato', 'basil', 'cream'],
61+
],
62+
output: [
63+
['chicken', 'chicken alfredo', 'parmesan chicken'],
64+
['parmesan', 'chicken alfredo', 'parmesan chicken'],
65+
['pasta', 'chicken alfredo', 'pasta salad'],
66+
['tomato', 'pasta salad', 'tomato soup'],
67+
],
68+
},
69+
{
70+
input: [
71+
['snowflake cookies', 'flour', 'sugar', 'vanilla'],
72+
['winter stew', 'beef', 'carrots', 'potatoes'],
73+
['holiday punch', 'cranberry juice', 'orange juice', 'sparkling water'],
74+
['festive salad', 'lettuce', 'cranberries', 'walnuts'],
75+
],
76+
output: [],
77+
},
78+
];
79+
80+
it('should return an array type', () => {
81+
expect(Array.isArray(organizeChristmasDinner(testCases[0].input))).toBe(true);
82+
});
83+
84+
it.each(testCases)('should return the correct output', ({ input, output }) => {
85+
expect(organizeChristmasDinner(input)).toEqual(output);
86+
});
87+
});

0 commit comments

Comments
 (0)