1

I've a list a = [(1,3),(3,7),(1,10),(3,5),......] So on. i.e (entry_time, exit_time).

Where the 1st element in each tuple is the entry time of an employee and the 2nd element is the exit time. need to find the time like in which hour of the day there were maximum people in the office.

Eg output:

{1'00: 10, 2'00: 20, 3'00: 15}

So the final output should be 2'00 with count 20.

Yanirmr
  • 923
  • 8
  • 25
Abhishek_K
  • 21
  • 4
  • Does this answer your question? [giving lots of intervals \[ai, bi\], find a interval which intersect with the most number of intervals](https://stackoverflow.com/questions/9672434/giving-lots-of-intervals-ai-bi-find-a-interval-which-intersect-with-the-most) – Yanirmr Mar 02 '20 at 07:49
  • look at this question: (https://stackoverflow.com/questions/9672434/giving-lots-of-intervals-ai-bi-find-a-interval-which-intersect-with-the-most and this one:https://stackoverflow.com/questions/21966886/given-a-set-of-intervals-find-the-interval-which-has-the-maximum-number-of-inte – Yanirmr Mar 02 '20 at 07:50

3 Answers3

4

Use list comprehension with flatten and range, then use collections.Counter and last extract maximum:

a = [(1, 3), (3, 7), (1, 10), (3, 5)]

from collections import Counter

d = Counter([f'{y}:00' for s, e in a for y in range(s, e + 1)])
print(d)
Counter({'3:00': 4, '4:00': 3, '5:00': 3, '1:00': 2, '2:00': 2,
     '6:00': 2, '7:00': 2, '8:00': 1, '9:00': 1, '10:00': 1})

maximum = max(d, key=d.get)
print(maximum, d[maximum])

3:00 4

If last value of tuple is not count:

d = Counter([f'{y}:00' for s, e in a for y in range(s, e)])
print (d)
Counter({'3:00': 3, '4:00': 3, '1:00': 2, '2:00': 2,
         '5:00': 2, '6:00': 2, '7:00': 1, '8:00': 1, '9:00': 1})

maximum = max(d, key=d.get)
print(maximum, d[maximum])
3:00 3
Yanirmr
  • 923
  • 8
  • 25
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

I want to suggest a simpler approach (but, maybe less efficient). You can use two for-loops. One for the hours and second for the person tuples.

a = [(1, 3), (3, 7), (1, 10), (3, 5)]
#  Initiate list for hours the represent the counter
rush_hour_status = [0] * 24
# Iterate over the day hours 12/24, doesn't matter.
for hour in range(24):
    for person in a:
        if person[0] <= hour and person[1] > hour:  # Check if this person is working in this hour
            rush_hour_status[hour] += 1

max_rush = max(rush_hour_status)
max_rush_index = (rush_hour_status.index(max_rush))
print(max_rush_index, max_rush)
Yanirmr
  • 923
  • 8
  • 25
0

Here is the code with O(n) complexity which prints list of peak hours

Assumption: Time is entered in 24 hrs format

a = [(10,15),(11,18),(10,11),(13,15)]
time_table = {}

for x in range(0,24):
    time_table[x]=0

for i in a:
    time_table[i[0]] = time_table[i[0]]+1
    time_table[i[1]] = time_table[i[1]]-1
    print(time_table[i[0]], time_table[i[1]])

for x in range(1,24):
    time_table[x]=time_table[x-1]+time_table[x]

print(time_table)

max_value = max(time_table.values())

def getKeysByValue(dictOfElements, valueToFind):
    listOfKeys = list()
    listOfItems = dictOfElements.items()
    for item  in listOfItems:
        if item[1] == valueToFind:
            listOfKeys.append(item[0])
    return  listOfKeys

listOfKeys = getKeysByValue(time_table, max_value)
print(max_value, listOfKeys)
Anup Thomas
  • 198
  • 6