Skip to content

Commit a3b8137

Browse files
committed
add 2022
1 parent 4824882 commit a3b8137

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1786
-2
lines changed

2022/challenge-01/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Automating Christmas gift wrapping!
2+
3+
## 🔢 Instructions
4+
5+
This year the elves have bought a gift wrapping machine. But... it's not programmed! **We need to create an algorithm that helps it in the task**.
6+
7+
The machine receives an array with the gifts. Each gift is a `string`. We need the machine to wrap each gift in wrapping paper and place it in an array of wrapped gifts.
8+
9+
The wrapping paper is the `*` symbol and to wrap a gift the `*` symbol is placed so that it completely surrounds the string on all sides. For example:
10+
11+
```javascript
12+
const gifts = ['book', 'game', 'socks'];
13+
const wrapped = wrapping(gifts);
14+
console.log(wrapped);
15+
/* [
16+
"******\n*book*\n******",
17+
"******\n*game*\n******",
18+
"*******\n*socks*\n*******"
19+
] */
20+
```
21+
22+
As you can see, the wrapping paper wraps the string. On top and bottom, so as not to leave any gaps, the corners are also covered with wrapping paper.
23+
24+
**Note**: `\n` is the character that represents a line break.
25+
26+
**Watch out!** Make sure you put the right number of \* to wrap completely the string.
27+
28+
**Good luck!**

2022/challenge-01/solution.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const wrapping = gifts => {
2+
return gifts.map(gift => {
3+
const wrapper = '*'.repeat(gift.length + 2);
4+
return `${wrapper}\n*${gift}*\n${wrapper}`;
5+
});
6+
};
7+
8+
export { wrapping };

2022/challenge-01/solution.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { wrapping } from './solution';
2+
3+
describe('Challenge 01: ¡Automatizando envolver regalos de Navidad!', () => {
4+
describe('wrapping(...)', () => {
5+
const testCases = [
6+
createTestCase(
7+
[['book', 'game', 'socks']],
8+
['******\n*book*\n******', '******\n*game*\n******', '*******\n*socks*\n*******'],
9+
'should return a list of wrapped gifts (1)'
10+
),
11+
createTestCase([['midu']], ['******\n*midu*\n******'], 'should return a list of wrapped gifts (2)'),
12+
createTestCase([['a']], ['***\n*a*\n***'], 'should return a list of wrapped gifts (3)'),
13+
createTestCase([[]], [], 'should return en empty list if no gifts are given')
14+
];
15+
16+
it('#T should return an array', () => {
17+
expect(wrapping([])).toBeInstanceOf(Array);
18+
});
19+
20+
it.each(testCases)('#$# $description', ({ args, expected }) => {
21+
expect(wrapping(...args)).toEqual(expected);
22+
});
23+
});
24+
});

2022/challenge-02/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Nobody wants to do extra hours at work
2+
3+
## 🔢 Instructions
4+
5+
A millionaire has bought a social network and it doesn't bring good news. He has announced that **each time a working day is lost due to a holiday**, it will have to be compensated with **two extra hours another day of the same year**.
6+
7+
Obviously the people who work in the company have not made the slightest joke and are **preparing a program that tells them the number of extra hours they would do** in the year if the new rule were applied.
8+
9+
Since it is office work, their working hours are **from Monday to Friday**. So you only have to worry about the holidays that fall on those days.
10+
11+
Given a year and an array with the dates of the holidays, return the number of extra hours that would be done that year:
12+
13+
```javascript
14+
const year = 2022;
15+
const holidays = ['01/06', '04/01', '12/25']; // format MM/DD
16+
17+
// 01/06 is January 6, Thursday. Count.
18+
// 04/01 is April 1, Friday. Count.
19+
// 12/25 is December 25, Sunday. Do not count.
20+
21+
countHours(year, holidays); // 2 days -> 4 extra hours in the year
22+
```
23+
24+
**Things to keep in mind**:
25+
26+
- The year can be a leap year. Make the checks you need for it, if necessary.
27+
- Although the holiday is December 31, the extra hours will be done the same year.
28+
- `Date.getDay()` method returns the day of the week of a date. `0` is Sunday, `1` is Monday, etc.

2022/challenge-02/solution.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const countHours = (year, holidays) => {
2+
return holidays.reduce((overtimeHours, holiday) => {
3+
const dayOfWeek = new Date(`${holiday}/${year}`).getDay();
4+
const isWorkDay = dayOfWeek !== 0 && dayOfWeek !== 6;
5+
return isWorkDay ? overtimeHours + 2 : overtimeHours;
6+
}, 0);
7+
};
8+
9+
const countHoursAlt = (year, holidays) => {
10+
return holidays.reduce((overtimeHours, holiday) => {
11+
const dayOfWeek = new Date(`${holiday}/${year}`).getDay();
12+
const isWorkDay = [1, 2, 3, 4, 5].includes(dayOfWeek);
13+
return overtimeHours + isWorkDay * 2;
14+
}, 0);
15+
};
16+
17+
export { countHours, countHoursAlt };

2022/challenge-02/solution.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { countHours, countHoursAlt } from './solution';
2+
3+
describe('Challenge 02: Nobody wants to do extra hours at work', () => {
4+
describe('countHours(...)', () => {
5+
const testCases = [
6+
createTestCase([2023, ['01/06', '04/01', '12/25']], 4, 'should return 4 for 2023'),
7+
createTestCase([2022, ['01/06', '04/01', '12/25']], 4, 'should return 4 for 2022'),
8+
createTestCase(
9+
[1985, ['01/01', '01/06', '02/02', '02/17', '02/28', '06/03', '12/06', '12/25']],
10+
10,
11+
'should return 10 for 1985'
12+
),
13+
createTestCase([2000, ['01/01']], 0, 'should return 0 for 2000')
14+
];
15+
16+
it('#T should return a number', () => {
17+
expect(typeof countHours(2022, ['01/01'])).toBe('number');
18+
expect(typeof countHoursAlt(2022, ['01/01'])).toBe('number');
19+
});
20+
21+
it.each(testCases)('#$# $description', ({ args, expected }) => {
22+
expect(countHours(...args)).toEqual(expected);
23+
expect(countHoursAlt(...args)).toEqual(expected);
24+
});
25+
});
26+
});

2022/challenge-03/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# How many packs of gifts can Santa carry?
2+
3+
## 🔢 Instructions
4+
5+
You receive a Christmas gifts pack that Santa Claus wants to deliver to the children. **Each gift inside the pack is represented by a string** and it has a weight equal to the number of letters in its name. Santa Claus's sleigh can only carry a **weight limit**.
6+
7+
Santa Claus also has a list of reindeer able to help him to deliver the gifts. Each reindeer has a **maximum weight limit** that it can carry. The maximum weight limit of each reindeer is **equal to twice the number of letters in its name**.
8+
9+
Your task is to implement a function `distributeGifts(packOfGifts, reindeers)` that receives a gifts pack and a list of reindeer and returns the maximum number of gifts packs that Santa Claus can deliver. **You can't split gifts packs**.
10+
11+
```javascript
12+
const packOfGifts = ['book', 'doll', 'ball'];
13+
const reindeers = ['dasher', 'dancer'];
14+
15+
// pack weights 4 + 4 + 4 = 12
16+
// reindeers can carry (2 * 6) + (2 * 6) = 24
17+
distributeGifts(packOfGifts, reindeers); // 2
18+
```
19+
20+
**Things to keep in mind**:
21+
22+
- The gifts pack can't be splitted.
23+
- Gifts and reindeers' names length will always be greater than 0.

2022/challenge-03/solution.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const distributeGifts = (packOfGifts, reindeers) => {
2+
return Math.floor((reindeers.join``.length * 2) / packOfGifts.join``.length);
3+
};
4+
5+
const distributeGiftsAlt = (packOfGifts, reindeers) => {
6+
// Only valid for numbers between [−2147483648, 2147483647]
7+
return ((reindeers.join``.length * 2) / packOfGifts.join``.length) | 0;
8+
};
9+
10+
export { distributeGifts, distributeGiftsAlt };

2022/challenge-03/solution.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { distributeGifts, distributeGiftsAlt } from './solution';
2+
3+
describe('Challenge 03: How many packs of gifts can Santa carry?', () => {
4+
describe('distributeGifts(...)', () => {
5+
const testCases = [
6+
createTestCase(
7+
[
8+
['a', 'b', 'c'],
9+
['d', 'e']
10+
],
11+
1,
12+
'should return 1 when Santa has 3 gifts with total weight of 3 and 2 reindeers whose weight limit is 4'
13+
),
14+
createTestCase([['videogames', 'console'], ['midu']], 0, "should return 0 if reindeers can't carry any pack"),
15+
createTestCase(
16+
[
17+
['game', 'videoconsole', 'computer'],
18+
['midudev', 'pheralb', 'codingwithdani', 'carlosble', 'blasco', 'facundocapua', 'madeval', 'memxd']
19+
],
20+
5,
21+
'should return 5 when Santa has 3 gifts with total weight of 24 and 8 reindeers whose weight limit is 134'
22+
),
23+
createTestCase(
24+
[
25+
['music'],
26+
['midudev', 'pheralb', 'codingwithdani', 'carlosble', 'blasco', 'facundocapua', 'madeval', 'memxd']
27+
],
28+
26,
29+
'should return 26 when Santa has 1 gift with total weight of 5 and 8 reindeers whose weight limit is 134'
30+
)
31+
];
32+
33+
it('#T should return a number', () => {
34+
expect(typeof distributeGifts([], [])).toBe('number');
35+
expect(typeof distributeGiftsAlt([], [])).toBe('number');
36+
});
37+
38+
it.each(testCases)('#$# $description', ({ args, expected }) => {
39+
expect(distributeGifts(...args)).toEqual(expected);
40+
expect(distributeGiftsAlt(...args)).toEqual(expected);
41+
});
42+
});
43+
});

2022/challenge-04/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Box inside a box and another...
2+
3+
## 🔢 Instructions
4+
5+
Santa Claus needs to review his gift boxes to make sure he can pack them all in his sleigh. He has a series of **boxes of different sizes, characterized by their length, width, and height**.
6+
7+
Your task is to **write a function** that, given a list of boxes with their sizes, determines whether it is possible to pack **all the boxes in one so that each box contains another** (which in turn contains another, and so on).
8+
9+
Each box represents its measures with an object. For example: `{l: 2, w: 3, h: 2}`. This means that the box has a length of 2, a width of 3 and a height of 2.
10+
11+
A box fits into another box if all the sides of the first are smaller than the sides of the second. **The elves have told us that the boxes cannot be rotated**, so you cannot put a box of 2x3x2 in a box of 3x2x2. Let's see some examples:
12+
13+
```javascript
14+
fitsInOneBox([
15+
{ l: 1, w: 1, h: 1 },
16+
{ l: 2, w: 2, h: 2 }
17+
]); // true
18+
```
19+
20+
In the previous example, the smallest box fits into the largest box. Therefore, it is possible to pack all the boxes in one. Now let's see a case that does not:
21+
22+
```javascript
23+
const boxes = [
24+
{ l: 1, w: 1, h: 1 },
25+
{ l: 2, w: 2, h: 2 },
26+
{ l: 3, w: 1, h: 3 }
27+
];
28+
29+
fitsInOneBox(boxes); // false
30+
```
31+
32+
In the previous example, the smallest box fits into the middle box, but the middle box does not fit into the largest box. Therefore, it is not possible to pack all the boxes in one.
33+
34+
Note that the boxes may not come in order:
35+
36+
```javascript
37+
const boxes = [
38+
{ l: 1, w: 1, h: 1 },
39+
{ l: 3, w: 3, h: 3 },
40+
{ l: 2, w: 2, h: 2 }
41+
];
42+
43+
fitsInOneBox(boxes); // true
44+
```
45+
46+
In the previous example, the first box fits into the third, and the third into the second. Therefore, it is possible to pack all the boxes in one.
47+
48+
**Things to keep in mind**:
49+
50+
- The boxes cannot be rotated because the elves have told us that the machine is not ready.
51+
- The boxes may come in any order.
52+
- The boxes are not always squares, they could be rectangles.

2022/challenge-04/solution.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const fitsInOneBox = boxes => {
2+
const sortedBoxes = [...boxes].sort((firstBox, secondBox) => {
3+
return firstBox.l - secondBox.l;
4+
});
5+
6+
return sortedBoxes.every(({ l: currentBoxL, w: currentBoxW, h: currentBoxH }, index, boxesList) => {
7+
const { l: previousBoxL, w: previousBoxW, h: previousBoxH } = boxesList[index - 1] || {};
8+
9+
const isFirstBox = index === 0;
10+
const canCurrentBoxContainPreviousOne =
11+
currentBoxL > previousBoxL && currentBoxW > previousBoxW && currentBoxH > previousBoxH;
12+
13+
return isFirstBox || canCurrentBoxContainPreviousOne;
14+
});
15+
};
16+
17+
const fitsInOneBoxAlt = boxes => {
18+
const sortedBoxes = [...boxes].sort((firstBox, secondBox) => {
19+
return secondBox.l - firstBox.l;
20+
});
21+
22+
return sortedBoxes.slice(1).every(({ l: currentBoxL, w: currentBoxW, h: currentBoxH }, index) => {
23+
const { l: previousBoxL, w: previousBoxW, h: previousBoxH } = sortedBoxes[index] || {};
24+
return currentBoxL < previousBoxL && currentBoxW < previousBoxW && currentBoxH < previousBoxH;
25+
});
26+
};
27+
28+
const fitsInOneBoxSome = boxes => {
29+
const sortedBoxes = [...boxes].sort((firstBox, secondBox) => {
30+
return firstBox.l - secondBox.l;
31+
});
32+
33+
return !sortedBoxes.some((currentBox, index, sortedBoxesList) =>
34+
sortedBoxesList
35+
.slice(index + 1)
36+
.some(remainBox =>
37+
[remainBox.l > currentBox.l, remainBox.w > currentBox.w, remainBox.h > currentBox.h].includes(false)
38+
)
39+
);
40+
};
41+
42+
export { fitsInOneBox, fitsInOneBoxAlt, fitsInOneBoxSome };

2022/challenge-04/solution.spec.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { fitsInOneBox, fitsInOneBoxAlt, fitsInOneBoxSome } from './solution';
2+
3+
describe('Challenge 04: Box inside a box and another...', () => {
4+
describe('fitsInOneBox(...)', () => {
5+
const testCases = [
6+
createTestCase(
7+
[
8+
[
9+
{ l: 1, w: 1, h: 1 },
10+
{ l: 2, w: 2, h: 2 }
11+
]
12+
],
13+
true,
14+
'should fit in one box'
15+
),
16+
createTestCase(
17+
[
18+
[
19+
{ l: 1, w: 1, h: 1 },
20+
{ l: 2, w: 2, h: 2 },
21+
{ l: 3, w: 1, h: 3 }
22+
]
23+
],
24+
false,
25+
'should not fit in one box'
26+
),
27+
createTestCase(
28+
[
29+
[
30+
{ l: 1, w: 1, h: 1 },
31+
{ l: 2, w: 2, h: 2 },
32+
{ l: 2, w: 10, h: 2 }
33+
]
34+
],
35+
false,
36+
'should not fit in one box'
37+
),
38+
createTestCase(
39+
[
40+
[
41+
{ l: 1, w: 1, h: 1 },
42+
{ l: 3, w: 3, h: 3 },
43+
{ l: 2, w: 2, h: 2 }
44+
]
45+
],
46+
true,
47+
'should fit in one box'
48+
)
49+
];
50+
51+
it('#T should return a boolean', () => {
52+
expect(typeof fitsInOneBox([{ l: 1, w: 1, h: 1 }])).toBe('boolean');
53+
expect(typeof fitsInOneBoxAlt([{ l: 1, w: 1, h: 1 }])).toBe('boolean');
54+
expect(typeof fitsInOneBoxSome([{ l: 1, w: 1, h: 1 }])).toBe('boolean');
55+
});
56+
57+
it.each(testCases)('#$# $description', ({ args, expected }) => {
58+
expect(fitsInOneBox(...args)).toEqual(expected);
59+
expect(fitsInOneBoxAlt(...args)).toEqual(expected);
60+
expect(fitsInOneBoxSome(...args)).toEqual(expected);
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)