-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug_script.py
More file actions
106 lines (85 loc) · 3.12 KB
/
Copy pathbug_script.py
File metadata and controls
106 lines (85 loc) · 3.12 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
USERS = [
{"user_id": 1, "user_name": "Alice", "age": 25, "city_identifier": 101},
{"user_id": 2, "user_name": "bob", "age": 32, "city_identifier": 102},
{"user_id": 3, "user_name": "Charlie", "age": 28, "city_identifier": 101},
{"user_id": 4, "user_name": "David", "age": 45, "city_identifier": 103},
]
CITIES_REFERENCE = {
101: "New York",
102: "London",
103: "Tokyo"
}
class ScoreService:
def __init__(self):
self._score_records = [
(1, [88, 92, 76]),
(2, [95, 89]),
(3, [100, 100, 100]),
(4, [59, 65, 71])
]
def get_scores_for_user(self, uid):
for user_id, scores in self._score_records:
if user_id == uid:
return scores
return []
def lookup_city(city_code):
return CITIES_REFERENCE.get(city_code, "Unknown")
def analyze_user_profiles(csv_file_path=None):
import csv
import io
score_service_conn = ScoreService()
processed_data = []
if csv_file_path:
users_data = []
scores_data = {}
if isinstance(csv_file_path, str):
with open(csv_file_path, 'r', encoding='utf-8') as file:
csv_content = file.read()
else:
csv_content = csv_file_path.decode('utf-8')
csv_reader = csv.DictReader(io.StringIO(csv_content))
for row in csv_reader:
user_data = {
'user_id': int(row['user_id']),
'user_name': row['user_name'],
'age': int(row['age']),
'city_identifier': int(row['city_identifier'])
}
users_data.append(user_data)
scores_str = row['scores'].strip('"')
scores = [int(s.strip()) for s in scores_str.split(',')]
scores_data[user_data['user_id']] = scores
score_service_conn._score_records = [(uid, scores) for uid, scores in scores_data.items()]
users = users_data
else:
users = USERS
for user_profile in users:
user_id = user_profile.get("user_id")
user_name = user_profile["user_name"]
age = user_profile["age"]
city = lookup_city(user_profile["city_identifier"])
scores = score_service_conn.get_scores_for_user(user_id)
total_score = sum(scores)
average_score = 0
if scores:
average_score = total_score / len(scores)
is_eligible = False
if age <= 30 and average_score >= 90.0:
is_eligible = "YES"
else:
is_eligible = "NO"
user_info = {
"Name": user_name.title(),
"Location": city,
"avg_score": average_score,
"EligibleStatus": is_eligible
}
processed_data.append(user_info)
return processed_data
if __name__ == "__main__":
final_data = analyze_user_profiles()
print("--- User Analysis Report ---")
for record in final_data:
print(
f"User: {record['Name']} | Location: {record['Location']} | Score: {record['avg_score']:.1f} | Eligible: {record['EligibleStatus']}")