2011-01-13 10 views

Odpowiedz

36
var array = new int[] { 1, 2, 3, 4, 7, 8, 11, 15, 16, 17, 18 }; 

var result = string.Join(",", array 
    .Distinct() 
    .OrderBy(x => x) 
    .GroupAdjacentBy((x, y) => x + 1 == y) 
    .Select(g => new int[] { g.First(), g.Last() }.Distinct()) 
    .Select(g => string.Join("-", g))); 

z

public static class LinqExtensions 
{ 
    public static IEnumerable<IEnumerable<T>> GroupAdjacentBy<T>(
     this IEnumerable<T> source, Func<T, T, bool> predicate) 
    { 
     using (var e = source.GetEnumerator()) 
     { 
      if (e.MoveNext()) 
      { 
       var list = new List<T> { e.Current }; 
       var pred = e.Current; 
       while (e.MoveNext()) 
       { 
        if (predicate(pred, e.Current)) 
        { 
         list.Add(e.Current); 
        } 
        else 
        { 
         yield return list; 
         list = new List<T> { e.Current }; 
        } 
        pred = e.Current; 
       } 
       yield return list; 
      } 
     } 
    } 
} 
+1

Dziękuję, to zaoszczędziło dużo czasu. – nRk

1

Jaki algorytm chcesz zastosować? Dowiedz się, co chcesz zrobić, a następnie sprawdź, czy można go lepiej wyjaśnić za pomocą tłumaczenia LINQ. Oto coś innego niż LINQ, które może dać ci pomysł.

int[] array = { 1, 2, 3, 4, 7, 8, 11, 15, 16, 17, 18}; 
List<string> ranges = new List<string>(); 

// code assumes array is not zero-length, is distinct, and is sorted. 
// to do: handle scenario as appropriate if assumptions not valid 

Action<int, int, List<string>> addToRanges = (first, last, list) => 
{ 
    if (last == first) 
     list.Add(last.ToString()); 
    else 
     list.Add(string.Format("{0}-{1}", first, last)); ; 
}; 

int firstItem = array[0]; 
int lastItem = firstItem; 
foreach (int item in array.Skip(1)) 
{ 
    if (item > lastItem + 1) 
    { 
     addToRanges(firstItem, lastItem, ranges); 
     firstItem = lastItem = item; 
    } 
    else 
    { 
     lastItem = item; 
    } 
} 

addToRanges(firstItem, lastItem, ranges); 

// return ranges or ranges.ToArray() 
+0

Chcę zaimplementować to, co jest bardziej przejrzyste, próbowałem czegoś z grupą linq, ale bez powodzenia – Alexandre

4

Nie trzeba Linq; w rzeczywistości najłatwiejsze rozwiązanie wymaga znajomości trzech pozycji w tablicy (twój numer początkowy, aktualny numer i następny numer po bieżącym), dla których enumerables nie są dobrze dopasowane.

Spróbuj tego:

var start = 0; 
var end = 0; 
var write = false; 
var builder = new StringBuilder(); 
for(var i=0; i<array.Length; i++) 
{ 
    //arranged this way to avoid ArrayOutOfBoundException 
    //if the next index doesn't exist or isn't one greater than the current, 
    //the current index is the end of our incremental range. 
    if(i+1 == array.Length || array[i+1] > array[i] + 1) 
    { 
     end = i; 
     write = true; 
    } 

    if(write) 
    { 
     if(end - start == 0) //one number 
     builder.Append(String.Format("{0}, ", array[start]); 
     else //multi-number range 
     builder.Append(String.Format("{0}-{1}, ", array[start], array[end]); 

     start = i+1; 
     end = i+1; //not really necessary but avoids any possible case of counting backwards 
     write = false; 
    } 

} 

Możesz zmienić to w celu zmniejszenia zagnieżdżania kodu, continue wcześnie logiki pętli i usunąć kilka vars; zyskasz kilka milionów czasu egzekucji. Będziesz także musiał przyciąć ostatnie dwa znaki (końcowy przecinek i spację) z końca StringBuilder, zanim wyciągniesz String.

+0

_Nie potrzebujesz Linq_? Podczas gdy twoja wersja jest całkowicie w porządku, ideologiczne rozwiązanie LINQ ma tę zaletę, że możesz z łatwością przesłać takie rozwiązanie do dowolnego funkcjonalnego języka programowania, takiego jak F # lub Haskell. – mbx

1

Oto cięcie na to:

public static IEnumerable<string> ToRanges(this IEnumerable<int> values) 
{ 
    int? start = null, end = null; 
    foreach (var value in values.OrderBy(vv => vv)) 
    { 
     if (!start.HasValue) 
     { 
      start = value; 
     } 
     else if (value == (end ?? start) + 1) 
     { 
      end = value; 
     } 
     else 
     { 
      yield return end.HasValue 
       ? String.Format("{0}-{1}", start, end) 
       : String.Format("{0}", start); 
      start = value; 
      end = null; 
     } 
    } 

    if (start.HasValue) 
    { 
     yield return end.HasValue 
      ? String.Format("{0}-{1}", start, end) 
      : String.Format("{0}", start); 
    } 
}