forked from codecodecoder78/RHDEV-BE-1-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-python-question.py
More file actions
83 lines (66 loc) · 2.36 KB
/
Copy path1-python-question.py
File metadata and controls
83 lines (66 loc) · 2.36 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
# 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):
try:
if "applicants" not in request.keys() or len(request["applicants"]) == 0:
raise ValueError
output = {"successfulApplicants" : [], "bannedApplicants" : [], "totalCost" : [], "tickets" : [],}
#filter out banned applicants
output["bannedApplicants"] = list(filter(lambda x: x in bannedVisitors, request["applicants"]))
#filter out successful applicants
output["successfulApplicants"] = list(filter(lambda x: x not in bannedVisitors, request["applicants"]))
#check total cost
totalCost = 0.0
for i in output["successfulApplicants"]:
if memberStatus[i] == True:
totalCost += 3.50
else:
totalCost += 5
output["totalCost"].append(totalCost)
#checks tickets
ticket = {"name": [],"membershipStatus":[],"price":[]}
for i in output["successfulApplicants"]:
ticket["name"].append(i)
if memberStatus[i] == True:
ticket["membershipStatus"].append("True")
ticket["price"].append("3.50")
else:
ticket["membershipStatus"].append("False")
ticket["price"].append("5.00")
return output
except:
return {"error": "No applicants"}
print(processRequest(request))
# {
# successfulApplicants:
# bannedApplicants:
# totalCost:
# tickets: [
# {
# "name": ,
# "membershipStatus": ,
# "price":
# }, ....
# ]
#
# }
# OR
# {"error": "No applicants"}