Home > database >  How do combine a dictionary with a function?
How do combine a dictionary with a function?

Time:08-24

I want to set each letter of the alphabet to a numeric value: a = 1, b = 2, c = 3, etc.

I created a dictionary to set each letter to a value. I created a function that takes in a word and finds the total value of its letters. How do I access my created dictionary within my function?


Console.WriteLine("YOUR NAME VALUE IS: "   hashFunction("b"));//I want the output to be 2 (b = 2) in this case.

int hashFunction(string name)
{
    int totalSum = 0;
    Char [] characters = name.ToCharArray();
    for (int letterIndex = 0; letterIndex < characters.Length; letterIndex  )
    {
        totalSum  = Convert.ToInt32(characters[letterIndex]);
    }

    return totalSum;
}

Dictionary<char, int> charValues = new Dictionary<char, int>();

        charValues.Add('a', 1);
        charValues.Add('b', 2);
        charValues.Add('c', 3);
        charValues.Add('d', 4);
        charValues.Add('e', 5);
        //etc etc etc
    }

CodePudding user response:

Since letters a..z within ASCII table are consequent we can compute letter index as

letter - 'a'   1

without elaborated mappig based on dictionary. Then we can use Linq to find the Sum:

using System.Linq;

...

// static : we don't want "this" within the method
static int hashFunction(string name) => name
  .Where(c => c >= 'a' && c <= 'z')
  .Sum(c => c - 'a'   1);

Edit: if you insist on dictionary based solution, you can put it like this:

using System.Linq;
  
... 

static IReadOnlyDictionary<char, int> charValues = Enumerable
  .Range('a', 'z' - 'a'   1)
  .ToDictionary(letter => (char)letter, letter - 'a'   1);

int hashFunction(string name)
{
  int totalSum = 0;

  foreach (char c in name) 
    if (charValues.TryGetValue(c, out int v))
      totalSum  = v;    

  return totalSum;
}

CodePudding user response:

Why do you need a dictionary?

IEnumerable<int> MapCharsToInts( IEnumerable<char> chars )
{
  foreach ( char ch in chars.Map( c => Char.ToLower(c) )
  {
    int v = 1   ch - 'a' ;
    yield v >= 1 && v <= 26 ? v : 0 ;
  }
}

CodePudding user response:

How do I access my created dictionary within my function?

by the name of the variable. You use the [ ] brakets like in an array, but you pass a key values as parameter into it. In your case one of the chars that you retrieve from your char array. You can actually use the string directly to access the chars, since it is a char array under the hood:

int hashFunction(string name)
{
    int totalSum = 0;
    for (int letterIndex = 0; letterIndex < name.Length; letterIndex  )
    {
        char theKey = name[letterIndex]; 
        totalSum  = charValues[theKey];
    }

    return totalSum;
}

the dictionary will then return the appropriate integer, or it will throw an exception if it does not find any match.

The most important part is that the dictionary has to be declared as field or property on class level! not a local variable in some other method.

  • Related