Commits

Takayuki KONDO committed 5c07881

py: add list-reserved sample

  • Participants
  • Parent commits 45d3d60

Comments (0)

Files changed (1)

py/list-reserved.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import print_function
+
+# あらかじめ 31日分のリストを確保する
+free = ''
+m = [free for n in range(31)]
+
+
+def isvacant(start, days):
+    '''
+    連続した日程が空いているかどうか
+    '''
+    for d in range(days):
+        if m[start+d-1] != free:
+            return False
+    return True
+
+
+def reserve(start, days, name):
+    '''
+    日程が空いている場合のみ予約する
+    '''
+    if isvacant(start, days):
+        for d in range(days):
+            m[start+d-1] = 'reserved ' + name
+        print('success reserved')
+        return True
+    else:
+        print('failed reserved')
+        return False
+
+
+def show():
+    for i, v in enumerate(m):
+        print("%d day: %s" % (i+1, v))
+
+reserve(2, 1, 'Taiwan')  # -> success
+reserve(4, 3, 'Taiwan')  # -> success
+reserve(1, 7, 'Swiss')   # -> fail
+reserve(1, 1, 'Swiss')
+show()