使用其键从字典中选择最高值



因此,目的是将单词分为字母,并根据每个字母的价值给出一个分数。我有这个工作,并将其用作词典将其作为键和单词作为值。我需要找到仅使用提供字母的最高评分单词(每个只使用了一次),如果有多个分数具有相同的分数,则需要打印到字典中的第一个。代码如下;

    Dictionary<string, int> wordHolder = new Dictionary<string, int>();

    int N = int.Parse(Console.ReadLine());
    for (int i = 0; i < N; i++)
    {
        string W = Console.ReadLine();

        char[] splitWord = W.ToCharArray();
        Console.Error.WriteLine(W);

        int points = splitWord.Sum(c => letterPoints.First(kvp => kvp.Value.Contains(c)).Key);
        wordHolder.Add(W, points);
        Console.Error.WriteLine(points);
    }
    string LETTERS = Console.ReadLine();
    Console.Error.WriteLine(LETTERS);

字母是单个字符串中提供的字符

您可以通过降序订购并获取第一个元素:

wordHolder.OrderByDescending(x => x.Value).First();

或通过上升并获取最后一个元素:

wordHolder.OrderBy(x => x.Value).Last();
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication9
{
  internal static class Program
  {
    private static void Main()
    {
      //  --- building up the data so the next (repeated) steps are efficient: ---
      // your words and points
      var wordHolder = new Dictionary<string, int>();
      // INSERT DATA INTO wordHolder HERE
      // create a dictionary where you can access all words and their points 
      // efficiently by the letters you have available:
      var efficientAccessByLetters = wordHolder.Select(entry =>
                                                new
                                                {
                                                  Index = new string(entry.Key.OrderBy(c => c).ToArray()),
                                                  Word = entry.Key,
                                                  Points = entry.Value
                                                })
                                                .GroupBy(x => x.Index)
                                                .ToDictionary(x => x.Key,
                                                              x => x.OrderByDescending(p => p.Points).ToList());
      //  --- Repeat this steps as many times as you like: ---
      while (true)
      {
        Console.WriteLine("Enter letters available:");
        // get letters that you want to build he best word from:
        var availableLetters = Console.ReadLine();
        // normalize the letters for fast index search by sorting them
        var normalized = new string(availableLetters.OrderBy(c => c).ToArray());
        if (!efficientAccessByLetters.ContainsKey(normalized))
        {
          Console.WriteLine("Sorry, no matching words found.");
        }
        else
        {
          // find all words and their respective points by index access
          var wordsThatMatchLetters = efficientAccessByLetters[normalized];
          // as words are sorted by points, just get the first
          var best = wordsThatMatchLetters.First();
          // output
          Console.WriteLine("Best Match: {0} for {1} points", best.Word, best.Points);
        }
      }
    }
  }
}

在没有字典的情况下做到这一点的最佳方法。

 public static string High(string s)
 {
    return s.Split(' ').OrderByDescending(a => a.Select(b => b - 96).Sum()).Last();
 }

最新更新