Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

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

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

  • Author
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.

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).

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

  • Author
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

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

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#.

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

  • Author
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

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.