forked from codecodecoder78/RHDEV-BE-1-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-python-question.py
83 lines (70 loc) · 2.03 KB
/
1-python-question.py
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
# This program simualtes the backend of a ticket purchasing system
# Price per visitor is $5
# Price per member is $3.50
# You are to do the following
# 1. Identify all banned visitors with a filter call
# 2. Determine the memberships status of all applicants
# 3. Calculate the total price for all eligible visitors
# 4. For each valid visitor, return a corresponding ticket in Dictionary form
# 5. Return an error via thrown exception if applicants is empty
# Complete everything above in a function called processRequest
# Your should abstract out function as much as reasonably possible
bannedVisitors = ["Amy", "Grace", "Bruce"]
memberStatus = {
"Ally": True,
"David": True,
"Brendan": False
}
request = {
"applicants": ["Amy", "Ally", "David", "Brendan", "Zoho"]
}
def initOutputFormat():
output={
"successfulApplicants":[],
"bannedApplicants":[],
"totalCost":0,
"tickets":[],
}
return output
def banFilter(app, output):
if app in bannedVisitors:
output['bannedApplicants'].append(app)
return True
return False
def ticketProcessing(app, output):
ticket={
"name": app,
"membershipStatus": memberStatus.get(app, False),
"price": 3.5 if memberStatus.get(app, False) else 5
}
output['tickets'].append(ticket)
output['successfulApplicants'].append(app)
output['totalCost']+=ticket['price']
def processRequest(request):
try:
if len(request["applicants"])==0:
raise Exception({"error": "No applicants"})
output = initOutputFormat()
for app in request["applicants"]:
if banFilter(app, output):
continue
ticketProcessing(app, output)
return output
except Exception as e:
return e
print(processRequest(request))
# {
# successfulApplicants:
# bannedApplicatns:
# totalCost:
# tickets: [
# {
# "name": ,
# "membershipStatus": ,
# "price":
# }, ....
# ]
#
# }
# OR
# {"error": "No applicants"}