2016-11-09 3 views
0

が、私はそうのような小惑星のオブジェクトのリストを持っているとしましょう:保管foreachループ内の前の要素のトラック

  • 9_Amphitrite
  • 24_Themis
  • 259_Aletheia
  • 31_Euphrosyne
  • 511_Davida
  • 87_Sylvia
  • 9メチス
  • 41_Daphne

各小惑星がタイトルStartRoationPeriod、およびEndRoationPeriodを持っています。

現在の小惑星StartRoationPeriodと以前の小惑星EndRoationPeriodが軌道定数にどのくらい近づいているかに基づいて名前を連結し、連結したタイトルを吐き出す必要があります。

したがって、上記のリストで、最後のオブジェクトは次のようになります。

  • を9_Amphitrite
  • 24_Themis; 259_Aletheia
  • 31_Euphrosyne; 511_Davida; 87_Sylvia
  • 9_Metis
  • 41_Daphne

これは私が追跡しなければならない現在の小惑星と以前の小惑星の両方の

私はループを書き始めましたが、現在の小惑星が前回の小惑星の終わりの回転周期と比較して回転周期を開始する場所や方法を確かめています...

私はこれがあなたのために働くべきだと思い
 string asteroid_title = string.Empty; 
     Asteroid prev_asteroid = null; 

     foreach (var asteroid in SolarSystem) 
     { 
      if (prev_asteroid != null) 
      { 
       if (asteroid.StartRoationPeriod + OrbitalConstant >= prev_asteroid.EndRoationPeriod) 
       { 
         asteroid_title = asteroid_title + asteroid.Title; 

       } else { 
         asteroid_title = asteroid.Title; 
         yield return CreateTitle(); 
       } 
      } 
      prev_evt = evt; 
     } 

答えて

1

(集計はforeachのに変換するために、あまりにも複雑な試みに見える場合、それは簡単です)

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace Program 
{ 
    class Asteroid 
    { 
     public int EndRoationPeriod { get; internal set; } 
     public string Name { get; internal set; } 
     public int StartRoationPeriod { get; internal set; } 
    } 

    class AsteroidGroup 
    { 
     public int EndRoationPeriod { get; internal set; } 
     public string Names { get; internal set; } 
    } 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      int OrbitalConstant = 10; 
      List<Asteroid> SolarSystem = new List<Asteroid>() 
      { 
       new Asteroid() { Name= "9_Amphitrite" ,StartRoationPeriod=10 ,EndRoationPeriod=50}, 
       new Asteroid() { Name= "24_Themis" ,StartRoationPeriod=45,EndRoationPeriod=100}, 
       new Asteroid() { Name= "259_Aletheia",StartRoationPeriod=40 ,EndRoationPeriod=150}, 
       new Asteroid() { Name= "31_Euphrosyne" ,StartRoationPeriod=60,EndRoationPeriod=200}, 
       new Asteroid() { Name= "511_Davida" ,StartRoationPeriod=195,EndRoationPeriod=250}, 
       new Asteroid() { Name= "87_Sylvia" ,StartRoationPeriod=90,EndRoationPeriod=300}, 
       new Asteroid() { Name= "9_Metis" ,StartRoationPeriod=100,EndRoationPeriod=350}, 
       new Asteroid() { Name= "41_Daphne" ,StartRoationPeriod=110,EndRoationPeriod=400}, 
      }; 

      var result = //I skip the first element because I initialize a new list with that element in the next step 
          SolarSystem.Skip(1) 
          //The first argument of Aggregate is a new List with your first element 
          .Aggregate(new List<AsteroidGroup>() { new AsteroidGroup { Names = SolarSystem[0].Name, EndRoationPeriod = SolarSystem[0].EndRoationPeriod } }, 
          //foreach item in your list this method is called,l=your list and a=the current element  
          //the method must return a list           
          (l, a) => 
          { 
          //Now this is your algorithm 
          //Should be easy to undrestand 

           var last = l.LastOrDefault(); 
           if (a.StartRoationPeriod + OrbitalConstant >= last.EndRoationPeriod) 
           { 
            last.Names += " " + a.Name; 
            last.EndRoationPeriod = a.EndRoationPeriod; 
           } 
           else 
            l.Add(new AsteroidGroup { Names = a.Name, EndRoationPeriod = a.EndRoationPeriod }); 

           //Return the updated list so it can be used in the next iteration 
           return l; 
          }); 

よりコンパクトなソリューション

var result = SolarSystem 
      .Skip(1) 
      .Aggregate(SolarSystem.Take(1).ToList(), 
         (l, a) => (a.StartRoationPeriod + OrbitalConstant >= l[l.Count - 1].EndRoationPeriod) ? 
         (l.Take(l.Count - 1)).Concat(new List<Asteroid> { new Asteroid() { Name = l[l.Count - 1].Name += " " + a.Name, EndRoationPeriod = a.EndRoationPeriod } }).ToList() : 
          l.Concat(new List<Asteroid> { a }).ToList() 
         ); 
+0

うわー、それはたくさんのコードです。 .Skip(1)と.Takeの行は何を行い、行(l、a)=>は何をしますか?ありがとう! – SkyeBoniwell

+0

@SkyeBoniwellいくつかのコメントを追加しました。コンパクトコードを無視して、それはちょうど楽しみのためでした –

+0

コメントありがとうございました。どのような変数、結果ですか?それは単なる文字列ですか?ありがとう – SkyeBoniwell

関連する問題