Interesting Puzzle

With the following structures and locals:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Category
{
  Category *parent;
  Category *right;   // left and right categories share the same parent
  Category *left;
  std::vector< Category* > children;
  std::vector< Product* > products;
};

struct Product
{
  std::vector< Category* > categories;
  bool can_sell;
};

std::vector< Category* > all_categories;
std::vector< Product*  > all_products;


Given a Category, create the paths to its children that have at least one product that can be sold.

Bonus points: This is actually a representation of a SQL database. Do it in one SQL query. I can make the set of descendant categories with sellable products, but I'm stuck trying to make the path from within SQL.
Last edited on
To my understanding, SQL isn't Turing-complete, which would be required to do this in a single query. If you could do this, you still shouldn't. There would be no way of knowing if the query will end or not.
Yeah, I had to stop spending time on it and move on to other things.

My solution was to collect the category's leaves that had at least one product into an array ( SQL ). For each leaf, set the leaf to the leaf's parent while the parent wasn't the category ( Ruby ).
Last edited on
Topic archived. No new replies allowed.