Johny19
Johny19

Reputation: 5582

Business opening hours grouping algorithm

I have row of data like this that represent a business opening hours

day / opentime   / closetime  / isOpen
0      09:00:00    17:00:00      true
1      09:00:00    17:00:00      true
2      08:00:00    17:00:00      true
3      09:00:00    17:00:00      true
4      09:00:00    17:00:00      true
5                                false
6      09:00:00    17:00:00      true

with day being an Integer from 0-6 (mon to sunday) and iOpen

Before Re-inventing the wheel and start thinking on a new algorithm I would like to know if there already some algo that would do something similar to this:

MON - TUE 9am - 5pm
WED 8am - 5pm
THU - FRI 9am - 5pm
SUN  9am - 5pm

basically grouping the day that have the opening and closing time together ?

I'm not asking for a ready to go algorithm but more of an advice to where to look if there is already something similar that has been done.

ps: bonus question. Is the way I store the data efficient to achieve my goal ?

Upvotes: 1

Views: 624

Answers (1)

Michael Tontchev
Michael Tontchev

Reputation: 1109

This is a fairly straightforward algorithm. Just write down in code what you would do as a human.

As a human, when I look at the data you give me, I start with Monday, and I write down the open/close times on a sheet of paper and write "Monday" next to them. Then I look at Tuesday. If Tuesday has the same open/close times as the previous day, then I simply go to where the time was already written on my paper and add "Tuesday" right next to Monday. I keep doing this until I find an open/close time that is not the same as the previous day's. In that case, I go to a new line on my paper and write down the new open/close times and continue on like this until the end.

If I were to program this in C, I would use an array of structs, where the struct merely had the open time, the close time, and an array of character arrays (ie, an array of strings) to store the dates associated with those hours. (Note: there could be more efficient ways of storing this, but this seems good enough for your purposes)

Upvotes: 2

Related Questions