Skip to content

Commit 881a0b7

Browse files
committed
feat(2022): add challenge 13
1 parent 5281d85 commit 881a0b7

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

β€Ž2022/challenge-13/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Backups for Santa Claus files
2+
3+
## πŸ”’ Instructions
4+
5+
To avoid losing data when the server crashes, Santa Claus has decided to make incremental backups. A hacker called S4vitelf is helping him.
6+
7+
On one hand, we have the **timestamp** of when the last backup was made.
8+
9+
We also have the changes that have been made in an array of arrays. Each internal array contains **two elements**: the **id** of the modified file and the **timestamp** of the modification.
10+
11+
You have to create a program that returns an array with the **id^^ of the files that we would have to make backup because they have been modified since the last backup and **sorted in ascending order\*\*. Example:
12+
13+
```javascript
14+
const lastBackup = 1546300800;
15+
16+
const changes = [
17+
[3, 1546301100],
18+
[2, 1546300800],
19+
[1, 1546300800],
20+
[1, 1546300900],
21+
[1, 1546301000]
22+
];
23+
24+
getFilesToBackup(lastBackup, changes); // => [ 1, 3 ]
25+
// The file with id 1 has been modified twice
26+
// after the last backup.
27+
28+
// The file with id 2 has not been modified after
29+
// the last backup.
30+
31+
// The file with id 3 has been modified once
32+
// after the last backup.
33+
34+
// We have to make a backup
35+
// of the files 1 and 3.
36+
```
37+
38+
**Remember that**:
39+
40+
- Returns the id of the files that have been modified after the last backup.
41+
- Returns an empty array if there are no files to make backup.
42+
- Remember to sort ids in ascending order.

β€Ž2022/challenge-13/solution.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const getFilesToBackup = (lastBackup, changes) => {
2+
const fileIds = changes.filter(change => change[1] > lastBackup).map(change => change[0]);
3+
4+
const filesToBackup = new Set(fileIds);
5+
6+
return [...filesToBackup].sort((fileA, fileB) => fileA - fileB);
7+
};
8+
9+
export { getFilesToBackup };

β€Ž2022/challenge-13/solution.spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { getFilesToBackup } from './solution';
2+
3+
describe('Challenge 13: Backups for Santa Claus files', () => {
4+
describe('getFilesToBackup(...)', () => {
5+
const testCases = [
6+
createTestCase(
7+
[
8+
1546300800,
9+
[
10+
[2, 1546300800],
11+
[3, 1546301100],
12+
[1, 1546300800],
13+
[1, 1546300900],
14+
[1, 1546301000]
15+
]
16+
],
17+
[1, 3],
18+
'should return an array of file ids that were changed after the last backup (1)'
19+
),
20+
createTestCase(
21+
[
22+
1546300600,
23+
[
24+
[1, 1546300800],
25+
[2, 1546300800],
26+
[1, 1546300900],
27+
[1, 1546301000],
28+
[3, 1546301100]
29+
]
30+
],
31+
[1, 2, 3],
32+
'should return an array of file ids that were changed after the last backup (2)'
33+
),
34+
createTestCase(
35+
[
36+
1556300600,
37+
[
38+
[1, 1546300800],
39+
[2, 1546300800],
40+
[1, 1546300900],
41+
[1, 1546301000],
42+
[3, 1546301100]
43+
]
44+
],
45+
[],
46+
'should return an empty array if there are no changes after the last backup'
47+
),
48+
createTestCase([1556300600, []], [], 'should return an empty array if there are no changes')
49+
];
50+
51+
it('#T should return an array', () => {
52+
expect(getFilesToBackup(10000, [])).toBeInstanceOf(Array);
53+
});
54+
55+
it.each(testCases)('#$# $description', ({ args, expected }) => {
56+
expect(getFilesToBackup(...args)).toEqual(expected);
57+
});
58+
});
59+
});

β€ŽREADME.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
6969
| 10 | [The Santa Claus sleigh jump](2022/challenge-10) | 🟠 | [Show](2022/challenge-10/solution.js) |
7070
| 11 | [Santa Claus is Scrum Master](2022/challenge-11) | πŸ”΄ | [Show](2022/challenge-11/solution.js) |
7171
| 12 | [Electric sleighs, wow!](2022/challenge-12) | 🟠 | [Show](2022/challenge-12/solution.js) |
72+
| 13 | [Backups for Santa Claus files](2022/challenge-13) | 🟒 | [Show](2022/challenge-13/solution.js) |
7273

7374
[^1]: **Difficulty**: 🟒 Easy 🟠 Medium πŸ”΄ Hard 🟣 Very Hard
7475

0 commit comments

Comments
Β (0)