Skip to content

Instantly share code, notes, and snippets.

@sangwonl
Created September 27, 2017 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sangwonl/34aa2715a6fccafc4756ebc668eb270a to your computer and use it in GitHub Desktop.
Save sangwonl/34aa2715a6fccafc4756ebc668eb270a to your computer and use it in GitHub Desktop.
#-*- coding: utf-8 -*-
import unittest
DAY_OF_WEEK = ['월', '화', '수', '목', '금', '토', '일']
DOW_INDEX_DISTANCE_TBL = {
# 1st element: base index ex) 0: 월, 1: 화, ...
# 2nd element: distance ex) 수-월=2, 목-월=3
(0, 0): '월',
(1, 0): '화',
(2, 0): '수',
(3, 0): '목',
(4, 0): '금',
(5, 0): '토',
(6, 0): '일',
(0, 2): '월~수',
(0, 3): '월~목',
(0, 4): '월~금',
(0, 5): '월~토',
(0, 6): '월~일',
(1, 2): '화~목',
(1, 3): '화~금',
(1, 4): '화~토',
(1, 5): '화~일',
(2, 2): '수~금',
(2, 3): '수~토',
(2, 4): '수~일',
(3, 2): '목~토',
(3, 3): '목~일',
(4, 2): '금~일',
}
def shorten_days_of_week_representation(days):
bin_repr = [int(d in days) for d in DAY_OF_WEEK]
pairs = []
padding = [0]
s, l = -1, -1
for i, b in enumerate(bin_repr + padding):
if b == 1:
if s < 0:
s, l = i, 0
else:
l += 1
elif s >= 0:
pairs.append((s, l))
s, l = -1, -1
return ','.join([DOW_INDEX_DISTANCE_TBL[p] for p in pairs])
class TestCase(unittest.TestCase):
def test_shorten_days_of_week_representation(self):
days = ['월', '화', '수', '금', '일']
reprensented = shorten_days_of_week_representation(days)
self.assertEqual(reprensented, '월~수,금,일')
days = ['월', '화', '수', '금', '토', '일']
reprensented = shorten_days_of_week_representation(days)
self.assertEqual(reprensented, '월~수,금~일')
days = ['월', '화', '수', '목', '금', '토', '일']
reprensented = shorten_days_of_week_representation(days)
self.assertEqual(reprensented, '월~일')
days = ['월', '수', '금', '일']
reprensented = shorten_days_of_week_representation(days)
self.assertEqual(reprensented, '월,수,금,일')
days = ['수', '목', '금', '일']
reprensented = shorten_days_of_week_representation(days)
self.assertEqual(reprensented, '수~금,일')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment