tags:

views:

60

answers:

3

I am trying to create the below matrix in my vb.net so during processing I can get the match scores for the alphabets, for example: What is the match for A and N?, I will look into my inbuilt matrix and return -2 Similarly, What is the match for P and L?, I will look into my inbuilt matrix and return -3

Please suggest me how to go about it, I was trying to use nested dictionary like this:

Dim myNestedDictionary As New Dictionary(Of String, Dictionary(Of String, Integer))()
Dim lTempDict As New Dictionary(Of String, Integer)
lTempDict.Add("A", 4)
myNestedDictionary.Add("A", lTempDict)

The other way could be is to read the Matrix from a text based file and then fill the two dimensional array.

Thanks.

alt text

+3  A: 

Use a two dimensional array and create a mapping for your alphabet as an enumeration that will also serve as an index into the arrays:

public enum AlphaBet
{
   A = 0,
   R = 1,
   ...
}

// Init the array
int[][] scores = ...;

int score = scores[AlphaBet.A][AlphaBet.N]; // score = -2
Oded
Why create an enum instead of simply using `char` as key?
Fredrik Mörk
@Frederic - I don't follow. How would you use `char` as key?
Oded
I suppose it's because there is a somewhat limited amount of characters being used. Building an array that covers all characters from A-Z would consume too much memory. The alternative then, is to use a indexed-lookup approach, storing only indexes to the characters that are used but then.... that is precisely what Enum offers.
chakrit
@Frederic - This is so you can refer to the array indexes as the alphabet location.
Oded
@Oded: yes, I realized later why you did what you did; I glanced over the code too quickly before. It does make sense (even though I would probably go for another solution myself).
Fredrik Mörk
Thanks Oded. I am still trying to understand your solution.
anshu
+1  A: 

take one dictionary that maps char to int. the ints must be sequential. then take 2d array, thats it. first you look into dictionary for index for both chars then go to array

Andrey
+5  A: 

I would think it would be simpler to use an un-nested dictionary with two-character keys.

myDictionary.add("AA", 4)
Carl Manaster
+1 I was just about to suggest this.
Taylor Leese
Same thought here.
chakrit
On the right track, but given the size of the data I think a SortedList might do better.
Joel Coehoorn
Thanks Carl, it is definitely 'simple' solution and will work for my present scenario. But it is hardcoding the match values, what if my matrix changes over the time.
anshu
@anshu, how would the nested dictionary better handle changes over time? I don't see the advantage - or the difficulty of handling it with this approach. If AA changes to hold 7 instead of 4, you just change it. But I probably misunderstand.
Carl Manaster
@Carl, I agree what you are saying. Actually, I need to read the values from input text file(which contains the matrix) and then loop through to do the following:myDictionary.add("AA", 4) and so on.
anshu