I'm trying to find out how to list the word with the highest score, based on this function to calculate a score, the words come from an array of words, or so. How can I tackle this?
Asked
Active
Viewed 1,364 times
2 Answers
4
Since you just need the word with highest score, there is no need to keep track of scores for all candidate words. Keeping track of the best one is enough.
string best_word;
int best_score = 0;
for (auto word &: all_the_words) {
int cur_score = ScrabbleScore(word);
if (cur_score > best_score) {
best_word = word;
best_score = cur_score;
}
}
// Now you have best_word and best_score.
Edit: Extend to take care of all words with the same best score.
vector<string> best_words;
int best_score = 0;
for (auto word &: all_the_words) {
int cur_score = ScrabbleScore(word);
if (cur_score > best_score) {
best_words.clear();
best_words.push_back(word);
best_score = cur_score;
} else if (cur_score == best_score) {
best_words.push_back(word);
}
}
// Now you have best_words and best_score.

timrau
- 22,578
- 4
- 51
- 64
1
You can put your word strings in a std::vector<std::string>
, and call std::sort()
algorithm on that vector, specifying a custom comparison function to sort the words by their "score".
See the following sample code for details:
#include <algorithm> // for std::sort
#include <exception> // for std::exception
#include <iostream> // for std::cout
#include <stdexcept> // for std::runtime_error
#include <string> // for std::string
#include <vector> // for std::vector
using namespace std;
// NOTE #1: Since this function is *observing* the "word" parameter,
// pass it by const reference (const string & word).
int ScrabbleScore(const string & word) {
int score = 0;
static const char scoreTable[26] = {
1, 3, 3, 2, 1, 4, 2, 4, 1, 8,
5, 1, 3, 1, 1, 3, 10, 1, 1, 1,
1, 4, 4, 8, 4, 10
};
for (auto letter : word) {
// if alphabet word
if (letter >= 'a' && letter <= 'z') {
score += scoreTable[letter - 'a'];
} else {
// NOTE #2: Throw an exception when an invalid
// letter is found.
throw runtime_error("Invalid letter in word.");
}
}
return score;
}
int main() {
// Some test words
vector<string> words = {
"hi", "hello", "world", "ciao",
"integer", "sum", "sort", "words"
};
// Sort vector by ScrabbleScore (descending order)
sort(words.begin(), words.end(),
[](const string& lhs, const string& rhs) {
return ScrabbleScore(lhs) > ScrabbleScore(rhs);
}
);
// Print result
cout << "<word> (<score>)" << endl;
cout << "------------------" << endl;
for (const auto & w : words) {
cout << w << " (" << ScrabbleScore(w) << ")" << endl;
}
}
Output:
<word> (<score>) ------------------ world (9) words (9) hello (8) integer (8) ciao (6) hi (5) sum (5) sort (4)

Mr.C64
- 41,637
- 14
- 86
- 162
-
-
Hey thanks for this the code, I'm trying to understand how it works the only part of the code which I don't understand is `for (const auto & w : words)` where can I read up on this? – Hayde Sep 24 '13 at 18:44
-
-
@Hayde: the above `for` is a C++11 _"for each"_ . It means: `for (const string & w : words)` (i.e. _"for each string in words vector"_). I used `const &` instead of just `string` since the for loop is _observing_ the items, so I use `const` reference; you can find more details [here](http://stackoverflow.com/q/15927033/1629821). – Mr.C64 Sep 24 '13 at 21:05
-
@Dukeling: I agree; if just the maximum is required, there are better ways. I thought more of a general vector "custom sorting" solution, listing also the scores of the other ("non-maximum score") words. – Mr.C64 Sep 24 '13 at 21:08