Display Table of Food Orders in a Restaurant in C++

C++Server Side ProgrammingProgramming

Suppose we have an array orders, which represents the orders that customers have done in a restaurant. So, orders[i]=[cust_namei, table_numi, food_itemi] where cust_namei is the customer name, table_numi is the customers table number, and food_itemi is the item customer orders.

We have to return the restaurant's “display table”. Here the “display table” is a table whose row entries denote how many of each food item each table ordered. The first column will be the table number and the remaining columns correspond to each food item in alphabetical order. First row should be a header whose first column is “Table”, followed by the names of the foods.

So, if the input is like orders = [["Amal","3","Paratha"],["Bimal","10","Biryni"],["Amal","3","Fried Chicken"],["Raktim","5","Water"],["Raktim","5","Paratha"],["Deepak","3","Paratha"]], then the output will be [["Table","Biryni","Fried Chicken","Paratha","Water"],["3","0","1","2","0"],["5","0","0","1","1"],["10","1","0","0","0"]]

To solve this, we will follow these steps −

  • Define one map m

  • Define one set names

  • Define one set t

  • for each element it in order list

    • insert it[1] into t

    • ok := true

    • if ok is false, then −

      • v = an array after splitting it[2] using blank space

      • for each element x in v, do

        • (increase m[it[1], x] by 1)

        • insert x into names

    • Otherwise

      • (increase m[it[1], it[2]] by 1)

      • insert it[2] into names

  • Define one 2D array ret

  • Define an array temp and copy element from names

  • insert "Table" as the first element into temp

  • insert temp at the end of ret

  • for each element it in t, do

    • Define an array te

    • insert it at the end of te

    • for each element x in names, do

      • insert m[it, x] as string at the end of te

    • insert te at the end of ret

  • sort the array ret

  • return ret

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<vector<string> > v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << "[";
      for(int j = 0; j <v[i].size(); j++){
         cout << v[i][j] << ", ";
      }
      cout << "],";
   }
   cout << "]"<<endl;
}
typedef long long int lli;
class Solution {
public:
   vector <string> split(string& s, char delimiter){
      vector <string> tokens;
      string token;
      istringstream tokenStream(s);
      while(getline(tokenStream, token, delimiter)){
         tokens.push_back(token);
      }
      return tokens;
   }
   static bool cmp(vector <string>& a, vector <string>& b){
      lli an = stol(a[0]);
      lli bn = stol(b[0]);
      return an < bn;
   }
   vector<vector<string>> displayTable(vector<vector<string>>& o) {
      map <string, map < string, int> >m;
      set <string> names;
      set <string> t;
      for(auto &it : o){
         t.insert(it[1]);
         bool ok = true;
         if(!ok){
            vector <string> v = split(it[2], ' ');
            for(auto& x : v){
               m[it[1]][x]++;
               names.insert(x);
            }
         }
         else{
            m[it[1]][it[2]]++;
            names.insert(it[2]);
         }
      }
      vector < vector <string> > ret;
      vector <string> temp(names.begin(), names.end());
      temp.insert(temp.begin(), "Table");
      ret.push_back(temp);
      for(auto& it : t){
         vector <string> te;
         te.push_back(it);
         for(auto& x : names){
            te.push_back(to_string(m[it][x]));
         }
         ret.push_back(te);
      }
      sort(ret.begin() + 1, ret.end(), cmp);
      return ret;
   }
};
main(){
   Solution ob;
   vector<vector<string>> v = {{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried
Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}};
   print_vector(ob.displayTable(v));
}

Input

{{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}}

Output

[[Table, Biryni, Fried Chicken, Paratha, Water, ],[3, 0, 1, 2, 0, ],[5, 0, 0, 1, 1, ],[10, 1, 0, 0, 0, ],]
raja
Updated on 17-Nov-2020 12:00:26

Advertisements