-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodechallenge_008.py
137 lines (107 loc) · 4.11 KB
/
codechallenge_008.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'''
Date: 12/20/2018
Problem description:
====================
This problem was asked by Snapchat.
Given an array of time intervals (start, end) for classroom lectures
(possibly overlapping), find the minimum number of rooms required.
For example, given [(30, 75), (0, 50), (60, 150)], you should return 2.
(room)
^
|
3 -
|
2 -************** ***************************
|
1 - **************
|
0 ----.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--> (time)
| 10 20 30 40 50 60 70 80 90 110 130 160
Base on the above graph of distribution, the return value is 2 rooms.
Algorithm:
=========
Input: list of tuples
Output: integer value
Psuedo code:
1. Edge cases-- check empty array, start > end time
2. Convert the array of tuples into a hash table (dict).
3. Copy the keys into start-time list, values into end-time list
4. Loop the length of one list, if subsequent start-time is less than
previous end-time then overlap occurs, get another room.
Note, it is easier to hash the array by converting it into a dictionary.
Also, no time gap in between back-to-back schedules is considered in this implementation.
Note: function starts with test_*() will be automatically executed using command pytest .\codechallenge_008.py
After thought:
==============
Resource allocation is applicable to many practical problems such as
hotel reservation, directing uber drivers, etc.
'''
import random, time
def reservedRooms(schedList):
# edge cases
if len(schedList) == 0:
return 0
# convert list into hash table
dSchedList = dict(schedList)
rooms = 0
stimes = list(dSchedList.keys())
etimes = list(dSchedList.values())
# loop over the list, if subsequent start-time is less than
# previous end-time then overlap occurs, get another room.
for i in range(len(stimes)-1):
if stimes[i+1] > stimes[i]:
rooms+=1
if etimes[i] < stimes[i+1]:
rooms+=1
return rooms
def genRandomSchedule(slots):
sched = [(int(100*random.random()), int(100*random.random())) for i in range(slots)]
sched.sort(reverse=False)
dsched = dict(sched)
dsched = {key:val+key for (key, val) in dsched.items()}
# Since random generator is used for pass-in schedList,
# check if endTime is less than startTime.
# if found we ignore the particular item by removing it from the dictionary
xsched = dsched.copy() # shallow copy of original list to avoid changes during iteration
for start, end in dsched.items():
if start >= end:
del xsched[start]
return list(xsched.items())
# test cases. Written for pytest .\codechallenge_008.py
def test_code():
schedtime = [(30, 75), (0, 50), (60, 150)]
begin = time.time()
assert reservedRooms(schedtime) == 2
end = time.time()
print("Elapsed time: {}".format(end-begin))
if __name__ == "__main__":
# test base case given in the problem
schedtime = [(30, 75), (0, 50), (60, 150)]
print("Given a schedule: {}".format(schedtime))
begin = time.time()
print ("Number of required room(s): {}".format(reservedRooms(schedtime)))
end = time.time()
print("Elapsed time: {}\n".format(end-begin))
# test using random generator
schedtime = genRandomSchedule(5)
print("Random generated schedule: {}".format(schedtime))
begin = time.time()
print ("Number of required room(s): {}".format(reservedRooms(schedtime)))
end = time.time()
print("Elapsed time: {}\n".format(end-begin))
'''
run-time ouput:
===============
Given a schedule: [(30, 75), (0, 50), (60, 150)]
Number of required room(s): 2
Elapsed time: 6.69956207275e-05
Random generated schedule: [(35, 88), (5, 29), (59, 66), (57, 118), (90, 126), (27, 116)]
Number of required room(s): 4
Elapsed time: 5.29289245605e-05
================================= test session starts ==================================
platform linux2 -- Python 2.7.13, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: /home/markn/devel/py-src/DailyCodeChallenge, inifile:
collected 1 item
codechallenge-08.py . [100%]
=============================== 1 passed in 0.12 seconds ===============================
'''