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

In this assignment, you will be implementing a slot machine that you find in casinos. A...
In this assignment, you will be implementing a slot machine that you find in casinos. A slot machine has certain number of reels which spin to produce one of a fixed set of symbols (e.g. flowers, bells) randomly when the user pulls a lever. The user needs to insert certain number of currency units as wager into the slot machine before pulling the lever. We refer to these currency units as “wagerUnitValue”. For example in slot machines with quarter (25...
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...
you will be implementing two functions for the LinkedList class. The function that you are implementing...
you will be implementing two functions for the LinkedList class. The function that you are implementing is called reverse and isPalindrome. The isPalindrome function should determine if the linked list is a palindrome or not and the reverse function should reverse the linked list. When you are finished with the implementation, try to use valgrind to see if there are any other errors or memory leaks to deal with. valgrind ./lab7 or make val Expected Output This is an example...
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 +
Develop Python implementation of combinatorial algorithm which will find all possible Anagrams of user input. Hint:...
Develop Python implementation of combinatorial algorithm which will find all possible Anagrams of user input. Hint: use backtracking algorithm. Example: Input = ROWAN UNIVERSITY Output (298 possibilities): YOUR WINTERS VAIN OAT RUINS WIN VERY … ROAR TUNES WIN IVY IRONY UNWISER VAT TIE IN SUN ROW VARY…
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 < π
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT