1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
masha68 [24]
3 years ago
11

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.
Computers and Technology
1 answer:
timofeeve [1]3 years ago
5 0

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.

You might be interested in
Match the items with their respective descriptions.
kap26 [50]

a is 3

b is 1

c is 2

d is 4

7 0
4 years ago
Read 2 more answers
Convert the following decimal number into octal number-147​
agasfer [191]

Answer:

The answer for your question is 223

3 0
3 years ago
Civil engineering structures are also called _________.
spayn [35]

Answer:

Hey mate here is your answer

Civil engineering structures

Civil structural engineering includes all structural engineering related to the built environment. It includes: Bridges. Dams.

So, Civil Structural engineering

please mark me as a brainliest

4 0
2 years ago
Which of the following choices is evidence that present-day vertebrates shared common ancestors?
kolbaska11 [484]

Answer:

B/Second

Explanation:

They display similar embryo development and share common genes that control development.

6 0
2 years ago
Write a program that reads the lengths of the sides of a triangle from the user. Compute the area of the triangle using Heron's
slamgirl [31]

Answer:

The java program is as follows.

import java.util.Scanner;

import java.lang.*;

public class Area

{

   //variables to hold the values

   static double a, b, c;

   static double s;

   static double area;

public static void main(String[] args) {

    //scanner class object created

    Scanner sc = new Scanner(System.in);

 System.out.print("Enter the first side: ");

 a=sc.nextInt();

 System.out.print("Enter the second side: ");

 b=sc.nextInt();

 System.out.print("Enter the third side: ");

 c=sc.nextInt();

 s=(a+b+c)/2;

 //function of Math class used

 area = Math.sqrt( s*(s-a)*(s-b)*(s-c) );

 //result displayed with 3 decimal places

 System.out.printf("The area of the triangle is %.3f", area);  

}

}

OUTPUT

Enter the first side: 1

Enter the second side: 1

Enter the third side: 1

The area of the triangle is 0.433

Explanation:

1. The variables to declare the three sides of the triangle, the semi-perimeter and the area of the triangle are declared with double datatype. All the variables are declared at class level and hence, declared with keyword, static.  

2. Inside main(), an object of the Scanner class is created.

Scanner sc = new Scanner(System.in);

3. Using the Scanner class object, user input is taken for all the three sides of the triangle.

4. Following this, the semi-perimeter is computed as shown.

s=(a+b+c)/2;

5. The area of the triangle is computed using the given formula which is implemented as shown.

area = Math.sqrt( s*(s-a)*(s-b)*(s-c) );

6. The sqrt() method of the Math class is used while computing the area of the triangle.

7. The area is displayed with three decimals. This is done using the printf() method as shown.

System.out.printf("The area of the triangle is %.3f", area);

8. The program is saved with the same name as the name of the class having the main() method.

9. The class having the main() method is always public.

3 0
3 years ago
Other questions:
  • Pls help me. ask yourself what would jesus do...
    14·1 answer
  • Write a program that read first a user's given name followed by the user's age from standard input. Then use an ofstream object
    8·1 answer
  • What are sums of money that are given out for specific reasons?
    5·2 answers
  • Refer to the exhibit, a technician applies the configuration in the exhibit to an unconfigured router. To verify the configurati
    13·1 answer
  • Nina is 12 years old. She has just changed her password and wants to make sure she has it in case of
    15·2 answers
  • 1. Answer the following questions: a. What are the different types of number system? Name them.​
    8·1 answer
  • To see the formula in a cell, look at the___
    5·1 answer
  • Braxton is writing a program to design t-shirts. Which of the following correctly sets an attribute for color?
    7·1 answer
  • How do we find the time complexity for this algorithm?
    6·1 answer
  • Which of the following is an example of machine learning?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!