The look and say sequence is a sequence in which each number is the result of a "look and say" operation on the previous element.
For example, starting with "1": ["1", "11", "21, "1211", "111221", ...]. You can see that the second element describes the first as "1(times number)1", the third is "2(times number)1" describing the second, the fourth is "1(times number)2(and)1(times number)1" and so on.
Your goal is to create a function which takes a starting string (not necessarily the classical "1") and return the n
th element of the series.
Examples:
lookAndSaySequence("1", 1) === "1"
lookAndSaySequence("1", 3) === "21"
lookAndSaySequence("1", 5) === "111221"
lookAndSaySequence("22", 10) === "22"
lookAndSaySequence("14", 2) === "1114"
Good luck!
This challenge comes from giacomosorbi 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 (5)
Here's a one line solution in javascript.
Zero readability, maximum coolness. 😎
I completely didn't understand the description here... For those also struggling, here's the explanation from wikipedia for what a "Look and Say sequence" is:
Actually, following this, isn't the above code incorrect? In that:
lookAndSaySequence("1", 1) === "1"
should be:
lookAndSaySequence("1", 1) === "11"
As it's "one 1"?
I think you're approaching the problem from an array offset point of view (start at 0) where 0 should return the input with no transformations applied. Likewise, 2 would give the result after two iterations.
The problem statement is approaching it from an ordinal perspective. In their view, 1 should be the un-mutated input.
I think they tried to call this out by noting
"1": ["1", "11", "21, "1211", "111221", ...]
, but I agree that it isn't very clear from the text alone.Kotlin, because I'm prepping a mini talk on declarative style...
SPOILERS:
base64decode('SXQncyBqdXN0IHJ1bi1sZW5ndGgtZW5jb2Rpbmch')
bonus:
Rust: play.rust-lang.org/?version=stable...