2016-10-25 77 views
-2

我只是想知道,基於這個規則的最好的詞是什麼:(不同的字母在一個單詞中)^ 2 /(單詞中的全部字母)。所以我開始了一些基本的東西排行榜與不同的列表

 StreamReader SR = new StreamReader (FILE_PATH); 
     int counter = 0; 
     string line; 

     List<string> words = new List<string>(); 
     List<double> points = new List<double>(); 

     while ((line = SR.ReadLine()) != null) { 
      short unique = (short)line.Distinct().ToArray().Length; 
      short total = (short)line.Length; 
      double value = (15 * Math.PI * Math.Pow (unique, 2))/total; 

      words.Add (line); 
      points.Add (value); 

      counter++; 
     } 

     int Index = points.IndexOf (points.Max()); 
     Console.WriteLine ("best: " + words [Index] + " points: " + points [Index]); 
    } 

但我也想有一個排行榜與「最好」的話和相對點。我腦子裏有一個想法,那就是需要不同的列表來找出有關點的單詞,但是有沒有更簡單快捷的方法呢?

回答

0

我建議使用Dictionary<string, double>及其對應),而不是拉鍊Lists

Dictionary<string, double> points = File 
    .ReadLines(FILE_PATH) 
//.Where(line => !string.IsNullOrEmpty(line)) // you may want to filter out empty lines 
    .Select(line => new { 
    word = line, 
    unique = word.Distinct().Count() 
    total = word.Length }) 
    .ToDictionary(item => item.word, 
       item => 15 * Math.PI * item.unique * item.unique/item.total); 

所以,你可以很容易地實現與一扶龍頭板Linq

var TopTen = points 
    .OrderByDescending(pair => pair.Value) 
    .ThenBy(pair => pair.Key) 
    .Take(10) 
    .Select(pair => $"word: {pair.Key} points: {pair.Value}"); 

    Console.Write(String.Join(Environment.NewLine, TopTen));