forked from codecodecoder78/RHDEV-BE-1-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-python-question.py
94 lines (72 loc) · 2.23 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
84
85
86
87
88
89
90
91
92
93
94
# 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 processRequest(request):
class EmptyApplicantsException(Exception):
pass
try:
applicants = request["applicants"]
if len(applicants) == 0:
raise EmptyApplicantsException
except EmptyApplicantsException:
return {"error": "No applicants"}
to_return = {}
successfulApplicants = list(filter(lambda x: x if x not in bannedVisitors else None, applicants ))
bannedApplicants = list(filter(lambda x: x if x in bannedVisitors else None, applicants ))
#find prices
member_tickets = []
total_price = 0
for i in successfulApplicants:
try:
member = memberStatus[i]
except KeyError:
member = False
if member:
price = 3.50
else:
price = 5
ticket = {
"name" : i,
"membershipStatus" : member,
"price" : price,
}
member_tickets.append(ticket)
total_price += price
to_return["successfulApplicants"] = successfulApplicants
to_return["bannedApplicants"] = bannedApplicants
to_return["totalCost"] = total_price
to_return["tickets"] = member_tickets
return to_return
print(processRequest(request))
# {
# successfulApplicants:
# bannedApplicatns:
# totalCost:
# tickets: [
# {
# "name": ,
# "membershipStatus": ,
# "price":
# }, ....
# ]
#
# }
# OR
# {"error": "No applicants"}