Jump to content

C# Sorting a list by amount of occurring letters in a string


Tom

Recommended Posts

Aint in a thinking mood, been thinking all day.


Say I have a Dictionairy<char, int> values, and each letter has an amount of points you score for each occurrence in a string

A=1,B=2,C=3,D=4,E=5,F=6,G=7,H=8,I=9,J=10,K=11,L=12,M=13,N=14,O=15,P=16,Q=17,R=18,S=19,T=20,U=21,V=22,W=23,X=24,Y=25,Z=26
Imagine that being loaded into a the dictionary pretty much. The word OSBot would give 15 + 19 + 2 + 15 + 20 = 71 points

 

So now I have a list of words, the words are completely random but they are real words.
I want to sort this list by the amount of points I would obtain per word.

The amount of points per letter arent always alphabetical, so A could be 30 points and Z could be 29 points, its random.

 

If anyone knows of a good way to do this, or needs a better explanation, let me know. LINQ stuff is preferred.

 

 

Edit: I'm onto something literally 1 minute after posting this thread.
Calculate the points per word, then add it to a dictionary Word, Points, then sort that dictionary by the amount of points.

Just delete this shit I was too tired for my own good

Edited by Tom
  • Like 1
Link to comment
Share on other sites

Just now, HunterRS said:

Your edit is pretty much heads on, python would be a lot easier coding wise if you can use it :p

They are both incredibly high level languages, not sure why it would make any difference.

 

O(n) would be simplest, just putting the string to a char array then adding from each occurrence in the Dictionary (map?). I'm sure there is a BST way which is O(n log n).

Link to comment
Share on other sites

2 minutes ago, Alek said:

They are both incredibly high level languages, not sure why it would make any difference.

 

O(n) would be simplest, just putting the string to a char array then adding from each occurrence in the Dictionary (map?). I'm sure there is a BST way which is O(n log n).

Syntax wise python would be just shorter, and I am lazy :) 

EDIT: Dictionary is map ya

Edited by HunterRS
Link to comment
Share on other sites

Just now, Alek said:

They are both incredibly high level languages, not sure why it would make any difference.

 

O(n) would be simplest, just putting the string to a char array then adding from each occurrence in the Dictionary (map?). I'm sure there is a BST way which is O(n log n).

Just ended up doing

 

                foreach(String w in words) {
                    int points = 0;
                    char[] c = w.ToCharArray();
                    foreach(char x in c) {
                        points += pointsPerCharacter[x.ToString()];
                    }

                    wordPoints.Add(w, points);
                }

                var topWords = wordPoints.OrderByDescending(pair => pair.Value);

 

As long as it works for now, I'm satisfied. Leaving assignments to the last minute, I'm sure you know how it is

Link to comment
Share on other sites

13 minutes ago, Tom said:

Just ended up doing

 


                foreach(String w in words) {
                    int points = 0;
                    char[] c = w.ToCharArray();
                    foreach(char x in c) {
                        points += pointsPerCharacter[x.ToString()];
                    }

                    wordPoints.Add(w, points);
                }

                var topWords = wordPoints.OrderByDescending(pair => pair.Value);

 

As long as it works for now, I'm satisfied. Leaving assignments to the last minute, I'm sure you know how it is

Alek does have a point thought, this might be good for now but there will be a time when you will be asked to do it with a specific run time function (Sorry if that is not the name in English, I mean O(n) or O(n^2) something like that) and you will want to know how to go about doing that. Might aswell start as soon as possible though ofcurse if it is time sensitive and not preformance sensitive then fuck all that shit :) .

Edited by HunterRS
  • Like 1
Link to comment
Share on other sites

6 minutes ago, Tom said:

Just ended up doing

 


                foreach(String w in words) {
                    int points = 0;
                    char[] c = w.ToCharArray();
                    foreach(char x in c) {
                        points += pointsPerCharacter[x.ToString()];
                    }

                    wordPoints.Add(w, points);
                }

                var topWords = wordPoints.OrderByDescending(pair => pair.Value);

 

As long as it works for now, I'm satisfied. Leaving assignments to the last minute, I'm sure you know how it is

 

Shouldn't the pointsPerCharacter be a Dictionary<char, int> ? You could remove that .ToString() call if it was.

Also i'm not sure you need to call .ToCharArray(), can't you just do:

foreach (char c in word)
{
  points += pointsPerCharacter[c];
}

I'm sure you could make your code even more succinct using Linq, but i'm not really super familiar with C#.

Link to comment
Share on other sites

36 minutes ago, Tom said:

Edit: I'm onto something literally 1 minute after posting this thread.
Calculate the points per word, then add it to a dictionary Word, Points, then sort that dictionary by the amount of points.

Just delete this shit I was too tired for my own good

 

Alternative method (again i'm not that familiar with C#) would be to create a custom Comparer and then use the built in List sort method https://msdn.microsoft.com/en-us/library/234b841s(v=vs.110).aspx

Link to comment
Share on other sites

12 minutes ago, Explv said:

 

Shouldn't the pointsPerCharacter be a Dictionary<char, int> ? You could remove that .ToString() call if it was.

Also i'm not sure you need to call .ToCharArray(), can't you just do:


foreach (char c in word)
{
  points += pointsPerCharacter[c];
}

I'm sure you could make your code even more succinct using Linq, but i'm not really super familiar with C#.

Yeah you're right in both aspects. I haven't visited C# since the start of last year so yeah its been fun

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...