[ Login ]

team calendaring system

5 month(s) ago uolter   comments 


You have been given a team calendaring system where a meeting is stored as a tuple of integers (start_time, end_time) . These integers represent the number of 30-minute blocks past 9:00am. You have been tasked with working out times when the whole team is busy.

For example: (2, 3) # meeting from 10:00 – 10:30 am (6, 9) # meeting from 12:00 – 1:30 pm

Write a function condense_meeting_times() that takes an array of meeting time ranges and returns an array of condensed ranges.

For example, given:

[(0, 1), (3, 5), (4, 8), (10, 12), (9, 10)] your function would return:

[(0, 1), (3, 8), (9, 12)]

Do not assume the meetings are in order. The meeting times are coming from multiple teams.

In this case the possibilities for start_time and end_time are bounded by the number of 30-minute slots in a day. But soon you plan to refactor it to store times as Unix timestamps (which are big numbers). Write something that's efficient even when we can't put a nice upper bound on the numbers representing our time ranges.​

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.
import unittest




def condense_meeting_times(meeting_times):
"""
condense a list of meetings time
:param meeting_tims: list of meetings
:return: list condensed.
"""

merge_lst = []

for t in sorted(meeting_times):

if not merge_lst:
merge_lst.append(t)
else:
s1 = set(range(merge_lst[-1][0], merge_lst[-1][1]+1))
s2 = set(range(t[0], t[1]+1))

if s1 & s2:
u = list(s1.union(s2))
merge_lst[-1]=(u[0], u[-1])
else:
merge_lst.append(t)

return merge_lst



class TestCondenseMeetings(unittest.TestCase):

def setUp(self):
pass


def
test_condense_meetings_ok(self):

meetings = [(0, 1), (3, 5), (4, 8), (10, 12), (9, 10)]

self.assertEqual(
condense_meeting_times(meetings),
[(
0, 1), (3, 8), (9, 12)])

def test_condensate_empty(self):

meetings = []

self.assertEqual( condense_meeting_times(meetings), [])

def test_condensate_disjoint(self):

meetings = meetings = [(2, 3), (6,9)]

self.assertEqual( condense_meeting_times(meetings), meetings)


if __name__ == '__main__':

unittest.main()


Zen

5 month(s) ago uolter   comments 


1.
2.
import this

The Zen of Python, by Tim Peters
  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Flat is better than nested.
  • Sparse is better than dense.
  • Readability counts.
  • Special cases aren't special enough to break the rules.
  • Although practicality beats purity.
  • Errors should never pass silently.
  • Unless explicitly silenced.
  • In the face of ambiguity, refuse the temptation to guess.
  • There should be one-- and preferably only one --obvious way to do it.
  • Although that way may not be obvious at first unless you're Dutch.
  • Now is better than never.
  • Although never is often better than *right* now.
  • If the implementation is hard to explain, it's a bad idea.
  • If the implementation is easy to explain, it may be a good idea.
  • Namespaces are one honking great idea -- let's do more of those!


palindrome check

6 month(s) ago uolter   comments 


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
is_palindrome = lambda x: ''.join([x[i] for i in range(len(x)-1,-1, -1)]) == x


r = '123123'
is_palindrome(r)
False
r = '123321'
is_palindrome(r)
False

# and now .... more concise:
is_palindrome = lambda x: x[::-1] == x



Italy the Extraordinary Commonplace

6 month(s) ago uolter   comments 




 
Back to top