Tom Posted September 27, 2017 Share Posted September 27, 2017 (edited) 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 September 27, 2017 by Tom 1 Quote Link to comment Share on other sites More sharing options...
HunterRS Posted September 27, 2017 Share Posted September 27, 2017 Your edit is pretty much heads on, python would be a lot easier coding wise if you can use it :p Quote Link to comment Share on other sites More sharing options...
Tom Posted September 27, 2017 Author Share Posted September 27, 2017 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 Yep this always fuckin happens, think about it for hours, make a post and figure it out moments later. Quote Link to comment Share on other sites More sharing options...
Alek Posted September 27, 2017 Share Posted September 27, 2017 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). Quote Link to comment Share on other sites More sharing options...
HunterRS Posted September 27, 2017 Share Posted September 27, 2017 (edited) 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 September 27, 2017 by HunterRS Quote Link to comment Share on other sites More sharing options...
Tom Posted September 27, 2017 Author Share Posted September 27, 2017 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 Quote Link to comment Share on other sites More sharing options...
HunterRS Posted September 27, 2017 Share Posted September 27, 2017 (edited) 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 September 27, 2017 by HunterRS 1 Quote Link to comment Share on other sites More sharing options...
Explv Posted September 27, 2017 Share Posted September 27, 2017 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#. Quote Link to comment Share on other sites More sharing options...
Explv Posted September 27, 2017 Share Posted September 27, 2017 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 Quote Link to comment Share on other sites More sharing options...
Tom Posted September 27, 2017 Author Share Posted September 27, 2017 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 Quote Link to comment Share on other sites More sharing options...
Team Cape Posted September 27, 2017 Share Posted September 27, 2017 Would it be more efficient to add the words into the list in an ordered fashion as you're calculating the point value of each word? Quote Link to comment Share on other sites More sharing options...