0

In reference to an answer already given to another question:

https://stackoverflow.com/a/1609686/1183608

I am working with this answer, however I am confused on the meaningfulness of calculating the acceptable_times array. I want to find out at what times of the day (schedule) are available for the specific time slot length, but the function for doing so in the above answer does not seem to satisfy that, unless I have missed something.

Community
  • 1
  • 1
grioja
  • 3
  • 1

1 Answers1

1

grioja, the answer you linked to does give you the times of day which are available for your desired slot length. The busy/available times are represented by bits, for example:

1100001110

(This would be 30 available minutes, followed by 60 busy minutes, followed by 45 available, followed by 15 busy.) Then you get a binary number which represents the length of the available time slot you want to find, say:

111

(This would mean you want to find a slot of 45 available minutes.) Then you try shifting the 2 numbers against each other:

1100001110
0000000111

1100001110
0000001110

1100001110
0000011100

...and so on, for every possible relative shift length. The AND operation he shows in that answer determines whether the 1's in the "desired slot length" number are matching all 1's in the "available times" number. If they are, you have found an acceptable time.

By the way, you could do the same thing in other ways. For example, you could represent busy/available times by characters in a string, and use a regular expression to find an available slot of the desired length.

Note that this general approach (which is basically just "linear search") will not scale if the list of available/busy time slots is very long. There are other approaches which will be much faster in that case.

Alex D
  • 29,755
  • 7
  • 80
  • 126
  • Thanks, that makes way more sense. I understand what the loop does now, just what it was returning wasn't very meaningful, which I have rectified. If you have any other sources on scheduling, or alternatives to this approach that you would think easier please let me know. – grioja Feb 01 '12 at 23:21