0% found this document useful (0 votes)
6 views6 pages

Dsa C++

Uploaded by

arhamaaltaf98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Dsa C++

Uploaded by

arhamaaltaf98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

PART 1 :

#include <iostream>
#include <functional>
#include <string>
#include <cassert>
using namespace std;

class Term {
public:
string query;
long weight;

Term(string q, long w){


query = q; weight = w;
}
bool operator<(const Term& other) const{
return query < other.query;
}

static function<bool(const Term&, const Term&)> byReverseWeightOrder(){


return [](const Term& a, const Term& b){return a.weight > b.weight;};
}

static function<bool(const Term&, const Term&)> byPrefixOrder(int r){


return [r](const Term& a, const Term& b){ return a.query.substr(0,r) <
b.query.substr(0,r);};
}

string toString() const{


return query + " " + to_string(weight);
}

string getQuery()const{
return query;
}

// Unit testing (required)


static void test(){
Term t1("apple", 100);
Term t2("banana", 200);
assert(t1 < t2 && "Test 1 failed: 'apple' should be less than 'banana'");
assert(!(t2 < t1) && "Test 2 failed: 'banana' should not be less than
'apple'");

// Test reverse weight order comparator


auto reverseWeightComp = Term::byReverseWeightOrder();
assert(reverseWeightComp(t2, t1) && "Test 3 failed: t2 should have greater
weight than t1");
assert(!reverseWeightComp(t1, t2) && "Test 4 failed: t1 should not have
greater weight than t2");

// Test reverse weight order with equal weights


Term t3("cherry", 100);
assert(!reverseWeightComp(t1, t3) && "Test 5 failed: t1 and t3 have equal
weights, comparison should be false");

// Test prefix order comparator


auto prefixComp = Term::byPrefixOrder(3);
Term t4("apricot", 300);
assert(prefixComp(t1, t4) && "Test 6 failed: 'apple' should be less than
'apricot' for the first 3 characters");
assert(!prefixComp(t4, t1) && "Test 7 failed: 'apricot' should not be less
than 'apple' for the first 3 characters");

// Test prefix order when prefixes are the same


Term t5("appreciate", 150);
assert(!prefixComp(t1, t5) && "Test 8 failed: 'apple' should not be less
than 'appreciate' for the first 3 characters");

// Test prefix order when one string is shorter


Term t6("app", 250);
assert(!prefixComp(t1, t6) && "Test 9 failed: 'apple' should not be less
than 'app' for the first 3 characters");

cout << "All tests passed successfully!\n";


}

};

-----------------------------------------------------------------------------

PART 2:

class BinarySearchDeluxe {
public:
template <typename Key>
static int firstIndexOf(const vector<Key>& a, const Key& key,
function<bool(const Key&, const Key&)> comparator) {
if (a.empty() || !comparator) {
throw invalid_argument("Array or comparator cannot be null.");
}

int firstindex = 0;
int lastindex = a.size() - 1;
int mid;

while (firstindex <= lastindex) {


mid = firstindex + (lastindex - firstindex) / 2;
if (comparator(a[mid], key)) {
firstindex = mid + 1;
} else if (comparator(key, a[mid])) {
lastindex = mid - 1;
} else {
lastindex = mid - 1;
}
}
return mid;
}

template <typename Key>


static int lastIndexOf(const vector<Key>& a, const Key& key,
function<bool(const Key&, const Key&)> comparator) {
if (a.empty() || !comparator) {
throw invalid_argument("Array or comparator cannot be null.");
}

int firstindex = 0;
int lastindex = a.size() - 1;
int mid;

while (firstindex <= lastindex) {


mid = firstindex + (lastindex - firstindex) / 2;
if (comparator(a[mid], key)) {
firstindex = mid + 1;
} else if (comparator(key, a[mid])) {
lastindex = mid - 1;
} else {
firstindex = mid + 1;
}
}
return mid;
}

static void test() {


try {
vector<Term> terms1 = {Term("app",700), Term("apple",500),
Term("application",300)};
Term compare("app", 700);
auto firstindex1 = firstIndexOf(terms1, compare,
Term::byPrefixOrder(3));
auto lastindex1 = lastIndexOf(terms1, compare, Term::byPrefixOrder(3));
cout << "The array is: " << endl;
for (auto val : terms1){
cout << val.toString() << endl;
}
cout << "First index of app is " << firstindex1 << endl;
cout << "Last index of app is " << lastindex1 << endl;
cout << endl;

vector<Term> terms2 = {Term("apple", 500), Term("app", 700),


Term("app",
600),
Term("application", 300)};
auto firstindex2 = firstIndexOf(terms2, compare,
Term::byPrefixOrder(3));
auto lastindex2 = lastIndexOf(terms2, compare, Term::byPrefixOrder(3));
cout << "The array is: " << endl;
for (auto val : terms2){
cout << val.toString() << endl;
}
cout << "First index of app is " << firstindex2 << endl;
cout << "Last index of app is " << lastindex2 << endl;
cout << endl;

vector<Term> terms3 = {Term("apple", 500), Term("app", 700),


Term("app",

600), Term("application", 300)};


auto firstindex3 = firstIndexOf(terms3, compare,
Term::byReverseWeightOrder());
auto lastindex3 = lastIndexOf(terms3, compare,
Term::byReverseWeightOrder());
cout << "The array is: " << endl;
for (auto val : terms3){
cout << val.toString() << endl;
}
cout << "First index of 700 is " << firstindex3 << endl;
cout << "Last index of 700 is " << lastindex3 << endl;
cout << endl;

} catch (const invalid_argument& e) {


cerr << "Invalid argument: " << e.what() << endl;
}
}
};

--------------------------------------------------------------------------------
PART 3:

class Autocomplete {
private:
std::vector<Term> terms;
public:
Autocomplete(const vector<Term>& terms) : terms(terms){}

vector<Term> allMatches(const string& prefix){


if (terms.empty()) cout << "Terms is empty";
Term compare(prefix,0);
vector<Term> Matches;
int firstindex = BinarySearchDeluxe::firstIndexOf(terms, compare,
Term::byPrefixOrder(prefix.size()));
int lastindex = BinarySearchDeluxe::firstIndexOf(terms, compare,
Term::byPrefixOrder(prefix.size()));
for(int i = firstindex; i< terms.size(); i++){
if(terms[i].query.find(prefix) == 0){
Matches.push_back(terms[i]);
}
}
sort(Matches.begin(),Matches.end(),Term::byReverseWeightOrder());
return Matches;
}

int numberOfMatches(const string& prefix){


if (terms.empty()) cout << "Terms is empty";
Term compare(prefix,0);
int count = 0;
int firstindex = BinarySearchDeluxe::firstIndexOf(terms, compare,
Term::byPrefixOrder(prefix.size()));
int lastindex = BinarySearchDeluxe::firstIndexOf(terms, compare,
Term::byPrefixOrder(prefix.size()));
for(int i = firstindex; i< terms.size(); i++){
if(terms[i].query.find(prefix) == 0){
count++;
}
}
return count;
}
static void test(){
vector<Term> terms = {
Term("apple", 500),
Term("app", 700),
Term("application", 300),
Term("ape", 200),
Term("apex", 100)
};

Autocomplete autocomplete(terms);

cout << "All matches for 'app':\n";


auto results = autocomplete.allMatches("app");
for (const auto& term : results) {
cout << term.toString() << endl;
}

cout << "Number of matches for 'ap': " <<


autocomplete.numberOfMatches("ap") << endl;
}
};

int main() {

cout << "Reading from cities file"<< endl;


string filename = "C:\\Users\\arham\\Downloads\\cities.txt";
ifstream infile(filename);
if (!infile) {
cerr << "Could not open the file: " << filename << endl;
return 1;
}

int n;
infile >> n;
vector<Term> terms;

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


long weight;
string query;
infile >> weight >> std::ws;
getline(infile, query);
terms.emplace_back(query, weight);
}

Autocomplete autocomplete(terms);
string prefix;
cout<< "Enter prefix for Cities: "<< endl;
while (getline(cin, prefix)) {
vector<Term> results = autocomplete.allMatches(prefix);
cout << results.size() << " matches\n";
for (size_t i = 0; i < min(results.size(), static_cast<size_t>(5));
i++) {
cout << results[i].toString() << "\n";
}
}

return 0
}
-----------------------------------------------------------------------------------
-

You might also like