【发布时间】:2014-04-25 18:50:27
【问题描述】:
我一直在尝试编写一个座位预订程序,它执行以下操作:
- 接受用户输入(行、座位数)
- 根据上述输入检查外部 CSV 文件中的可用席位。
- 返回空闲座位的数量(如果有)并且座位号或告诉用户该行没有足够的空间。
一切正常,但是我正在努力检索免费座位的座位号。我目前的方法有什么想法吗?非常感谢!
import csv
from itertools import groupby
LargeBlock = 0
SpacesReq = int(input("How many spaces do you require? (8 Max.) "))
while SpacesReq > 8:
print("Invalid amount.")
break
SectionReq = input("What row would you like to check between A-E? (Uppercase required) ")
with open('data.csv', 'rt') as file:
open_f = csv.reader(file, delimiter=',')
for line in open_f:
if line[10] == SectionReq:
LargeBlock = max(sum(1 for _ in g) for k, g in groupby(line) if k == '0')
if SpacesReq > LargeBlock:
print('There are only ', LargeBlock, ' seats together, available on row ',SectionReq,'.')
else:
print('There is room for your booking of ',SpacesReq,' seats on row ',SectionReq,'.')
break
CSV 结构
1 0 1 0 0 0 0 0 0 0 E
0 0 0 0 0 0 0 0 0 0 D
0 0 0 0 0 1 0 0 0 0 C
0 0 0 0 0 0 0 0 1 0 B
0 0 0 0 0 1 1 1 1 1 A
【问题讨论】:
-
这里有一个权衡 - 您可以在计算是否有足够的座位时收集座位位置(在这种情况下,如果没有足够的座位,您会“浪费”位置的努力座位),或者一旦你知道有足够的位置,你就可以找到位置(当有足够的座位时,你要做双倍的工作)。
标签: python python-3.x