2014-03-19 2 views
0

내가 다음을 수행 좌석 예약 프로그램을 코딩하는 노력했습니다 값 :검색 중 목록 항목

  • 는 대한 사용자 입력 (행, 좌석 번호)
  • 확인 외부 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 
+0

충분한 좌석이 있는지 여부를 계산할 때 좌석 위치를 수집 할 수 있습니다 (이 경우 좌석이 충분하지 않은 경우 위치를 "낭비"하고 있음) 또는 일단 자리가 충분하다는 것을 알게되면 위치를 찾을 수 있습니다 (좌석이 충분하면 작업을 두 배로 늘릴 수 있습니다). – selllikesybok

답변

0

여기에 이것에 대해 갈 하나의 다른 방법이다 :

for line in open_f: 
     if line[10] == SectionReq: 

      blockfound = "".join([str(e) for e in line[:-1]]).find("0"*SeatsReq) 

      if blockfound is not -1: 
       print('There are not enough seats together, available on row ',SectionReq,'.') 
      else: 
       print('There is room for your booking of ',SpacesReq,' seats on row ',SectionReq,', in seats ',str(blockfound),' through ',str(SeatsReq+blockfound),'.') 
      break 

귀하의 사양 (서면으로) 얼마나 많은 좌석 사용자에게 우리를 필요로하지 않습니다는 필요에 충분하지 않으면 실제로 행에서 사용할 수 있습니다.

0

,

>>> groups = [(k, len(list(g))) for k, g in groupby(line)] 
>>> groups 
[('1', 1), ('0', 1), ('1', 1), ('0', 7)] 

당신에게/점유 무료 연속 행 다음

의 수의 매핑을 제공합니다 (이 경우 라인 E 사용)

>>> [i for i, x in enumerate(groups) if x[0] == '0' and x[1] > SpacesReq] 
[3] 

충분한 공간을 가진 블록의 모든 인덱스를 제공합니다. 일치하는 첫 번째 블록 또는 그 다음 None

을 반환 blockIndex = next((i for i, x in enumerate(groups) if x[0] == '0' and x[1] > SpacesReq), None)

또는,

>>> sum(blockSize for _, blockSize in groups[:blockIndex]) 
3 

은 충분히 큰 블록 전에 당신에게 좌석의 총 수를 제공합니다.