Note that there are some explanatory texts on larger screens.

plurals
  1. POFind all consecutive numbers in an int[]
    primarykey
    data
    text
    <p>I want to find all the consecutive numbers in a an ordered int[8] of casual numbers from 0 to 31. The consecutive numbers must be from minimum length of 3 and a max of 5 numbers.</p> <p>In the example the last give me very real problem.</p> <p>ex:</p> <pre><code>int[] = new int[] { 3,7,14,16,23, 28, 29 ,30 } // no result (28,29 is length of 2 numbers) int[] = new int[] { 4,5,6,7,18, 19, 20 ,21 } // 4,5,6,7 (yes! length of 4!!) int[] = new int[] { 2.3.4.5.6.7.8.9 } // two results : 2,3,4 and 5,6,7,8,9 </code></pre> <p>I don't want <em>the solution</em> but just an example of how approach the question because I'm trying using generals and I'm really stuck!</p> <p>Really thanks for help!</p> <p>Christian</p> <p>-this is the code from where I started (not soup from my kitchen)</p> <pre><code>public partial class Test2 : Form { public Test2() { InitializeComponent(); } private void Test2_Load(object sender, EventArgs e) { int[] numbers = new[] { 21, 4, 5, 22, 17, 6, 20, 23 }; // int[] numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8 }; foreach (Campo r in FindRanges(numbers, 3)) { listBox1.Items.Add(string.Join(", ", r.Select(x =&gt; x.ToString()).ToArray())); } } struct Campo : IEnumerable&lt;int&gt; { readonly int _start; readonly int _count; public Campo(int start, int count) { _start = start; _count = count; } public int Start { get { return _start; } } public int Count { get { return _count; } } public int End { get { return _start + _count - 1; } } public IEnumerator&lt;int&gt; GetEnumerator() { for (int i = 0; i &lt; _count; ++i) { yield return _start + i; } } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } public static Campo operator +(Campo x, int y) { return new Campo(x.Start, x.Count + y); } public Campo removefirst() { return new Campo(this.Start + 3, this.Count); } public Campo removelast() { return new Campo(this.Start, this.Count - 1); } } static IEnumerable&lt;Campo&gt; FindRanges(IEnumerable&lt;int&gt; source, int minCount) { var ordered = source.OrderBy(x =&gt; x); Campo r = default(Campo); foreach (int value in ordered) { if (r.Count == 0) { r = new Campo(value, 1); continue; } if (r.Count == 5) { r = r.removefirst(); continue; } if (value == r.End) { continue; } if ((value == 0 || value == 8 || value == 16 || value == 24) &amp;&amp; (r.Count &gt; minCount)) { continue; } if ((value == 7 || value == 15 || value == 23 || value == 31) &amp;&amp; (r.Count == 1)) { continue; } else if (value == r.End + 1) { r += 1; } else { if (r.Count &gt;= minCount) { yield return r; } r = new Campo(value, 1); } } if (r.Count &gt;= minCount) { yield return r; } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload