2021/winter interview training for facebook/google
This commit is contained in:
74
puzzles/training/all_possible_subsets.cpp
Normal file
74
puzzles/training/all_possible_subsets.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
VIM: let b:cf5build="clang -std=c++20 -O2 -pthread -lstdc++ -I. {SRC} -o {OUT}"
|
||||
VIM: let b:cf5run="{OUT}"
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
/*
|
||||
Problem Description
|
||||
For given set of N elements print all possible subsets.
|
||||
|
||||
Sample inputs - Expected outputs
|
||||
{"a"} ->
|
||||
<- empty set
|
||||
a
|
||||
{"a", "b"} ->
|
||||
<- empty set
|
||||
a
|
||||
b
|
||||
a b
|
||||
Input corner cases
|
||||
Empty input set yields to one empty subset.
|
||||
*/
|
||||
|
||||
void printAllSubsets(const std::vector<std::string> &inputSet, std::ostream &os)
|
||||
{
|
||||
std::vector<bool> subset(inputSet.size(), false);
|
||||
|
||||
while (){
|
||||
|
||||
std::cout << '[';
|
||||
for (size_t i = 0; i < inputSet.size(); ++i){
|
||||
if (subset[i]){
|
||||
std::cout << inputSet[i] << ' ';
|
||||
}
|
||||
}
|
||||
std::cout << ']' << std::endl;
|
||||
|
||||
|
||||
std::
|
||||
|
||||
}
|
||||
std::vector<std::string>
|
||||
|
||||
}
|
||||
|
||||
int main ( void )
|
||||
{try{
|
||||
auto begin = std::chrono::high_resolution_clock::now();
|
||||
|
||||
|
||||
//......
|
||||
const std::vector<std::string> inputSet = {"a", "b", "c", "d"};
|
||||
printAllSubsets(inputSet, std::cout);
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> seconds = end - begin;
|
||||
std::cout << "Time: " << seconds.count() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
catch ( const std::exception& e )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
||||
return 2;
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "unknown exception." << std::endl;
|
||||
return 1;
|
||||
}}
|
||||
Reference in New Issue
Block a user