Anonymous user
Non-continuous subsequences: Difference between revisions
m
Fixed lang tags.
(add JavaScript) |
m (Fixed lang tags.) |
||
Line 295:
Here, solution sequences are calculated abstractly by <tt>ncs</tt>, then used by <tt>ncs_of</tt> to draw items from the input list.
The algorithm is filtered templates. As marked by sections, <tt>ncs</tt> (a) makes all possible sub-sequences of the given length, (b) retains those that contain an internal gap, then (c) returns a list of their index-lists.
<lang
Examples:
<lang j> ncs 4
=={{header|JavaScript}}==
Line 350:
<lang Mathematica>GoodBad[i_List]:=Not[MatchQ[Differences[i],{1..}|{}]]
n=5
Select[Subsets[Range[n]],GoodBad]</lang>
gives back:
<lang Mathematica> {{1,3},{1,4},{1,5},{2,4},{2,5},{3,5},{1,2,4},{1,2,5},{1,3,4},{1,3,5},{1,4,5},{2,3,5},{2,4,5},{1,2,3,5},{1,2,4,5},{1,3,4,5}}
=={{header|OCaml}}==
Line 401 ⟶ 400:
variables to keep track if subsequence is continuous.
<
lvars acc = [], gap_started = false, is_continuous = true;
define do_it(l1, l2);
Line 424 ⟶ 423:
enddefine;
ncsubseq([1 2 3 4 5]) =></
Output:
<
[2 3 5] [1 2 3 5] [1 4 5] [2 4 5] [1 2 4 5] [1 3 4 5]]</
|