Home > Enterprise >  Count the Number of Elements with the same Value in a Row?
Count the Number of Elements with the same Value in a Row?

Time:09-17

Let's assume I have the following data structure:

const Table = [
  [
    {
      "id": "258ce34d-cba6-44a8-bdb9-e436d18701aa",
      "seat": 1,
      "group": 1
    },
    {
      "id": "60adc321-c7e3-4d34-963a-e09dc53345d0",
      "seat": 2,
      "group": 1
    }
  ],
  [
    {
      "seat": "empty",
      "group": 0
    },
    {
      "seat": "empty",
      "group": 0
    }
  ],
  [
    {
      "id": "c8c3c973-351b-4314-8096-a6d12c7b01fb",
      "seat": 5,
      "group": 3
    },
    {
      "id": "1c256b45-b3f3-49cc-b7e4-29967594c4fb",
      "seat": 6,
      "group": 3
    }
  ],
  [
    {
      "seat": "empty",
      "group": "empty"
    },
    {
      "seat": 0,
      "group": 0
    }
  ],
  [
    {
      "id": "63469f95-7deb-483c-ad7d-cf0cbdc191b1",
      "seat": 9,
      "group": 5
    },
    {
      "id": "e77c8fb3-2e0b-43f7-a9ca-1bbd8143ba59",
      "seat": 10,
      "group": 5
    }
  ]
]

It is an array that represents a table in a restaurant. In this array there are other arrays representing the groups of guests.

As you can see there are two free slots at the table.

The task now is to write a function that calculates how many seats are free in a row. Input is this array and output should be a number. So in this case MaxFreeSeatsinRow(Table) => 2

For example, if a group of 4 people arrives, there are enough seats but not in one row.

Does anyone have an idea how to calculate this? Thanks for your help!

CodePudding user response:

If I understood correctly, you'd like to know of the number of free seats per nested array?

If so, I think it can be pretty straightforward using a functional approach:


const freeSeats = Table.map(row => {
  return row.filter(place => place.seat === "empty").length
})

// freeSeats = [0, 2, 0, 2, 0]

Then it's easy to get the maximum grouped seats:

Math.max(...freeSeats)

CodePudding user response:

According to your comments, this is really just a matter of flattening the structure to an array of seats and then finding the longest streak of empty seats.

We can write a general-purpose longestStreak function which accepts a predicate function and returns a function which accepts a list of elements, and then tests each element against that predicate, updating the length of the current streak and possibly the maximum value when it matches, and resetting the current streak to zero when it doesn't.

Our main function, canSeat accepts a Table, and supplies to longestStreak a predicate which tests if a seat has value of 0 or "empty" (note: do you really want to support both?) and then supplies to the resulting function an extract of the Table selecting all the seats in it.

const longestStreak = (pred) => (xs) => 
  xs .reduce (
    ({max, curr}, x) => pred (x) 
      ? {max: curr >= max ? curr   1 : max, curr: curr   1} 
      : {max, curr: 0}
    , {max: 0, curr: 0}
  ) .max

const canSeat = (table) => longestStreak 
  (s => s == 0 || s == "empty") 
  (table .flatMap (groups => groups .map (g => g .seat)))

const Table = [[{id: "258ce34d-cba6-44a8-bdb9-e436d18701aa", seat: 1, group: 1}, {id: "60adc321-c7e3-4d34-963a-e09dc53345d0", seat: 2, group: 1}], [{seat: "empty", group: 0}, {seat: "empty", group: 0}], [{id: "c8c3c973-351b-4314-8096-a6d12c7b01fb", seat: 5, group: 3}, {id: "1c256b45-b3f3-49cc-b7e4-29967594c4fb", seat: 6, group: 3}], [{seat: "empty", group: "empty"}, {seat: 0, group: 0}], [{id: "63469f95-7deb-483c-ad7d-cf0cbdc191b1", seat: 9, group: 5}, {id: "e77c8fb3-2e0b-43f7-a9ca-1bbd8143ba59", seat: 10, group: 5}]]

console .log (canSeat (Table))

  • Related