C#練習問題解答:得点が一番高い言葉【難易度:2級】--景越C#クラシックプログラミング問題ライブラリ、1000本のC#基礎練習問題などに挑戦してください


得点が一番高い言葉【難易度:2級】:
答え1:
using System.Linq;
using System;

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

答え2:
public class Kata
{
  public static string High(string s)
  {
      string[] words = s.Split(' ');
      string highestWord = "";
      int highestNumber = 0;
      
      foreach (string word in words)
      {
           int number = WordsToMarks(word);
           if (highestNumber < number)
           {
               highestWord = word;
               highestNumber = number;
           }
      }
      return highestWord;
  }
  
  public static int WordsToMarks(string str)
  {
      int n = 0;
      string abc = "abcdefghijklmnopqrstuvwxyz";
      foreach (char x in str)
      {
          n += abc.IndexOf(x) + 1;
      }
      return n;
  }
  
}

答え3:
using System.Linq;

public class Kata
{
  private static int Score(string s) 
    => s.Select(e=> e - 96).Sum(); 
    
  public static string High(string s)
   => s.Split(' ').Aggregate((r,e)=> Score(r) < Score(e) ? e : r);
}

答え4:
using System.Linq;

public class Kata
{
  public static string High(string s) => s.Split(' ').OrderBy(w => w.Sum(c => c - 'a' + 1)).Last();
}

答え5:
using System;
using System.Linq;

public class Kata
{
  public static string High(string s)
  {
      return s
          .Split(" ", StringSplitOptions.RemoveEmptyEntries)
          .Select(
              (word, position) =>
              (
                  Word: word,
                  Score: word.Sum(c => c - 'a'),
                  Position: position
              ))
          .OrderByDescending(t => t.Score)
          .ThenBy(t => t.Position)
          .First().Word;    
  }
}

答え6:
using System;

public class Kata {
    private const int ASCII_LETTER_START_VALUE = 96;
    
    public static string High(string s) {
        string result = String.Empty;
        int highestScore = 0;
        int score = 0;
        string[] sArray = s.Split();
        foreach(string element in sArray) {
            score = WordScore(element);
            if(score > highestScore) {
                result = element;
                highestScore = score;
            }
        }
        return result;
    }

    private static int WordScore(string s) {
        int result = 0;
        for(int i = 0; i < s.Length; i++)
            result += (s[i] - ASCII_LETTER_START_VALUE);
        return result;
    }
}

答え7:
using System.Linq;

public class Kata
{
  public static string High(string s)
  {
    var words = s.Split(' ');

            int maxPosition = 0;
            int maxValue = -1;

            for (int i = 0; i < words.Length; i++)
            {
                int wordValue = words[i].Sum(x => (int)x - 96);

                if (wordValue > maxValue)
                {
                    maxValue = wordValue;
                    maxPosition = i;
                }
            }

            return words[maxPosition];
  }
}

答え8:
using System;
public class Kata
{
  public static string High(string s)
  {
    string[] split = s.Split(' ');
    int sum = 0;
    string word = "";
    foreach(string data in split){
    int tempSum = 0;
      for(int i = 0 ; i< data.Length;i++){
        for(int c = 97;c<=122;c++){
          if(data[i] == c){
            tempSum += data[i] - 96;
          }
        }
      }
      if(tempSum > sum){
        sum = tempSum;
        word = data;
        tempSum = 0;
      }
    }
    Console.WriteLine(s);
    return word;
  }
}

答え9:
using System.Collections.Generic;
using System.Linq;
public class Kata
{
  public static string High(string s)
  {
            List<KeyValuePair<string, int>> wordScoringList = new List<KeyValuePair<string, int>>();
            string[] words = s.Split(' ');
            for (int w = 0; w < words.Length; w++)
            {
                int currentSum = 0;
                string currentWord = words[w];
                for (int ch = 0; ch < currentWord.Length; ch++)
                {

                    currentSum += (char.ToUpper(currentWord[ch]) - 64);
                }
                wordScoringList.Add(new KeyValuePair<string, int>(currentWord, currentSum));
            }
            var maxWord = wordScoringList.OrderByDescending(x => x.Value).First().Key;
            return maxWord;
  }
}

答え10:
using System.Collections.Generic;
using System;

public class Kata
        {
            public static string High(string s)
            {
                Dictionary<char, int> values = new Dictionary<char, int>();
                values.Add('a', 1);
                values.Add('b', 2);
                values.Add('c', 3);
                values.Add('d', 4);
                values.Add('e', 5);
                values.Add('f', 6);
                values.Add('g', 7);
                values.Add('h', 8);
                values.Add('i', 9);
                values.Add('j', 10);
                values.Add('k', 11);
                values.Add('l', 12);
                values.Add('m', 13);
                values.Add('n', 14);
                values.Add('o', 15);
                values.Add('p', 16);
                values.Add('q', 17);
                values.Add('r', 18);
                values.Add('s', 19);
                values.Add('t', 20);
                values.Add('u', 21);
                values.Add('v', 22);
                values.Add('w', 23);
                values.Add('x', 24);
                values.Add('y', 25);
                values.Add('z', 26);

                string[] strings = s.Split(" ");
                int[] scores = new int[strings.Length];

                for (int i = 0; i < strings.Length; i++)
                {
                    foreach(char letter in strings[i])
                    {
                        scores[i] += values[letter];
                    }
                }

                int highScore = 0;
                int indexOfHighScore = 0;

                foreach(int score in scores)
                {
                    if (score > highScore)
                    {
                        highScore = score;
                        indexOfHighScore = Array.IndexOf(scores, score);
                    }
                }

                return strings[indexOfHighScore];
            }
        }