Skip to content

Instantly share code, notes, and snippets.

@thekindlyone
Created October 2, 2014 08:36
Show Gist options
  • Save thekindlyone/7d57da421fd7869f2a45 to your computer and use it in GitHub Desktop.
Save thekindlyone/7d57da421fd7869f2a45 to your computer and use it in GitHub Desktop.
Airplane booking thing
D:\pythonary\chall>python simple.py
Please Enter Name: Aritra Das
Enter class (1 for economy, 2 for business): 1
Booking Successful. Your seat is: B1
A B C D
1 O * O O
2 O O O O
3 O O O O
4 O O O O
Legend =>
Booked: B
Open: O
Your Seat: *
Please Enter Name: Derrick
Enter class (1 for economy, 2 for business): 2
Booking Successful. Your seat is: B3
A B C D
1 O B O O
2 O O O O
3 O * O O
4 O O O O
Legend =>
Booked: B
Open: O
Your Seat: *
Please Enter Name: seating
A B C D
1 O B O O
2 O O O O
3 O B O O
4 O O O O
Legend =>
Booked: B
Open: O
Your Seat: *
Please Enter Name: Derrick
Seat Already booked for this name. Seat number: B3
A B C D
1 O B O O
2 O O O O
3 O * O O
4 O O O O
Legend =>
Booked: B
Open: O
Your Seat: *
Please Enter Name: another
Enter class (1 for economy, 2 for business): 2
Booking Successful. Your seat is: C3
A B C D
1 O B O O
2 O O O O
3 O B * O
4 O O O O
Legend =>
Booked: B
Open: O
Your Seat: *
Please Enter Name: quit
D:\pythonary\chall>python simple.py
Please Enter Name: seating
A B C D
1 O B O O
2 O O O O
3 O B B O
4 O O O O
Legend =>
Booked: B
Open: O
Your Seat: *
Please Enter Name: quit
D:\pythonary\chall>
from random import choice
import sys
from os.path import isfile
import pickle
def flightinfo(seats,name=None):
return ' A B C D \n'+(
'\n'.join(
[str((d/4)+1)+' '+''.join(map(
lambda seat: '* ' if seats.get(name,None)==seat else 'B ' if seat in seats.values() else 'O ',
[a,b,c,d]))
for a,b,c,d in zip(*[iter(range(16))]*4)
]
)
) + prompts['legend']
def book(name,available,seats):
seats[name]=choice(available)
return seats
def seatno(num): # get fancy naming of seats from index naming scheme
return {1:'A',2:'B',3:'C',4:'D'}.get((num%4)+1)+str((num/4)+1)
def inp(prompt,seats):
rv=raw_input(prompt)
if rv.lower().strip()=='quit':
quit(seats)
if rv.lower().strip()=='seating':
print flightinfo(seats)
return False
else:
return rv
def quit(seats): #persistance on exit
with open('seats.p','wb') as f:
pickle.dump(seats,f)
sys.exit()
prompts={ 'name' :'Please Enter Name: ',
'class' :'Enter class (1 for economy, 2 for business): ',
'seat' : 'Enter Desired seat: ',
'exists' :'Seat Already booked for this name. Seat number: ',
'legend' :'\nLegend =>\nBooked: B\nOpen: O\nYour Seat: *\n\n',
'other' :'This class is already full, try another.',
'error' :'BS input, try again.',
'success':'Booking Successful. Your seat is: ',
'full' :'All seats full.'
}
ranges={'1':range(8),'2':range(8,16)} #convenience dictionary to get all seats of given catagory
if isfile('seats.p'): # persistance stuff at start
with open('seats.p') as f:
seats=pickle.load(f)
else:
seats={}
while True:
if len(seats)==16:
print prompts['full']
if(raw_input('Want to clear data?(y/n) ').lower()=='y' ):
seats={}
print 'Data cleared!\n\n'
continue
else:
quit(seats)
available=None
name=inp(prompts['name'],seats)
if not name:
continue
if seats.has_key(name):
print prompts['exists'],seatno(seats[name])
print flightinfo(seats,name=name)
continue
while not available:
catagory=inp(prompts['class'],seats).strip()
if catagory not in ['1','2']:
print prompts['error']
continue
available=set(ranges[catagory])-set(seats.values()) #all elements of set of seats of catagory that are not in booked seats too
if available:
seats=book(name,list(available),seats)
print prompts['success'], seatno(seats[name])
print flightinfo(seats,name=name)
else:
print prompts['other']
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment