You work on a team whose job is to understand the most sought after toys for the holiday season. A teammate of yours has built a webcrawler that extracts a list of quotes about toys from different articles. You need to take these quotes and identify which toys are mentioned most frequently. Write an algorithm that identifies the top N toys out of a list of quotes and list of toys.

Respuesta :

Answer:

#include <iostream>

#include <string>

#include <vector>

#include <regex>

#include <cstring>

using namespace std;

class Toy {

public:

   string name{};

   int count{0};

   // Constructor

   Toy(string n) {

       name = n;

   }

};

size_t findCaseInsensitive(string data, string toSearch, size_t pos = 0)

{

   // Convert complete given String to lower case

   transform(data.begin(), data.end(), data.begin(), ::tolower);

   // Convert complete given Sub String to lower case

   transform(toSearch.begin(), toSearch.end(), toSearch.begin(), ::tolower);

   // Find sub string in given string

   return data.find(toSearch, pos);

}

void swapToys(Toy* toy1, Toy* toy2 ) {

   Toy temp = *toy1;

   *toy1 = *toy2;

   *toy2 = temp;

}

vector<Toy*> sortToyVector(vector<Toy*> t) {

   for (int i = 0; i < t.size(); i++) {

       for (int j = 0; j < t.size()- i - 1; j++) {

           if (t[j]->count < t[j + 1]->count) {

               swapToys(t[j], t[j + 1]);

           }

           else if (t[j]->count == t[j + 1]->count) {

               const char* lstr = { t[j]->name.c_str() };

               const char* rstr = { t[j+1]->name.c_str() };

               if (strcmp(lstr, rstr) > 0) {

                   swapToys(t[j], t[j + 1]);

               }

           }

           else;

       }

   }

   return t;

}

void displayTopToys(

   int numToys,

   int topToys,

   vector<string> toys,

   int numQuotes,

   vector<string> quotes

)

{  

   vector<Toy*> tvec{};

   for (int i = 0; i < toys.size(); i++) {

       Toy* t = new Toy(toys[i]);

       for (int j = 0; j < quotes.size(); j++) {

           size_t pos{ 0 };

           pos = findCaseInsensitive(quotes[j], toys[i]);

           while (pos != std::string::npos) {

               t->count = t->count + 1;

               pos = findCaseInsensitive(quotes[j], toys[i], pos + toys[i].length());

           }

       }

       cout << t->name << " :" << t->count << endl;

       tvec.push_back(t);

   }

   tvec = sortToyVector(tvec);

   cout << endl;

   cout << endl;

   cout << "Top Toys:" << endl;

   for (int i = 0; i < tvec.size(); i++) {

       cout << i << ": " << tvec[i]->name << " :" << tvec[i]->count << endl;

   }

   cout << endl;

   cout << endl;

   cout << "Number of Top Toys: " << topToys << endl;

   cout << "Top Toys:" << endl;

   for (int i = 0; i < topToys; i++) {

       cout << i << ": " << tvec[i]->name << endl;

   }

   cout << endl;

   cout << endl;

}

int main() {

   // Inputs

   int numToys = 6;

   int topToys = 2;

   vector<string> toys = { "elmo", "elsa", "legos", "drone", "tablet", "warcraft" };

   int numQuotes = 6;

   vector<string> quotes = {

   "Emo is the hottest of the season! Elmo will be on every kid's wishlist!",

   "The new Elmo dolls are super high quality",

   "Expect the Elsa dolls to be very popular this year",

   "Elsa and Elmo are the toys I'll be buying for my kids",

   "For parents of older kids, look into buying them a drone",

   "Warcraft is slowly rising in popularity ahead of the holiday season"

   };

   displayTopToys(numToys, topToys, toys, numQuotes, quotes);

   return 0;

}  

Input:

The input to the function/method consists of five arguments:

  numToys, an integer representing the number of toys

   topToys, an integer representing the number of top toys your algorithm needs to return;

   toys, a list of strings representing the toys,

   numQuotes, an integer representing the number of quotes about toys;

   quotes, a list of strings that consists of space-sperated words representing articles about toys

Output:

Return a list of strings of the most popular N toys in order of most to least frequently mentioned

Note:

The comparison of strings is case-insensitive. If the value of topToys is more than the number of toys, return the names of only the toys mentioned in the quotes. If toys are mentioned an equal number of times in quotes, sort alphabetically.

Explanation:

C++ was use to code the above algorithm.

RELAXING NOICE
Relax