Question

In: Computer Science

In this assignment you will be implementing a function that will find all single-word anagrams given...

In this assignment you will be implementing a function that will find all single-word anagrams given a single word and a list of all words. In the starter code you will the starting source file anagram.cpp and the list of words wordlist.txt. You will need to implement the anagram() function which takes a string for the word to find anagrams of as the first parameter and a vector of strings containing a list of all words as the second parameter. The return value of anagram() will be a vector of strings containing all anagrams (including the parameter word if it is an actual word). There is a main function that will test the anagram() function as well as an already-implemented function loadWordlist() which will read in words from the specified file with the format by string-type parameter and return these words in a vector of strings. The anagram() function will take a word (string) and a wordlist of all words (vector of strings) and builds a dictionary/map where the key is a specific amount of times each letter occurs in a word and the associated value is a vector of strings containing all words using those letters (anagrams). You may either modify the Stringset class to be a dictionary (holding a key:value pair as opposed to just a key) or use the STL unordered_map structure. The main() function provided will test the anagram() function with a word the user types in using the words stored from wordlist.txt in wordlist, for example if the user types in steak, anagrams=anagram("steak",wordlist); will run and if implemented correctly the following contents of anagrams will be displayed:

skate stake steak takes teaks

Submit anagram.cpp with the implemented anagram() function. You are free to implement any helper functions in anagram.cpp.

Anagram.cpp:
#include
#include
#include
#include

using namespace std;

vector loadWordlist(string filename);
vector anagram(string word, vector wordlist);

int main()
{
vector words;
vector anagrams;
string inputWord;
words=loadWordlist("wordlist.txt");
cout << "Find single-word anagrams for the following word: ";
cin >> inputWord;
anagrams = anagram(inputWord, words);
for (int i=0; i {
cout << anagrams[i] << endl;
}
return 0;
}

vector loadWordlist(string filename)
{
vector words;
ifstream inFile;
string word;
inFile.open(filename);
if(inFile.is_open())
{
while(getline(inFile,word))
{
if(word.length() > 0)
{
words.push_back(word);
}
}
inFile.close();
}
return words;
}
vector anagram(string word, vector wordlist)
{
}

Stringset Class:

class Stringset
{
private:
vector<list<string>> table;
int num_elems;
int size;
public:
Stringset();
vector<list<string>> getTable() const;
int getNumElems() const;
int getSize() const;
void insert(string word);
bool find(string word) const;
void remove(string word);
};

Solutions

Expert Solution

Hello Buddy!

Here is the solution code.

Although, wordlist.txt file was not provided, so I made a simple one for testing the code. Also there are many mistakes and blanks in the provided starting template code. I fixed them all too.

Everything is working fine. Hope it helps!

code is commented in the required part.  if you face any issue, feel free to ask.

Anagram.cpp

#include <iostream>
#include <string>
#include <bits/stdc++.h> 
#include <unordered_map> 

using namespace std;

vector<string> loadWordlist(string filename);
vector<string> anagram(string word, vector<string> wordlist);

int main()
{
    vector<string> words;
    vector<string> anagrams;
    string inputWord;
    words=loadWordlist("wordlist.txt");
    cout << "Find single-word anagrams for the following word: ";
    cin >> inputWord;
    anagrams = anagram(inputWord,words);
    for (int i=0; i<anagrams.size();i++ ){
        cout << anagrams[i] << endl;
    }
    return 0;
}

vector<string> loadWordlist(string filename)
{
    vector<string> words;
    ifstream inFile;
    string word;
    inFile.open(filename);
    if(inFile.is_open()){
        while(getline(inFile,word)){
            if(word.length() > 0){
                words.push_back(word);
            }
        }
        inFile.close();
    }
    return words;
}


vector<string> anagram(string word, vector<string> wordlist)
{   //a variable to store one item from wordlist
    string item;
    
    // a vector to store all the found anagrams
    vector<string> anagrams_vector;
    
    //pushing the original word in anagram_vector
    anagrams_vector.push_back(word);
    
    //looping through all the items in wordlist
    for(int k=0;k<wordlist.size();k++){
        //take out one item from wordlist
        item = wordlist[k];
        
        int l1 = word.length();
        int l2 = item.length();
        
        //initializing unordered map , so that we can match the number of characters in both strings
        unordered_map<char,int> m;
        if(l1!=l2){
            //if different lenght, they can't be anagram
            continue;
        }
        
        for(int i=0;i<l1;i++){
            //increasing key-values for those characters, which are in word(user input)
            m[word[i]]++;
        }
        
        for(int i=0;i<l2;i++){
            if(m.find(item[i])==m.end()){
                //if any character, that was in word, not found in the item, then they are not anagram
                break;
            }
            else{
                //decreasing the corresponding values of no. of characters, which are found in item 
                m[item[i]]--;
                if(m[item[i]]==0){
                    //if all characters will be zero, then whole map will become empty.
                    m.erase(item[i]);
                }
            }
        }
        //if map is empty, it means 'word' and 'item' are anagram.
        if(m.size()==0){
            anagrams_vector.push_back(item);
        }    
    } 
  return anagrams_vector;
}

.

wordlist.cpp

nikhil
khilin
niklhi
kkkkkkj
nikhik
jhirkk
inkhil
khkhkh
ikihln
nhikil
kjhkgsdkjfhkj

Std Input : "nikhil"

Output :

Thumbsup ;)


Related Solutions

PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter...
PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter a word or empty string to finish: poster The word poster has the following 6 anagrams: presto repost respot stoper topers tropes
For this project, you are going to write a program to find the anagrams in a...
For this project, you are going to write a program to find the anagrams in a given dictionary for a given word. If two words have the same letters but in different order, they are called anagrams. For example, “listen” and “silent” are a pair of anagrams. **JAVA** First let’s focus on “LetterInventory.java”. Its purpose is to compute the canonical “sorted letter form” for a given word. 1. Implement the constructor, which takes the String input word. You can assume...
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given...
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given limit. Aim Develop code for implementing the sieve of Eratosthenes. Steps for Completion Implement the isPrime() method of the SieveOfEratosthenes class that should return true if the number is prime, and false otherwise. Consider building the sieve in the class constructor. CODE GIVEN public class SieveOfEratosthenes { public SieveOfEratosthenes(int maxValue) { // Build the sieve here } public boolean isPrime(int value) { // Write...
The rule of the derivative of a function f is given. Find the location of all...
The rule of the derivative of a function f is given. Find the location of all local extrema. f'(x) = (x2- 1)(x - 2) Group of answer choices Local maxima at -1 and 2; local minimum at 1 Local maximum at 1; local minima at -1 and 2 Local maximum at -1; local minima at -2 and 1 Local maximum at 2 - ; local minimum at 2 +
Submitting your assignment   1. Please collate your assignment as a single document in a Microsoft Word...
Submitting your assignment   1. Please collate your assignment as a single document in a Microsoft Word format. 2. Save the assignment as your candidate ID number, e.g. CON-000123456. 3. All marking is anonymous, which means your name should not appear on your work. 4. You must state which region/jurisdiction you are from in the header of each page. 5. You must ensure that all your work is properly referenced. 6. Penalties will be applied to those candidates who submit their...
All questions in this assignment refer to the “om” database (or Schema) that you will find...
All questions in this assignment refer to the “om” database (or Schema) that you will find in your MySQL Workbench program if you have run the sample database install script. Please save all of your answers in one script (.sql) or type all your answers into Notepad++ and submit them as a single .sql file. You are encouraged to test your SQL statements in Workbench, and please use the ‘tidy’ tool to properly format your SQL before you save it...
Find all local extreme values of the given function and identify each as a local maximum,...
Find all local extreme values of the given function and identify each as a local maximum, local minimum, or saddle point of the next functions. 1) f(x, y) = e-(x2 + y2 - 16x) 2) f(x, y) = x2 + 100 - 20x cos y; -π < y < π
You are given a task to parse through financial information and find the word “money” followed...
You are given a task to parse through financial information and find the word “money” followed by “bad” 2 or more times or “money” followed by “good” once. Create a regex that can search achieve this. You can ignore spaces. Legal words in the language could be “moneybadbadbad” or “moneygood”
This assignment must be completed on a Word Document and submitted. All questions related to the...
This assignment must be completed on a Word Document and submitted. All questions related to the case study must be answered completely in order to receive full credit. This case study assesses SLO #2: "On completion of this course, student will describe steps taken to prevent and manage medical emergencies in the dental office." You have just seated your patient to have a tooth prepared for a crown. It has already been a hectic morning and the dentist has urged...
use the Rational Zeros Theorem to find all the real zeros of each polynomial function given...
use the Rational Zeros Theorem to find all the real zeros of each polynomial function given below: f(X)=3X^3 + 6X^2 -15X - 30 f(X) = 2X^4 - X^3 - 5X^2 + 2X + 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT