Prolog - 170 bytes
[]/[].
T/R:-0*_*T*R.
C*X*[X|T]*R:-(C+1)*X*T*R.
C*X*T*Y:-10*C+X+Y+R,T/R.
N+R+A:-N=:=0,A=R;D is N mod 10,N//10+R+[D|A].
F-C-R:-C/N,(N=F,R=C;F-N-R).
[1]-[1,1]. S-T:-S-[1]-T.
This snipped defines the function (-)/2
. You can invoke it like
?- [1,1,1,3,2,1,3,2,1,1]-T.
T = [1, 3, 1, 1, 2, 2, 2, 1] .
?- [1]-T.
T = [1, 1] .
There seems to be only one lengths in this sequence with an odd parity: the initial [1]
.
wr_len :- wr_len(1, [1]).
wr_len(N, Cur) :-
length(Cur, Len),
TrailingZeroes is lsb(Len),
(TrailingZeroes > 0 -> Par = 'even'; Par = 'odd'),
writef('%t\t%t\t%t\t%t\n', [N, Len, Par, TrailingZeroes]),
get_next(Cur, Next),
succ(N, O),
!, wr_len(O, Next).
% index, length, parity of length, num of trailing 0 in bin presentation of length
?- wr_len.
1 1 odd 0
2 2 even 1
3 2 even 1
4 4 even 2
5 6 even 1
6 6 even 1
7 8 even 3
8 10 even 1
9 14 even 1
10 20 even 2
11 26 even 1
12 34 even 1
13 46 even 1
14 62 even 1
15 78 even 1
16 102 even 1
17 134 even 1
18 176 even 4
19 226 even 1
20 302 even 1
21 408 even 3
22 528 even 4
23 678 even 1
24 904 even 3
25 1182 even 1
26 1540 even 2
27 2012 even 2
28 2606 even 1
29 3410 even 1
30 4462 even 1
31 5808 even 4
32 7586 even 1
33 9898 even 1
34 12884 even 2
35 16774 even 1
36 21890 even 1
37 28528 even 4
38 37158 even 1
39 48410 even 1
40 63138 even 1
41 82350 even 1
42 107312 even 4
43 139984 even 4
44 182376 even 3
45 237746 even 1
46 310036 even 2
47 403966 even 1
48 526646 even 1
49 686646 even 1
50 894810 even 1
51 1166642 even 1
52 1520986 even 1
53 1982710 even 1
54 2584304 even 4
55 3369156 even 2
56 4391702 even 1
57 5724486 even 1
58 7462860 even 2
59 9727930 even 1
ERROR: Out of global stack
% I added a few "strategic" cuts (`!`) to get so far.
Readable:
get_next([], []).
get_next(Current, Next) :-
get_next_sub(0, _, Current, Next).
get_next_sub(Length, Digit, [Digit|Tail], Result) :-
get_next_sub(Length+1, Digit, Tail, Result).
get_next_sub(Length, Digit, Further, Result) :-
number_to_list(10*Length+Digit, Result, ResultTail),
get_next(Further, ResultTail).
number_to_list(Number, Result, Accumulator) :-
0 is Number -> Result = Accumulator;
Digit is Number mod 10,
number_to_list(Number // 10, Result, [Digit|Accumulator]).
get_previous(Stop, Current, Result) :-
get_next(Current, Next),
( Next = Stop
-> Result = Current
; get_previous(Stop, Next, Result)
).
get_prev_or_next(Input, Result) :-
length(Input, Length),
( 1 is Length mod 2
-> get_next(Input, Result)
; get_previous(Input, [1], Result)
).
1I'm confused. Can you explain how
513111
divides into51
,32
and11
? – r3mainer – 2014-11-27T22:44:03.9731I feel like this is a combined duplicate of some look and say sequence challenge and a run-length decoding challenge (I'm sure we had those). – Martin Ender – 2014-11-27T22:57:40.847
3What would the predecessor of
11111111111111
be? According to your spec, it would be1111111
. You should modify your specification to define a reasonable answer for this. – TheNumberOne – 2014-11-27T23:44:24.1131@TheBestOne
11111111111111
simply has no predecessor. It's an illegal input. – kay - SE is evil – 2014-11-28T00:37:46.3972@TheBestOne Yes this is correct, I defined an arbitrary rule for the predecessor that does not always match a 'real' predecessor. – flawr – 2014-11-28T09:19:52.850