- Engineering
- Computer Science
- 1 introduction the instructions on how to complete this homework...
Question: 1 introduction the instructions on how to complete this homework...
Question details
1 Introduction The instructions on how to complete this homework, please check the document howtohw.pdf. The Twitter website has become a de facto first source for many important events in the last decade. Twitter’s hashtag feature lets users tag tweets with single words or phrases (e.g. #superbowl, #algorithms, or #vacaciones). Popular or trending hashtags indicate strong shared interest by many people in a topic, and tracking these trends is of interest to businesses, news outlets, and researchers. #ads 2 vector E #gig 2 #hot 1 #dog 0 #cat 4 #fad 6 #big 8 #egg 9 string hashtag int pop Figure 1: Representing hashtags and their popularities using a vector-based data structure. In this homework, you’ll implement an vector-based data structure that tracks information about a collection of hashtags, including which are most popular, i.e. are trending. 1 2 Instructions The following files have been given to you: 1. A C++ header file (trendtracker.h) declaring the Trendtracker class. 2. A C++ source file (main.cpp) containing a main function with tests. 3. A text file (common.txt) containing 3612 common English words.2 dl=0 Create a new C++ source file named trendtracker.cpp that implements the Trendtracker class, so that trendtracker.cpp and the provided files compile into a program that runs with failed or not failed tests. Submit the source file trendtracker.cpp
"MAIN.CPP" #include <cassert> #include <fstream> #include <iostream> #include <cstdlib> #include <string> #include "trendtracker.h" using namespace std; inline void _test(const char* expression, const char* file, int line) { cout<<"test(" << expression << ") failed" << endl; //cerr << "test(" << expression << ") failed in file " << file; //cerr << ", line " << line << "." << endl; //abort(); } //inline void _test(const char* expression) //{ //cout<<"test(" << expression << ") not equal or failed in file"<<endl; //} #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__)) //#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION)) //#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : cout<<) int main() { // Setup vector<string> R; string s, line; int numOfktrend; // Test size() and insert(). Trendtracker T1; test(T1.size() == 0); test(T1.popularity("#algorithms") == 0); test(T1.popularity("#cs4all") == 0); test(T1.popularity("#programming") == 0); T1.insert("#cs4all", 0); test(T1.size() == 1); test(T1.popularity("#cs4all") == 0); T1.insert("#algorithms", 2); test(T1.size() == 2); test(T1.popularity("#algorithms") == 2); T1.insert("#programming", 1); test(T1.size() == 3); test(T1.popularity("#programming") == 1); T1.insert("#algorithms", -1); test(T1.size() == 3); test(T1.popularity("#algorithms") == 2); T1.tweeted("#programming"); test(T1.popularity("#programming") == 2); T1.tweeted("#programming"); test(T1.popularity("#programming") ==3); T1.tweeted("#programming"); test(T1.popularity("#programming") == 4); T1.tweeted("#cs4all"); test(T1.popularity("#cs4all") == 1); T1.tweeted("#algorithms"); test(T1.popularity("#algorithms") == 3); T1.tweeted("#cs4all"); test(T1.popularity("#cs4all") == 2); T1.tweeted("#datastructures"); test(T1.popularity("#datastructures") == 0); T1.insert("#datastructures",8); test(T1.popularity("#datastructures") == 8); T1.tweeted("#datastructures"); test(T1.popularity("#datastructures") == 9); T1.insert("#C++",3); T1.tweeted("#C++"); test(T1.popularity("#C++") == 4); T1.insert("#3333",2); T1.tweeted("#3333"); test(T1.popularity("#3333") == 3); //return the numbers of hashtags of popularity equal to 3 and 2 respectively numOfktrend = T1.k_trend(3); cout << "The number of hashtags with popularity equal to 3 is " << numOfktrend << endl; numOfktrend = T1.k_trend(2); cout << "The number of hashtags with popularity equal to 2 is " << numOfktrend << endl; T1.top_three_trends(R); test(R.size() == 0); test(R[0] == "#datastructures"); //////////////////////another instance////////////////////////////////////////////// Trendtracker T2; T2.insert("#dobermann", 1); T2.insert("#shihtzu", 9); T2.insert("#pomeranian", 9); T2.insert("#pembroke", 6); T2.insert("#havanese", 3); T2.tweeted("#shihtzu"); T2.tweeted("#shihtzu"); T2.tweeted("#pomeranian"); T2.tweeted("#havanese"); T2.tweeted("#pembroke"); T2.tweeted("#pomeranian"); test(T2.popularity("#shihtzu") == 11); //test(T2.popularity("#shihtzu") == 10); test(T2.popularity("#pomeranian") == 11); //test(T2.popularity("#pomeranian") == 10); test(T2.popularity("#havanese") == 4); test(T2.popularity("#pembroke") == 7); test(T2.popularity("#dobermann") == 1); ///////////////////////////another instance/////////////////////////////////// Trendtracker T3; ifstream f; f.open("common.txt"); assert(f.is_open()); // If this fails, you're missing common.txt while (getline(f, line)) { string str_num = line.substr(line.find(',')+1, line.size()-1); T3.insert(line.substr(0, line.find(',')), std::stoi(str_num,nullptr,10)); } f.close(); test(T3.size() == 3597); //for (int i = 0; i < 556; ++i) { test(T3.popularity("#finishing") == 1463179852); test(T3.popularity("#completely") == 571540977); test(T3.popularity("#us") == 1466256269); test(T3.popularity("#sometimes") == 685583454); test(T3.popularity("#quieting") == 1791728192); //test(T3.top_trend() == "#finishing"); T3.top_three_trends(R); test(R[0] == "#finishing"); test(R[1] == "#completely"); test(R[2] == "#is"); } cout << "Assignment complete." << endl; }
"trendtracker.h" **************************************** #ifndef TRENDTRACKER_H #define TRENDTRACKER_H #include <vector> #include <string> using namespace std; class Trendtracker { // For the mandatory running times below: // n is the number of hashtags in the Trendtracker. public: // Creates a new Trendtracker tracking no hashtags. // // Must run in O(1) time. Trendtracker(); // Inserts a hashtag of the popularity pop into the Trendtracker. // If pop is a non-negative integer and the hashtag already is in Trendtracker, //add pop to the popularity of this hashtag in Trendtracker; // if pop is a non-negative integer but the hashtag does not exist, add // otherwise, pop is a negative value and then do nothing but insert a hashtag of popularity zero if it is not in Trendtracker. // // Must run in O(n) time. void insert(string ht, int pop); // Return the number of hashtags in the Trendtracker. // // Must run in O(1) time. int size(); // Adds 1 to the total number times a hashtag has been tweeted. // If the hashtag does not exist in TrendTracker, does nothing. // // Must run in O(n) time. void tweeted(string ht); // Returns the number of times a hashtag has been tweeted. // If the hashtag does not exist in Trendtracker, returns -1. // // Must run in O(n) time. int popularity(string name); // Returns the nubmer of hashtags of popularity equal to k. // k must be a non-negative integer and if not, return -1. // If the Trendtracker has no hashtags with popularity k, returns 0. // // Must run in O(n) time. int k_trend(int k); // Fills the provided vector with the 3 most-tweeted hashtags, // in order from most-tweeted to least-tweeted. // // If there are fewer than 3 hashtags, then the vector is filled // with all hashtags (in most-tweeted to least-tweeted order). // // Must run in O(n) time. void top_three_trends(vector<string> &T); private: // A struct representing a hashtag and // the number of times it has been tweeted. struct Entry { public: string hashtag; int pop; }; // Entries containing each hashtag and its popularity. vector<Entry> E; }; #endif
Solution by an expert tutor
