/* Given a set of integers, returns a uniformly-random subset of that
 * set.
 */
Set<int> randomSubsetOf(Set<int>& s){
	Set<int> sub;
	if(s.size() == 0) return sub;
	else
		if(randomChance(.5)){
			sub.add(s.first());
			s.remove(s.first());
			return sub + randomSubsetOf(s);}
		else{
			s.remove(s.first());
			return sub + randomSubsetOf(s);}
}
Example #2
0
Set<int> randomSubsetOf(Set<int>& s){
  bool tag=bool(randomInteger(0,1));
  if(s.isEmpty()){
    Set<int> emptySet;
    return emptySet;
  }
  if(tag){
    int element=s.first();
    s.remove(element);
    Set<int>    result=randomSubsetOf(s);
    result.add(element);
    return result;
  }
  if(!tag){
    int element=s.first();
    s.remove(element);
    return randomSubsetOf(s);
  }
}
int countSubsetSumWays(Set<int> &set, int target) {
	if (set.isEmpty() && target != 0) {
		return 0;
	} else if (set.isEmpty() && target == 0) {
		return 1;
	} else {
		int element = set.first();
		Set<int> rest = set - element;
	 	return countSubsetSumWays(rest, target) + countSubsetSumWays(rest, target - element);
	}
}
Example #4
0
//this function is called by searchKeywords to output the first match to the keyword
// searched by the user and then request the user's next command
void keywordQueryMenu(Set<Movie*> &matches, int numMatches) {
  matches.first();
  bool userWantsToExit = false;
  while (!userWantsToExit) {
    cout << "\n" << matches.getCurrent()->getTitle() << endl;
    Set<string> allKeywords = matches.getCurrent()->getAllKeywords();
    allKeywords.first();
    while (true) {
      cout << allKeywords.getCurrent() << endl;
      try {
	allKeywords.next();
      } catch (NoSuchElementException &e) {
	break;
      }
    }

    bool thereIsNext = true;
    try {
      matches.next();
    } catch (NoSuchElementException &e) {
      thereIsNext = false;
    }

    bool userWantsNextFilm = false;
    while (!userWantsToExit && !userWantsNextFilm) {
      if (thereIsNext) {
	cout << "\n" << endl;
	cout << "Choose your next option: " << endl;
	cout << setw(4) << "1" << ". " << matches.getCurrent()->getTitle() << endl;
	cout << setw(4) << "2" << ". Return to menu" << endl;
	cout << "\nInput your command: ";
	int command;
	cin >> command;
	if (cin.fail()) {
	  cin.clear();
	  cin.ignore(1000, '\n');
	  cout << "Sorry, your command was not valid" << endl;
	  continue;
	} 
	cin.clear();
	cin.ignore(1000, '\n');
	cout << endl;
	if (command == 2) {
	  userWantsToExit = true;
	  cout << "Thanks for using the keyword search query!" << endl;
	} else if (command == 1) {
	  userWantsNextFilm = true;
	} else {
	  cout << "Sorry, that was not a valid command number" << endl;
	  continue;
	}
	  
      } else {
        cout << "\n" << endl;
        cout << "Choose your next option: " << endl;
        cout << setw(4) << "1" << ". Return to menu" << endl;
        cout << "\nInput your command: ";
        int command;
        cin >> command;
        if (cin.fail()) {
          cin.clear();
          cin.ignore(1000, '\n');
          cout << "Sorry, your command was not valid" << endl;
          continue;
        }
        cin.clear();
        cin.ignore(1000, '\n');
        cout <<endl;
	if (command == 1) {
	  userWantsToExit = true;
	  cout << "\nThanks for using the keyword search query!" << endl;
	} else {
	  cout << "Sorry, that was not a valid command. Please try again" << endl;
	  continue;
	}
      }

      if (userWantsToExit) {
	break;
      }
    }
  }