Ad

Your family members are a bit picky when it comes to choose who they want to seat next to in a family outing to the restaurant. Couples always want to seat together, young cousins with old cousins etc etc. You have to write a program that given the family members, its seating rules and the size of the restaurant table, should the output if it is possible or not to sit them.

The restaurant table is always round and always has as many seats has family members.

example
// family members seating rules
canTheySit(["Mark", "Marta", "Oliver"], ["Mark", "Marta"]) == true

function arrangeSeating(table, seating)
{
  // first check if table is in any one of this solutions
  // none is seated
  // one is seated 
  // both are seated
  
  var seated = {};
  seated[seating[0]] = false;
  seated[seating[1]] = false;
  
  for(var i = 0; i < table.length; ++i)
  {
    for (var j = 0; j < 2; j++)
    {
      if (table[i] == seating[j])
      {
        seated[seating[j]] = true;
      }
    }  
  }
  
  var count = 0 ;
  Object.keys(seated).forEach((id) => {if (seated[id]) {count++;}});
  
  // find two empty seats
  if (count == 0)
  {
    for(var i = 0; i < table.length; ++i)
    {
      if (table[i] === "Empty" && table[(i + 1) % table.length] == "Empty")
      {
        table[i] = seating[0];
        table[(i + 1) % table.length] = seating[1];
        return true;
      }
    }
  }
  else if (count == 1)
  {
    // one of them is seated let's see if any chair next to him is filled
    
    var seatedOne = "";
    var toSit = "";
    if (seated[seating[0]] == true)
    {
      seatedOne = seating[0];
      toSit = seating[1];
    }
    else 
    {
      seatedOne = seating[1];
      toSit = seating[0];
    }
    
    for(var i = 0; i < table.length; ++i)
    {
      if (table[i] === seatedOne)
      {
        var rightSeat = (i + 1) % table.length;
        var leftSeat  = ((table.length + i) - 1) % table.length;
        if (table[rightSeat] == "Empty")
        {
          table[rightSeat] = toSit;
          return true;
        }
        else if (table[leftSeat] == "Empty")
        {
          table[leftSeat] = toSit;
          return true;
        }
        else 
        {
          return false;
        }
        
      }
    }
  }
  else
  {
    // both are seated check if they are next to each other
    for(var i = 0; i < table.length; ++i)
    {
      if (table[i] === seating[0])
      {
        var rightSeat = (i + 1) % table.length;
        var leftSeat  = ((table.length + i) - 1) % table.length;
        if (table[rightSeat] === seating[1])
        {
          return true;
        }
        else if (table[leftSeat] === seating[1])
        {
          return true;
        }
        else 
        {
          return false;
        }
      }
    }
  }
  return false;
}

function canTheySit(familyMembers, rules)
{
  var indices = {};
  var indices_inv = {};
  var pairs = Array(familyMembers.length).fill().map(()=>Array(familyMembers.length).fill(false));
  
  for (var i = 0; i < familyMembers.length; ++i)
  {
    indices[familyMembers[i]] = i;
    indices_inv[i] = familyMembers[i];
  }
  rules.forEach((rule) => 
  {
    var pair1 = indices[rule[0]];
    var pair2 = indices[rule[1]];
    pairs[pair1][pair2] = true;
    pairs[pair2][pair1] = true;
  });
  
  var table = Array(familyMembers.length).fill("Empty");
  
  var toReturn = true;
  var i = 0;
  pairs.forEach((list) =>
  {
    var total = 0;
    for (var j = i + 1;  j < list.length; ++j)
    {
      if (list[j]) 
      {
        // find if possible to sit
        if (!arrangeSeating(table, [indices_inv[i], indices_inv[j]]))
        {
          toReturn = false;
        }
      } 
    }
    i++;
  });
  
  return toReturn;
}

You and your friend only have a limit span of free time per day to meet, given your free hour range and his find if you will be able to meet.

Given a start hour and end hour for your free time and a start hour and end hour of your friend free time, write a function that return True or False if you will be able to meet with your friend.

The Hours will be given the 24 h format.

For example :

13 - 15 and 14 - 16 ==> true
0 - 3 and 23 - 2 ==> true
1 - 3 and 23 - 24 ==> false

#include <cmath>

static float hourToRad(int hour)
{
  return (float) hour * (M_PI / 12.0);
};

static bool willTheyMeet(
  int startHour1, 
  int endHour1, 
  int startHour2,
  int endHour2)
{
  auto a1 = hourToRad(startHour1);
  auto a2 = hourToRad(endHour1);
  auto b1 = hourToRad(startHour2);
  auto b2 = hourToRad(endHour2);
  
  if (a1 == b2 || a2 == b1)
                return false;
  float da = (a2 - a1) / 2.0;
  float db = (b2 - b1) / 2.0;
  float ma = (a2 + a1) / 2.0;
  float mb = (b2 + b1) / 2.0;
  float cda = cos(da);
  float cdb = cos(db);
  return cos(ma - b1) >= cda ||
         cos(ma - b2) >= cda ||
         cos(mb - a1) >= cdb ||
         cos(mb - a2) >= cdb;
};