Skip to content

Commit

Permalink
Find the first non-consecutive number
Browse files Browse the repository at this point in the history
  • Loading branch information
PheRum committed May 20, 2024
1 parent 900f3fb commit 84c42ea
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
24 changes: 24 additions & 0 deletions 8_kyu/Find the first non-consecutive number/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Find the first non-consecutive number

https://www.codewars.com/kata/58f8a3a27a5c28d92e000144

Your task is to find the first element of an array that is not consecutive.

By not consecutive we mean not exactly 1 larger than the previous element of the array.

E.g. If we have an array [1,2,3,4,6,7,8] then 1 then 2 then 3 then 4 are all consecutive but 6 is not, so that's the first non-consecutive number.

If the whole array is consecutive then return null2.

The array will always have at least 2 elements1 and all elements will be numbers. The numbers will also all be unique and in ascending order. The numbers could be positive or negative and the first non-consecutive could be either too!

If you like this Kata, maybe try this one next: https://www.codewars.com/kata/represent-array-of-numbers-as-ranges

1 Can you write a solution that will return null2 for both [] and [ x ] though? (This is an empty array and one with a single number and is not tested for, but you can write your own example test. )

2
Swift, Ruby and Crystal: nil
Haskell: Nothing
Python, Rust, Scala: None
Julia: nothing
Nim: none(int) (See options)
13 changes: 13 additions & 0 deletions 8_kyu/Find the first non-consecutive number/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { firstNonConsecutive } from "./index";

describe("Tests", () => {
it("a simple example", function () {
const first = firstNonConsecutive([1, 2, 3, 4, 6, 7, 8]);
expect(first).toBe(6);
});

it("all sequential", function () {
const first = firstNonConsecutive([1, 2, 3, 4]);
expect(first).toBe(null);
});
});
9 changes: 9 additions & 0 deletions 8_kyu/Find the first non-consecutive number/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function firstNonConsecutive(arr: number[]) {
for (let i = 1; i < arr.length; i++) {
if (arr[i - 1] + 1 !== arr[i]) {
return arr[i];
}
}

return null;
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

### Katas solved

`Total`: 49 \
`8_kyu`: 47 \
`Total`: 50 \
`8_kyu`: 48 \
`7_kyu`: 0 \
`6_kyu`: 2 \
`5_kyu`: 0 \
Expand Down

0 comments on commit 84c42ea

Please sign in to comment.