Commits

Takayuki KONDO  committed 5c07d09

py: improve list-reserved

  • Participants
  • Parent commits 5c07881

Comments (0)

Files changed (1)

File py/list-reserved.py

 from __future__ import print_function
 
 # あらかじめ 31日分のリストを確保する
-free = ''
-m = [free for n in range(31)]
+m = [{'isbook': False, 'place': '', 'start': 0, 'end': 0, 'days': 0}
+     for n in range(31)]
 
 
 def isvacant(start, days):
     連続した日程が空いているかどうか
     '''
     for d in range(days):
-        if m[start+d-1] != free:
+        if m[start+d-1]['isbook'] is True:
             return False
     return True
 
 
-def reserve(start, days, name):
+def reserve(start, end, place):
     '''
     日程が空いている場合のみ予約する
     '''
+    days = end - start + 1
+    if days < 1:
+        return False
+
     if isvacant(start, days):
         for d in range(days):
-            m[start+d-1] = 'reserved ' + name
-        print('success reserved')
+            m[start+d-1]['isbook'] = True
+            m[start+d-1]['start'] = start
+            m[start+d-1]['end'] = end
+            m[start+d-1]['days'] = days
+            m[start+d-1]['place'] = place
+        print('success reserved %s %d - %d' % (place, start, end))
         return True
     else:
         print('failed reserved')
 
 def show():
     for i, v in enumerate(m):
-        print("%d day: %s" % (i+1, v))
+        print("%2d 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()
+
+def main():
+    reserve(2, 3, 'Taiwan')  # -> success
+    reserve(4, 7, 'Canada')  # -> success
+    reserve(1, 7, 'Swiss')   # -> fail
+    reserve(1, 1, 'Swiss')   # -> success
+    reserve(10, 11, 'New Zealand')   # -> success
+    reserve(30, 29, 'Hawaii')   # -> fail
+    show()
+
+if __name__ == '__main__':
+    main()