-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathairport_clearance.py
More file actions
87 lines (71 loc) · 2.37 KB
/
airport_clearance.py
File metadata and controls
87 lines (71 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
python2.7
This script is used to address following problem statement:
There are x number of flight and y number of freights in queue for clearance at airport.
The priority is given to flight clearance.
Even if a freight is encountered, make sure there is no flight in the queue next, if there is, allo the flight.
IMP: Pick a random element from (x+y), it could be flight or freight.
"""
import random
import sys
class Clearance(object):
"""docstring for Clearance"""
def __init__(self):
super(Clearance, self).__init__()
self.in_queue = None
def input_values(self):
try:
self.total_flights = int(raw_input("Enter Flights count\t: "))
self.total_freights = int(raw_input("Enter Freights count\t: "))
except ValueError as verr:
print "Sorry. Please enter numeric values.\n\n"
sys.exit()
def process_values(self):
self.flight_ids = map(lambda x: "Flight #" + str(x), xrange(self.total_flights))
self.frieght_ids = map(lambda x: "Freight #" + str(x), xrange(self.total_freights))
self.merged_ids = self.flight_ids + self.frieght_ids
def pending_ids(self, merged_ids):
self.toggle_in_queue(self.picked_ele)
self.get_random()
def push_ele(self, ele_name):
print "Allowed: {0}".format(ele_name)
def clear_all(self):
if self.in_queue:
self.push_ele(self.in_queue)
if self.picked_ele:
self.push_ele(self.picked_ele)
sys.exit("\n******** Successfully Completed!! ********")
def toggle_in_queue(self, picked_ele):
if not self.merged_ids:
self.clear_all()
if self.in_queue:
if "Flight" in self.picked_ele:
self.push_ele(self.picked_ele)
self.push_ele(self.in_queue)
self.in_queue = None
else:
self.push_ele(self.in_queue)
self.push_ele(self.picked_ele)
self.in_queue = None
else:
if "Freight" in self.picked_ele:
print "Queued: {0}".format(self.picked_ele)
self.in_queue = self.picked_ele
# Wait for second ele using get_random()
else:
# print "\nLast: Flight", self.in_queue
self.push_ele(self.picked_ele)
self.get_random()
def get_random(self):
'''To pick a random of All ids'''
if self.merged_ids:
self.picked_ele = random.choice(self.merged_ids)
print "\nRequest: {0}".format(self.picked_ele)
self.merged_ids.remove(self.picked_ele)
self.pending_ids(self.merged_ids)
def begin(self):
self.input_values()
self.process_values()
self.get_random()
c = Clearance()
c.begin()