• 3 aug 2010
Lengths of winning strikes

The question is: given a sequence with only 1 and _1, determine (the lengths of) all subsequences which add up to 1. After this sum a new subsequence starts.

```Example:

1 _1 1 _1 1 1 _1 1 1 1

+-+-----------+------+-+
|1|_1 1 _1 1 1|_1 1 1|1|
+-+-----------+------+-+

with lengths

1 5 3 1```

I found a very elegant solution.

```   t=:-(_1++:)10000 ?.@\$ 2

+/\t
1 0 1 0 _1 _2 _1 0 1 0 1 0 1 2 1 2 3 4 5 6 5 4 3 4 5 6 7 8 7 6 7 6 5 6 7 6 7 8 7 8 7 8 9 10 11 10 11 10 11 12 11 12 13 12 13 14 ...

(i. >:@i.@(>./)) +/\ t
0 13 16 17 18 19 26 27 42 43 44 49 52 55 82 85 88 99 812 813 814 815 960 963 964 967 974 977 978 1287 1288 1289 1290 1291 1292 ...```

As soon as you realize that if each sequence adds up to 1, than the consecutive sequences add up to 1, 2, 3, ...
So one has to find the first occurrence of 1, 2, 3, ... up to the maximum of +/\t , which is done by (i. >:@i.@(>./)) .

The lengths are of course given by

```   2-~/\ (i.>:@i.@(>./)) +/\ t
13 3 1 1 1 7 1 15 1 1 5 3 3 27 3 3 11 713 1 1 1 145 3 1 3 7 3 1 309 1 1 1 1 1 1 5 1 23 3 759 1 15 1 1 1 1 5 5 1 5 7 15 5 1 5 3 9 25 1345 ...```

Below the graph of +/\t .

RE Boss/J-blog/length_of_winning_strike (last edited 2010-08-03 20:09:45 by RE Boss)