summaryrefslogtreecommitdiffstats
path: root/labb2/wordchain/src/wordchain.cpp
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-09-15 13:47:27 +0200
committerGustav Sörnäs <gustav@sornas.net>2020-09-15 13:47:27 +0200
commitaa9d7f6543982618e0b4c72cfbfd3ebfeda293c1 (patch)
tree5555cd3fb03b9f795a1f6eb71d0d2d11b0171cf2 /labb2/wordchain/src/wordchain.cpp
parent4f2bda6a9c188f0c7d028b02b93ab3978fb7543c (diff)
downloadtddd86-aa9d7f6543982618e0b4c72cfbfd3ebfeda293c1.tar.gz
TDDDD86 labb 2 redovisninglab2
Diffstat (limited to 'labb2/wordchain/src/wordchain.cpp')
-rw-r--r--labb2/wordchain/src/wordchain.cpp17
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);