-1

Hello I looking for solution how I can sum up all the decimal values from collection where each char letter has its own value.

For example, lets say that I am looking to get the result for the word CAR. Because each letter represents its value I am looking to sum up the result and this should give an appropriate output. In this case 8.5. One more thing I am not looking for answer in LINQ.

Dictionary<char, double Letters = new Dictionary<char, double>(){
                {'A', 1.5}, {'C', 3.9}, {'R', 3.1}, 
            }; 

double result = CalculateScore("Car");

I would appreciate your help as I am new to C# and I have stuck with this task for while.

piotr
  • 85
  • 9

4 Answers4

1

Without Linq https://dotnetfiddle.net/lhxpuf

Dictionary<char, decimal> Letters = new Dictionary<char, decimal>(){
    {'A', 1.5m}, {'C', 3.9m}, {'R', 3.1m}, 
};

decimal result = 0;
string input = "CAR";
foreach (char item in input)
{
    if (Letters.ContainsKey(char.ToUpper(item)))
    {
        result += Letters[item];
    }
}

With Linq https://dotnetfiddle.net/Wxzy09

Dictionary<char, decimal> Letters = new Dictionary<char, decimal>(){
    {'A', 1.5m}, {'C', 3.9m}, {'R', 3.1m}, 
};
string input = "CAR";
decimal result = input.Where(x => Letters.ContainsKey(x)).Sum(x => Letters[x]);
fubo
  • 44,811
  • 17
  • 103
  • 137
1

Simply run through the characters of your word and get its value from the dictionary. When your current character however is lower-case you have to transform it to upper-case before.

double CalculateScore(string word)
{
    Dictionary<char, double> letters = new Dictionary<char, double>(){
            {'A', 1.5}, {'C', 3.9}, {'R', 3.1}, 
        }; 
    double sum = 0;
    for(int i = 0; i < word.Length; i++)
    {
        sum += letters[word[i].ToUpper()];
    }
    return sum;
}

Just for the sake of completeness a Linq-solution which can be used by targeting .NET-framework 3.5 or greater and a using for System.Linq:

double CalculateScore(string word)
{
    Dictionary<char, double> letters = new Dictionary<char, double>(){
            {'A', 1.5}, {'C', 3.9}, {'R', 3.1}, 
        }; 
    return word.Sum(x => letters[x].ToUpper());
}
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

First, let's change the dictionary to <char, double> as 8.5 is certainly not an int, then let's implement CalculateScore (assuming that 'a' and 'A' is the same):

private static Dictionary<char, double> Letters = new Dictionary<char, double>()
{
   {'A', 1.5}, {'C', 3.9}, {'R', 3.1}, 
};

private static double CalculateScore(string word)
{
    double result = 0;
    foreach(var c in word)
    {
       var upperChar = char.ToUpperInvariant(c);
       if(Letters.ContainsKey(upperChar)
       {
          result += Letters[upperChar];
       }                      
    }

    return result;
}

And now we can use it :

var result = CalculateScore("Car");
Fabjan
  • 13,506
  • 4
  • 25
  • 52
  • 2
    I wouldn't use a double for this kind of calculation. But it is good enough – Steve Apr 20 '17 at 09:20
  • Thanks a lot Fabjan for your response. That is working like a dream.Steve can I ask why you would not use it for decimal calculations? – piotr Apr 20 '17 at 09:33
  • http://stackoverflow.com/questions/329613/decimal-vs-double-speed and also http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when – Steve Apr 20 '17 at 09:49
0

Changed int to double because you have non int values in your dictionary.

private double CalculateScore(Dictionary<char, double> letters, string word)
{
    double sum = 0.0;

    foreach (char part in word)
    {
        if(letters.ContainsKey(char.ToUpper(part)))
        {
            sum += letters[part];
        }
    }

    return sum;
}

And call the method like

double sum = CalculateScrote(Letters, "Car");
Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51