Repository navigation
Expand file tree
/
Copy pathtask_1.py
More file actions
87 lines (72 loc) · 1.68 KB
/
Copy pathtask_1.py
File metadata and controls
87 lines (72 loc) · 1.68 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
'''
Task 1: Transactions Processing
Input Dataset
[
{"user_id": 1, "amount": 100},
{"user_id": 2, "amount": 200},
{"user_id": 1, "amount": 150},
{"user_id": 3, "amount": 300},
{"user_id": 2, "amount": -50},
{"user_id": null, "amount": 500},
{"user_id": 4, "amount": "invalid"}
]
Requirements
Ignore invalid records:
user_id is null
amount is not a number
Allow negative amounts (refunds)
Calculate:
Total spend per user
Top spender (highest total)
Expected Output
{
"totals": {
"1": 250,
"2": 150,
"3": 300
},
"top_spender": 3
}
'''
data = [
{"user_id": 1, "amount": 100},
{"user_id": 2, "amount": 200},
{"user_id": 1, "amount": 150},
{"user_id": 3, "amount": 300},
{"user_id": 2, "amount": -50},
{"user_id": None, "amount": 500},
{"user_id": 4, "amount": "invalid"}
]
# print(data)
clean_data = []
for i in data:
user_id=i["user_id"]
amount=i["amount"]
print(user_id,amount,type(amount))
if user_id != None and type(amount) == int:
temp = {"user_id":user_id,"amount":amount}
clean_data.append(temp)
else:
print("--------")
pass
# print(clean_data)
def total_spend_user(data):
user_ids = [i["user_id"] for i in data]
user_ids = set(user_ids)
print(user_ids)
total_spend_user=[]
total =0
for i in data:
user_id = i["user_id"]
amount = i["amount"]
print(user_id,amount)
for j in user_ids:
print("---",user_id,j)
if user_id == j:
total += amount
total_spend_user.append({user_id,total})
else:
pass
return total_spend_user
print(total_spend_user(clean_data))
print(clean_data)