Non-continuous subsequences: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 471: | Line 471: | ||
% ./a.out 1 2 3 4 5 6 7 8 9 0 | wc -l |
% ./a.out 1 2 3 4 5 6 7 8 9 0 | wc -l |
||
968</pre> |
968</pre> |
||
=={{header|C sharp}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
class Program |
|||
{ |
|||
public static void Main() { |
|||
var sequence = new[] { "A", "B", "C", "D" }; |
|||
foreach (var subset in Subsets(sequence.Length).Where(s => !IsContinuous(s))) { |
|||
Console.WriteLine(string.Join(" ", subset.Select(i => sequence[i]))); |
|||
} |
|||
} |
|||
static IEnumerable<List<int>> Subsets(int length) { |
|||
int[] values = Enumerable.Range(0, length).ToArray(); |
|||
var stack = new Stack<int>(length); |
|||
for (int i = 0; stack.Count > 0 || i < length; ) { |
|||
if (i < length) { |
|||
stack.Push(i++); |
|||
yield return (from index in stack.Reverse() select values[index]).ToList(); |
|||
} else { |
|||
i = stack.Pop() + 1; |
|||
if (stack.Count > 0) i = stack.Pop() + 1; |
|||
} |
|||
} |
|||
} |
|||
static bool IsContinuous(List<int> list) => list[list.Count - 1] - list[0] + 1 == list.Count; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
A B D |
|||
A C |
|||
A C D |
|||
A D |
|||
B D |
|||
</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 515: | Line 555: | ||
<pre> |
<pre> |
||
2147483151 |
2147483151 |
||
</pre> |
|||
=={{header|C sharp}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
class Program |
|||
{ |
|||
public static void Main() { |
|||
var sequence = new[] { "A", "B", "C", "D" }; |
|||
foreach (var subset in Subsets(sequence.Length).Where(s => !IsContinuous(s))) { |
|||
Console.WriteLine(string.Join(" ", subset.Select(i => sequence[i]))); |
|||
} |
|||
} |
|||
static IEnumerable<List<int>> Subsets(int length) { |
|||
int[] values = Enumerable.Range(0, length).ToArray(); |
|||
var stack = new Stack<int>(length); |
|||
for (int i = 0; stack.Count > 0 || i < length; ) { |
|||
if (i < length) { |
|||
stack.Push(i++); |
|||
yield return (from index in stack.Reverse() select values[index]).ToList(); |
|||
} else { |
|||
i = stack.Pop() + 1; |
|||
if (stack.Count > 0) i = stack.Pop() + 1; |
|||
} |
|||
} |
|||
} |
|||
static bool IsContinuous(List<int> list) => list[list.Count - 1] - list[0] + 1 == list.Count; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
A B D |
|||
A C |
|||
A C D |
|||
A D |
|||
B D |
|||
</pre> |
</pre> |
||
Line 1,407: | Line 1,407: | ||
a c d e |
a c d e |
||
</pre> |
</pre> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<lang M2000 Interpreter> |
<lang M2000 Interpreter> |
||
Line 1,746: | Line 1,747: | ||
{{out}} |
{{out}} |
||
<pre>found 1048365 sequences</pre> |
<pre>found 1048365 sequences</pre> |
||
=={{header|Perl 6}}== |
|||
{{works with|rakudo|2015-09-24}} |
|||
<lang perl6>sub non_continuous_subsequences ( *@list ) { |
|||
@list.combinations.grep: { 1 != all( .[ 0 ^.. .end] Z- .[0 ..^ .end] ) } |
|||
} |
|||
say non_continuous_subsequences( 1..3 )».gist; |
|||
say non_continuous_subsequences( 1..4 )».gist; |
|||
say non_continuous_subsequences( ^4 ).map: {[<a b c d>[.list]].gist};</lang> |
|||
{{out}} |
|||
<pre>((1 3)) |
|||
((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> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,081: | Line 2,068: | ||
;; => '((1 2 4) (1 3 4) (1 3) (1 4) (2 4)) |
;; => '((1 2 4) (1 3 4) (1 3) (1 4) (2 4)) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|rakudo|2015-09-24}} |
|||
<lang perl6>sub non_continuous_subsequences ( *@list ) { |
|||
@list.combinations.grep: { 1 != all( .[ 0 ^.. .end] Z- .[0 ..^ .end] ) } |
|||
} |
|||
say non_continuous_subsequences( 1..3 )».gist; |
|||
say non_continuous_subsequences( 1..4 )».gist; |
|||
say non_continuous_subsequences( ^4 ).map: {[<a b c d>[.list]].gist};</lang> |
|||
{{out}} |
|||
<pre>((1 3)) |
|||
((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> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,375: | Line 2,377: | ||
seqR("1234", "", 0, 0) |
seqR("1234", "", 0, 0) |
||
}</lang> |
}</lang> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{trans|Generalized monadic filter}} |
{{trans|Generalized monadic filter}} |