Skip to content

Commit b2cb8a8

Browse files
author
Jakub Jirous
authored
Number Of Boomerangs (#23)
2 parents 6f7afbd + a2ef4c7 commit b2cb8a8

5 files changed

Lines changed: 93 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ Plus it has a gamified environment where you can gain XP, level up and unlock ac
4242

4343
1. [Seven Boom](/src/challenges/hard/01-seven-boom/INDEX.md)
4444
2. [Tower of Hanoi](/src/challenges/hard/02-tower-of-hanoi/INDEX.md)
45+
3. [Number Of Boomerangs](/src/challenges/hard/03-number-of-boomerangs/INDEX.md)

src/challenges/hard/02-tower-of-hanoi/tower-of-hanoi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export type Input = number;
22
export type Output = number;
33

44
/**
5-
*A function that takes a number of discs as an argument and returns the minimum amount of steps needed to complete the game.
5+
* A function that takes a number of discs as an argument and returns the minimum amount of steps needed to complete the game.
66
*
77
* @param discs
88
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Number Of Boomerangs
2+
3+
A boomerang is a V-shaped sequence that is either upright or upside down.
4+
5+
Specifically, a boomerang can be defined as: sub-array of length 3, with the first and last digits being the same and the middle digit being different.
6+
7+
Some boomerang examples:
8+
9+
```
10+
[3, 7, 3], [1, -1, 1], [5, 6, 5]
11+
```
12+
13+
Create a function that returns the total number of boomerangs in an array.
14+
15+
To illustrate:
16+
17+
- 3 boomerangs in this sequence: `[3, 7, 3]`, `[1, 5, 1]`, `[2, -2, 2]`
18+
```
19+
[3, 7, 3, 2, 1, 5, 1, 2, 2, -2, 2]
20+
```
21+
22+
Be aware that boomerangs can overlap, like so:
23+
24+
- 5 boomerangs (from left to right): `[1, 7, 1]`, `[7, 1, 7]`, `[1, 7, 1]`, `[7, 1, 7]`, and `[1, 7, 1]`
25+
```
26+
[1, 7, 1, 7, 1, 7, 1]
27+
```
28+
29+
---
30+
31+
### Examples:
32+
33+
```
34+
numberOfBoomerangs([9, 5, 9, 5, 1, 1, 1]) ➞ 2
35+
numberOfBoomerangs([5, 6, 6, 7, 6, 3, 9]) ➞ 1
36+
numberOfBoomerangs([4, 4, 4, 9, 9, 9, 9]) ➞ 0
37+
```
38+
39+
### Notes:
40+
41+
- `[5, 5, 5]` (triple identical digits) is NOT considered a boomerang because the middle digit is identical to the first and last.
42+
43+
---
44+
45+
### Solution:
46+
47+
- [Code](/src/challenges/hard/03-number-of-boomerangs/number-of-boomerangs.ts)
48+
- [Tests](/src/challenges/hard/03-number-of-boomerangs/test/number-of-boomerangs.test.ts)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export type Input = number[];
2+
export type Output = number;
3+
4+
/**
5+
* A function that returns the total number of boomerangs in an array.
6+
*
7+
* @param inputArray
8+
*/
9+
export const numberOfBoomerangs = (inputArray: Input): Output => {
10+
return inputArray.reduce((acc, value, index, array) => {
11+
return value !== array[index + 1] && value === array[index + 2]
12+
? acc + 1
13+
: acc;
14+
}, 0);
15+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Input, Output, numberOfBoomerangs } from "../number-of-boomerangs";
2+
3+
type Cases = [Input, Output][];
4+
5+
describe("Number Of Boomerangs", () => {
6+
const cases: Cases = [
7+
[[9, 5, 9, 5, 1, 1, 1], 2],
8+
[[5, 6, 6, 7, 6, 3, 9], 1],
9+
[[4, 4, 4, 9, 9, 9, 9], 0],
10+
[[5, 9, 5, 9, 5], 3],
11+
[[4, 4, 4, 8, 4, 8, 4], 3],
12+
[[2, 2, 2, 2, 2, 2, 3], 0],
13+
[[2, 2, 2, 2, 3, 2, 3], 2],
14+
[[1, 2, 1, 1, 1, 2, 1], 2],
15+
[[5, 1, 1, 1, 1, 4, 1], 1],
16+
[[3, 7, 3, 2, 1, 5, 1, 2, 2, -2, 2], 3],
17+
[[1, 7, 1, 7, 1, 7, 1], 5],
18+
[[5, 5, 5], 0],
19+
];
20+
21+
test.each(cases)(
22+
"for inputArray = %j output should be %d",
23+
(firstArg, expectedResult) => {
24+
const result = numberOfBoomerangs(firstArg);
25+
expect(result).toEqual(expectedResult);
26+
}
27+
);
28+
});

0 commit comments

Comments
 (0)