In a six digit (or larger) number, locate the greatest sequence of five consecutive digits found within the number given.
Example
In the following 10 digit number:
1234567890
67890 is the greatest sequence of 5 consecutive digits.
Tests
digit5(7316717)
digit5(1234567898765)
This challenge comes from jhoffner on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (8)
Javascript version:
Rust
🔗 the below code in a runnable playground
"imperative"
iterator
unwrapping is safe, because even with
0
we will have at least one element(actually we will consider
0
15 times)immutable
bad: leads to dividing bigger numbers
strings
good: can be longer than u64
usage
Here is a Python solution,
Haskell
To be honest, the solutions I provide here would NEVER pass code review. But I get it working at a REPL, and it's just too tempting to post as-is 😊
Here is a nearly identical solution (sans the transducer), written in a readable way:
Ah, it's just a simple application of a sliding window algorithm.