diff options
Diffstat (limited to 'labb2/wordchain/src/wordchain.cpp')
| -rw-r--r-- | labb2/wordchain/src/wordchain.cpp | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/labb2/wordchain/src/wordchain.cpp b/labb2/wordchain/src/wordchain.cpp index 5eb44c6..93f66af 100644 --- a/labb2/wordchain/src/wordchain.cpp +++ b/labb2/wordchain/src/wordchain.cpp @@ -10,7 +10,7 @@ using namespace std; const string ALPHABET = "abcdefghijklmnopqrstuvwxyz";
-void openDictionary(set<string> &words, string path) {
+void openDictionary(set<string> &words, const string &path) {
ifstream input;
input.open(path);
string word;
@@ -21,13 +21,14 @@ void openDictionary(set<string> &words, string path) { }
/*
- * Populates `neighbours` with all valid word neighbours from `words`.
+ * Populate `neighbours` with all valid word neighbours
+ * (words with only one char differing) from `words`.
*/
-void getValidNeighbours(forward_list<string> &neighbours, string &word, set<string> &words) {
+void getValidNeighbours(forward_list<string> &neighbours, const string &word, const set<string> &words) {
for (int i = 0; i < word.length(); i++) {
string new_word = word;
char cur_c = word[i];
- for (char new_c: ALPHABET) {
+ for (char new_c : ALPHABET) {
if (new_c != cur_c) {
new_word[i] = new_c;
if (words.count(new_word)) {
@@ -38,7 +39,11 @@ void getValidNeighbours(forward_list<string> &neighbours, string &word, set<stri }
}
-void wordchain(stack<string> &chain, string w1, string w2, set<string> &words) {
+/*
+ * Find the shortest word chain between `w1` and `w2`
+ * and store it in `chain`.
+ */
+void wordchain(stack<string> &chain, const string &w1, const string &w2, const set<string> &words) {
stack<string> firstChain;
firstChain.push(w1);
@@ -51,7 +56,7 @@ void wordchain(stack<string> &chain, string w1, string w2, set<string> &words) { chains.pop();
forward_list<string> validNeighbours;
getValidNeighbours(validNeighbours, curChain.top(), words);
- for (string &w: validNeighbours) {
+ for (string &w : validNeighbours) {
if (w == w2) {
// done
chain.push(w);
|
