Non-continuous subsequences: Difference between revisions
Content added Content deleted
(jq) |
m ({{out}}) |
||
Line 6: | Line 6: | ||
A ''continuous'' subsequence is one in which no elements are missing between the first and last elements of the subsequence. |
A ''continuous'' subsequence is one in which no elements are missing between the first and last elements of the subsequence. |
||
Note: Subsequences are defined ''structurally'', not by their contents. |
Note: Subsequences are defined ''structurally'', not by their contents. |
||
So a sequence ''a,b,c,d'' will always have the same subsequences and continuous subsequences, no matter which values are substituted; it may even be the same value. |
|||
'''Task''': Find all non-continuous subsequences for a given sequence. Example: For the sequence ''1,2,3,4'', there are five non-continuous subsequences, namely ''1,3''; ''1,4''; ''2,4''; ''1,3,4'' and ''1,2,4''. |
'''Task''': Find all non-continuous subsequences for a given sequence. Example: For the sequence ''1,2,3,4'', there are five non-continuous subsequences, namely ''1,3''; ''1,4''; ''2,4''; ''1,3,4'' and ''1,2,4''. |
||
Line 52: | Line 53: | ||
end Test_Non_Continuous;</lang> |
end Test_Non_Continuous;</lang> |
||
{{out}} |
|||
Sample output: |
|||
<pre style="height:30ex;overflow:scroll"> 1 3 |
<pre style="height:30ex;overflow:scroll"> 1 3 |
||
Line 120: | Line 120: | ||
# OD # ) |
# OD # ) |
||
END; test non continuous</lang> |
END; test non continuous</lang> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
aeiu |
aeiu |
||
Line 196: | Line 196: | ||
# OD # ) |
# OD # ) |
||
)</lang> |
)</lang> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
iu |
iu |
||
Line 275: | Line 275: | ||
NEXT s% |
NEXT s% |
||
ENDPROC</lang> |
ENDPROC</lang> |
||
{{out}} |
|||
'''Output:''' |
|||
<pre> |
<pre> |
||
For [1, 2, 3, 4] non-continuous subsequences are: |
For [1, 2, 3, 4] non-continuous subsequences are: |
||
Line 324: | Line 324: | ||
); |
); |
||
</lang> |
</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>e n t |
<pre>e n t |
||
e n |
e n |
||
Line 523: | Line 523: | ||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>Non-continuous subsequences of 0 1 2 3 4 |
<pre>Non-continuous subsequences of 0 1 2 3 4 |
||
- 0 2 |
- 0 2 |
||
Line 627: | Line 627: | ||
</lang> |
</lang> |
||
{{out}} |
|||
output |
|||
< |
<pre> |
||
> coffee non_contig_subseq.coffee |
> coffee non_contig_subseq.coffee |
||
[ [ 1, 3 ], |
[ [ 1, 3 ], |
||
Line 645: | Line 645: | ||
for n=9 there are 466 solutions |
for n=9 there are 466 solutions |
||
for n=10 there are 968 solutions |
for n=10 there are 968 solutions |
||
</ |
</pre> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 808: | Line 808: | ||
masks(length(List))).</lang> |
masks(length(List))).</lang> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Eshell V5.10.1 (abort with ^G) |
Eshell V5.10.1 (abort with ^G) |
||
Line 858: | Line 858: | ||
} |
} |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
5 non-continuous subsequences: |
5 non-continuous subsequences: |
||
Line 882: | Line 882: | ||
ncsubseq = fenceM [((:), action even), (flip const, action odd)] (>= 3) 0</lang> |
ncsubseq = fenceM [((:), action even), (flip const, action odd)] (>= 3) 0</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>*Main> ncsubseq [1..3] |
<pre>*Main> ncsubseq [1..3] |
||
[[1,3]] |
[[1,3]] |
||
Line 914: | Line 913: | ||
nc xs (y:ys) = (nc (xs++[y]) ys) ++ map (xs++) (tail $ poset ys)</lang> |
nc xs (y:ys) = (nc (xs++[y]) ys) ++ map (xs++) (tail $ poset ys)</lang> |
||
{{out}} |
|||
Output: |
|||
<pre> |
|||
*Main> ncsubs "aaa" |
*Main> ncsubs "aaa" |
||
["aa"] |
["aa"] |
||
Line 928: | Line 927: | ||
[] |
[] |
||
(0.00 secs, 0 bytes) |
(0.00 secs, 0 bytes) |
||
</pre> |
|||
A disjointed subsequence is a consecutive subsequence followed by a gap, |
A disjointed subsequence is a consecutive subsequence followed by a gap, |
||
then by any nonempty subsequence to its right: |
|||
<lang haskell>import Data.List (subsequences, tails, delete) |
<lang haskell>import Data.List (subsequences, tails, delete) |
||
Line 939: | Line 940: | ||
main = print $ length $ disjoint [1..20]</lang> |
main = print $ length $ disjoint [1..20]</lang> |
||
Build a lexicographic list of consecutive subsequences, |
Build a lexicographic list of consecutive subsequences, |
||
and a list of all subsequences, then subtract one from the other: |
|||
<lang haskell>import Data.List (inits, tails) |
<lang haskell>import Data.List (inits, tails) |
||
Line 1,033: | Line 1,035: | ||
print(JSON.stringify( non_continuous_subsequences( powerset([1,2,3,4]))));</lang> |
print(JSON.stringify( non_continuous_subsequences( powerset([1,2,3,4]))));</lang> |
||
{{out}} |
|||
Outputs: |
|||
<pre>[[1,3],[1,4],[2,4],[1,2,4],[1,3,4]]</pre> |
<pre>[[1,3],[1,4],[2,4],[1,2,4],[1,3,4]]</pre> |
||
Line 1,103: | Line 1,105: | ||
echo "ncsub(", toSeq 1.. 4, ") = ", ncsub(toSeq 1..4) |
echo "ncsub(", toSeq 1.. 4, ") = ", ncsub(toSeq 1..4) |
||
echo "ncsub(", toSeq 1.. 5, ") = ", ncsub(toSeq 1..5)</lang> |
echo "ncsub(", toSeq 1.. 5, ") = ", ncsub(toSeq 1..5)</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>ncsub(@[1, 2, 3]) = @[@[1, 3]] |
<pre>ncsub(@[1, 2, 3]) = @[@[1, 3]] |
||
ncsub(@[1, 2, 3, 4]) = @[@[1, 2, 4], @[1, 3, 4], @[1, 3], @[1, 4], @[2, 4]] |
ncsub(@[1, 2, 3, 4]) = @[@[1, 2, 4], @[1, 3, 4], @[1, 3], @[1, 4], @[2, 4]] |
||
Line 1,135: | Line 1,137: | ||
let ncsubseq = fence 0</lang> |
let ncsubseq = fence 0</lang> |
||
{{out}} |
|||
Output: |
|||
<pre># ncsubseq [1;2;3];; |
<pre># ncsubseq [1;2;3];; |
||
- : int list list = [[1; 3]] |
- : int list list = [[1; 3]] |
||
Line 1,187: | Line 1,188: | ||
nonContigSubseq([1,2,3]) |
nonContigSubseq([1,2,3]) |
||
nonContigSubseq(["a","b","c","d","e"])</lang> |
nonContigSubseq(["a","b","c","d","e"])</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>[1, 3] |
<pre>[1, 3] |
||
Line 1,239: | Line 1,240: | ||
say ~ non_continuous_subsequences( 1..4 )».perl; |
say ~ non_continuous_subsequences( 1..4 )».perl; |
||
say ~ non_continuous_subsequences( ^4 ).map: {[<a b c d>[.list]].perl};</lang> |
say ~ non_continuous_subsequences( ^4 ).map: {[<a b c d>[.list]].perl};</lang> |
||
{{out}} |
|||
<pre>[1, 3] |
|||
[1, 3] [1, 4] [2, 4] [1, 2, 4] [1, 3, 4] |
[1, 3] [1, 4] [2, 4] [1, 2, 4] [1, 3, 4] |
||
["a", "c"] ["a", "d"] ["b", "d"] ["a", "b", "d"] ["a", "c", "d"]</pre> |
["a", "c"] ["a", "d"] ["b", "d"] ["a", "b", "d"] ["a", "c", "d"]</pre> |
||
Line 1,291: | Line 1,293: | ||
ncsubseq([1 2 3 4 5]) =></lang> |
ncsubseq([1 2 3 4 5]) =></lang> |
||
{{out}} |
|||
Output: |
|||
< |
<pre>[[1 3] [1 4] [2 4] [1 2 4] [1 3 4] [1 5] [2 5] [1 2 5] [3 5] [1 3 5] |
||
[2 3 5] [1 2 3 5] [1 4 5] [2 4 5] [1 2 4 5] [1 3 4 5]]</ |
[2 3 5] [1 2 3 5] [1 4 5] [2 4 5] [1 2 4 5] [1 3 4 5]]</pre> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Line 1,414: | Line 1,416: | ||
return [[]] if s >= 3 else []</lang> |
return [[]] if s >= 3 else []</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>>>> ncsub(range(1, 4)) |
<pre>>>> ncsub(range(1, 4)) |
||
[[1, 3]] |
[[1, 3]] |
||
Line 1,548: | Line 1,549: | ||
/*──────────────────────────────────S subroutine───────────────────────*/ |
/*──────────────────────────────────S subroutine───────────────────────*/ |
||
s: if arg(1)==1 then return ''; return word(arg(2) 's',1) /*plurals.*/</lang> |
s: if arg(1)==1 then return ''; return word(arg(2) 's',1) /*plurals.*/</lang> |
||
{{out}} when using the input: <tt> 1 2 3 4 </tt> |
|||
<pre style="overflow:scroll"> |
<pre style="overflow:scroll"> |
||
list= 1 2 3 4 |
list= 1 2 3 4 |
||
Line 1,560: | Line 1,561: | ||
5 non-continuous subsequences were found. |
5 non-continuous subsequences were found. |
||
</pre> |
</pre> |
||
{{out}} when using the following input: <tt> a e I o u </tt> |
|||
<pre style="overflow:scroll"> |
<pre style="overflow:scroll"> |
||
list= a e I o u |
list= a e I o u |
||
Line 1,583: | Line 1,584: | ||
16 non-continuous subsequences were found. |
16 non-continuous subsequences were found. |
||
</pre> |
</pre> |
||
{{out}} when using the [channel Islands (Great Britain)] as input: <tt> Alderney Guernsey Herm Jersey Sark </tt> |
|||
<pre style="overflow:scroll"> |
<pre style="overflow:scroll"> |
||
list= Alderney Guernsey Herm Jersey Sark |
list= Alderney Guernsey Herm Jersey Sark |
||
Line 1,606: | Line 1,607: | ||
16 non-continuous subsequences were found. |
16 non-continuous subsequences were found. |
||
</pre> |
</pre> |
||
{{out}} when using the following [six noble gases] as input: <tt> helium neon argon krypton xenon radon </tt> |
|||
<pre style="overflow:scroll"> |
<pre style="overflow:scroll"> |
||
list= helium neon argon krypton xenon radon |
list= helium neon argon krypton xenon radon |
||
Line 1,727: | Line 1,728: | ||
(recurse (+ s 1) xs)))))))</lang> |
(recurse (+ s 1) xs)))))))</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>> (ncsubseq '(1 2 3)) |
<pre>> (ncsubseq '(1 2 3)) |
||
((1 3)) |
((1 3)) |
||
Line 1,803: | Line 1,803: | ||
fun ncsubseq xs = fence 0 xs</lang> |
fun ncsubseq xs = fence 0 xs</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>- ncsubseq [1,2,3]; |
<pre>- ncsubseq [1,2,3]; |
||
val it = [[1,3]] : int list list |
val it = [[1,3]] : int list list |
||
Line 1,853: | Line 1,852: | ||
examples = noncontinuous 'abcde'</lang> |
examples = noncontinuous 'abcde'</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>abce |
<pre>abce |
||
abd |
abd |