forked from vinatsx/RHDEVS-AY2223-BE1-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pythonhw.py
85 lines (73 loc) · 2.21 KB
/
pythonhw.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
# 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 = ["Charles", "Grace", "Bruce"]
memberStatus = {
"Ally": True,
"David": True,
"Brendan": False
}
request = {
"applicants": []
}
def filterbanned(bannedVisitors, applicants):
banned=[]
for i in applicants:
if i in bannedVisitors:
banned.append(i)
return banned
def filtersuccessful(bannedVisitors, applicants):
successful=[]
for i in applicants:
if i not in bannedVisitors:
successful.append(i)
return successful
def processRequest(request):
applicants = request.get("applicants")
if not applicants:
raise Exception("There are no applicants")
members = memberStatus.keys()
bannedApplicants = filterbanned(bannedVisitors, applicants)
successfulApplicants = filtersuccessful(bannedVisitors, applicants)
totalCost=0
tickets = []
for i in successfulApplicants:
if i in members and memberStatus[i] == True:
totalCost += 3.50
tickets.append({
"name": i,
"membershipStatus": True,
"price": 3.50
})
else:
totalCost += 5
tickets.append({
"name": i,
"membershipStatus": False,
"price": 5
})
return tickets, totalCost
print(processRequest(request))
# {
# successfulApplicants:
# bannedApplicatns:
# totalCost:
# tickets: [
# {
# "name": ,
# "membershipStatus": ,
# "price":
# }, ....
# ]
#
# }
# OR
# {"error": "No applicants"}