24
1
Background
The Look and Say Sequence is built up by reading off the digits of previous entries in the sequence, counting the number of digits in groups of the same digit. For example:
1 => 11 # (one 1)
11 => 21 # (two 1s)
21 => 1211 # (one 2, then one 1)
1211 => 111221 # (one 1, then one 2, then two 1s)
This sequence was the subject of various previous challenges. A simple Python 3 implementation of the generating function is:
>> from itertools import groupby
>> def look_and_say(obj):
return "".join(f"{len(list(group))}{digit}" for digit, group in groupby(str(obj)))
Challenge
The purpose of this challenge is to implement an inverse Look and Say function. Given a Look and Say representation of a non-negative integer, your code should return the smallest non-negative integer for which the input is its Look and Say. Note that this is not the same as general Run Length Decoding, since the output must encode back precisely to the input: see the examples below for some of the subtleties involved.
This is code-golf, so shortest code in bytes wins, with all the standard loopholes forbidden. Input can be passed in (or returned) as either an integer or a string.
Test cases
10 => 0 # (1 zero)
12 => 2 # (1 two)
123 => 333333333333 # (12 threes)
1234 => 2444 # (1 two, 3 fours)
1232 => 222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222 # (123 twos, since 2222 would return 42 not 1232)
2019 => 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 # (201 nines, since the only non-negative starting with 0 is 0)
2109 => 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 # (210 nines, since 11 would return 21 not 2109)
123456 => 244466666
123454 => 333333333333444444444444444444444444444444444444444444444 # (12 threes, 45 fours)
124234 => 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222444 (124 twos, 3 fours, which is smaller than the alternatives)
Invalid inputs
Since there are no integers which have the following Look and Say representations, the behaviour for these inputs is undefined:
1
3
20
100
1010
10010
2I'd like to recommend some more test cases:
22334
,33224
,10101
and10111
, I think they would cover some more edge cases. – flawr – 2019-11-21T12:37:35.260With an input of 121212, I assume that a response of 312 (three 12's) is not a valid response? – Keeta - reinstate Monica – 2019-11-22T12:46:01.050
1@Keeta That goes the wrong way. It's 312 as input does not have 121212 as a valid response. – Taemyr – 2019-11-22T13:59:17.410