2016-04-11 25 views
0

kullanarak tuples bir listesini toplam değerleri Merhaba, yapmaya çalıştığım şey umarım çok basit, bir Listem var (string, ondalık) ve ilk örneğinin toplamını almaya çalışıyorum heres benzer bir soru sorarak önceki sonrası, mükerrer olarak işaretleyiniz yokKoşullu olarak LINQ veya Döngüler

 static void Main(string[] args) 
{ 

     var wordList = new List<string>(); 

     List<Tuple<string, double>> items = new List<Tuple<string, double>>() 
{ 

new Tuple<string,double>("q", .5), //3.5 - .5 = 3 
new Tuple<string,double>("w", 1.5), // 3 - 1.5 = 1.5 
new Tuple<string,double>("e", .7), // .7 - 1.5 = .8 
new Tuple<string,double>("r", .8), // .8 - .8 = 0 
    /* 
look for 'q', if found start at 'q', add values in Item2 until 
sum <= 3.5, put 'w', 'e', 'r' on a string List, once it stops, 
look for 'w' in the string list, if found --> //code, then, because 
the list is long and has many values, repeat the proces through the 
entire list, continuously looking for strings that meet the <= 3.5 sum 

Scenario example: 

the first occurence of 'q' is in index 0, find it 
add .5 and check if <= 3.5 is true, 
if so-continue, 'w', value is 1.5, add to sum (.5) = 2 
2 <= 3.5, add 'w' into wordList, and repeat 
'e', .7, .7 + 2.7 <= 3.5, add 'e' into wordList, continue... 

after the conditions are met 
wordList should have: 

'w' 
'e' 
'r', check list: 
'if any items in wordList contains 'w' --> code 
else --> if contains 'e' --> //code 
else --> if contains 'q' --> //code 

'if 'q', 'w', and 'e' are not present... 
keep checking and keep checking EVEN if 
any were found, we want to go through the entire list 
expecting to find some 

*/ 

new Tuple<string,double>("q", .5), 
/* ok here's the next set, found this 'q', .5 <= 3.5, go to next 
'w' 1.5 + .5 = 2 < =3.5, go to next 
'q' .5 + 2 = 2.5 < 3.5, note that 'q' was added, but not the found 'q' 
when searching for 'q', don't add it to wordlist if found, add subsequent 
'q''s until <= 3.5 

*/ 
new Tuple<string,double>("ba", .5), 
new Tuple<string,double>("w", 1.5), 
new Tuple<string,double>("q", .7), 
new Tuple<string,double>("r", .8), 
new Tuple<string,double>("ba", .5) 


}; 

for (int i = 0; i < wordList.Count; i++) 
     { 
      switch (wordList[i]) 
      { 
       case "w": 
        //code 
        break; 

       case "e": 
        //code 
        break; 

      } 

     } 

, deniyorum: 'q', < = 3.5 yanlıştır kadar, bu böyle bir şey olmazdı onun değer katmak ve devam Son benzer yazıyı silmek için, bu ana gönderi. Doğru dileğin anlasalardı Conditionally sum values in a list of tuples

+1

Kişisel algoritması benim için çok açık değildir. Belki de neyi başarmak istediğini açıklayabilirsin. Bir tuple listesinde –

+0

, 'q' değerini bulun ve bu değeri bir değere ekleyin, eğer <= 3.5, bir sonraki indise 'w' giderseniz ve toplamı bir önceki toplamlara ekleyin, .5 + 1.5, eğer hala <= 3.5 ise , devam, bir kez 3.5 yanlış, 'w' ve 'e' oluşumu için wordlist'i kontrol edin, eğer varsa, anahtar deyimime göre kod yapın ve listeyi tam olarak arayana kadar tekrarlayın, 'w' ve 'e' bulunamadı ama –

cevap

0

, ana işlevi aşağıdaki kodu eklemeyi deneyin:

counter = 3.5 
foreach (Tuple<string,double> element in items) 
{ 
     counter -= element[1]; 
     if (counter>=0) 
     { 
      wordList.add(element[0]); 
     } 
     else 
     { 
      break; 
     } 

} 
+0

'u kontrol etmeye devam et evet! Ben bu cevabı hissediyorum, ama eğer T_T listesinde 100 öğe varsa, sadece bu değil, bir sonraki çekime gitmeden önce bu işlevi değerlendirmek istiyorum, // bu kontrol için kod yap, liste tamamen araştırıldı ve teşekkürler! –