注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
本帖最后由 微信用户_ky8ei 于 2025-4-19 11:12 编辑
他们的OA是http call一个endpoint,返回数据,根据页面提供的逻辑写代码生成数据,在调用一ort json
- import requests
- def check_permission(user_id, owner_id, permission_level, users_with_id_key):
- if permission_level == "ALL":
- return True
- elif permission_level == "OWNED_OR_TEAM":
- if user_id == owner_id:
- return True
- else:
- user_teams = users_with_id_key[user_id]["teamIds"]
- owner_teams = users_with_id_key[owner_id]["teamIds"]
- if any([user_team in owner_teams for user_team in user_teams]):
- return True
- elif permission_level == "OWNED_ONLY":
- if user_id == owner_id:
- return True
- return False
- def process_data(data):
- result = []
- users_with_id_key = {item["userId"]: item for item in data["users"]}
- for user in data["users"]:
- user_id = user["userId"]
- viewPermissionLevel = user["viewPermissionLevel"]
- editPermissionLevel = user["editPermissionLevel"]
- user_data_to_append = {
- "userId": user["userId"],
- "viewableDealIds": [],
- "editableDealIds": []
- }
- for deal in data["deals"]:
- deal_id = deal["dealId"]
- owner_id = deal["ownerUserId"]
- if check_permission(user_id, owner_id, viewPermissionLevel, users_with_id_key):
- user_data_to_append["viewableDealIds"].append(deal_id)
- if check_permission(user_id, owner_id, editPermissionLevel, users_with_id_key):
- user_data_to_append["editableDealIds"].append(deal_id)
- result.append(user_data_to_append)
- return result
- def get_data(url):
- response = requests.get(url)
- return response.json()
- def send_data(url, data):
- response = requests.post(url, json=data)
- print(f"Response Status Code: {response.status_code}")
- print(f"Response Status Text: {response.text}")
- if __name__ == '__main__':
- raw_data = """
- {
- "users": [{
- "userId": 1,
- "teamIds": [5],
- "viewPermissionLevel": "OWNED_OR_TEAM",
- "editPermissionLevel": "OWNED_ONLY"
- }, {
- "userId": 2,
- "teamIds": [5],
- "viewPermissionLevel": "ALL",
- "editPermissionLevel": "OWNED_ONLY"
- }, {
- "userId": 3,
- "teamIds": [],
- "viewPermissionLevel": "OWNED_OR_TEAM",
- "editPermissionLevel": "ALL"
- }],
- "deals": [{
- "dealId": 10,
- "ownerUserId": 1
- }, {
- "dealId": 20,
- "ownerUserId": 2
- }, {
- "dealId": 30,
- "ownerUserId": 3
- }]
- }
-
- """
- example_output = """
- {
- "results": [{
- "userId": 1,
- "viewableDealIds": [10, 20],
- "editableDealIds": [10]
- }, {
- "userId": 2,
- "viewableDealIds": [10, 20, 30],
- "editableDealIds": [20]
- }, {
- "userId": 3,
- "viewableDealIds": [10, 20, 30],
- "editableDealIds": [10, 20, 30]
- }]
- }
-
- """
- data = json.loads(raw_data)
- result = process_data(data)
- print(json.dumps(result, indent=4))
复制代码 二、电话峰值题- <div>
- </div><div>"""
- <a href="https://leetcode.com/discuss/post/5833936/hubspot-interview-guidance-by-anonymous_-3xsp/" target="_blank">https://leetcode.com/discuss/pos...by-anonymous_-3xsp/</a>
- """
- import json
- from datetime import datetime, timedelta
- import requests
- def get_dates_between(start_timestamp, end_timestamp):
- start_date = datetime.fromtimestamp(start_timestamp / 1000).date()
- end_date = datetime.fromtimestamp((end_timestamp-1) / 1000).date()
- current_date = start_date
- dates = []
- while current_date <= end_date:
- dates.append(current_date.strftime('%Y-%m-%d'))
- current_date += timedelta(days=1)
- return dates
- def process_data(data):
- data_by_customer_id_and_date = {}
- for call in data:
- customer_id = call["customerId"]
- start_timestamp = call["startTimestamp"]
- end_timestamp = call["endTimestamp"]
- dates = get_dates_between(start_timestamp, end_timestamp)
- if customer_id not in data_by_customer_id_and_date:
- data_by_customer_id_and_date[customer_id] = {}
- for date in dates:
- if date not in data_by_customer_id_and_date[customer_id]:
- data_by_customer_id_and_date[customer_id][date] = []
- data_by_customer_id_and_date[customer_id][date].append(call)
- return data_by_customer_id_and_date
- def process_customer_for_a_day(call_in_a_day, date):
- ts_for_the_day = int(datetime.strptime(date, '%Y-%m-%d').timestamp() * 1000)
- sweep_lines = []
- for call in call_in_a_day:
- call_id = call["callId"]
- start_ts = call["startTimestamp"]
- end_ts = call["endTimestamp"]
- sweep_lines.append([start_ts, 1, call_id])
- sweep_lines.append([end_ts, -1, call_id])
- sweep_lines_sorted = sorted(sweep_lines, key=lambda x: [x[0], x[1]])
- print(sweep_lines_sorted)
- result = {
- "customerId": call_in_a_day[0]["customerId"],
- "date": date,
- "maxConcurrentCalls": 0,
- "timestamp": ts_for_the_day,
- "callIds": [
- ]
- }
- concurrent_calls = set()
- for d in sweep_lines_sorted:
- ts = d[0]
- start_or_end = d[1]
- call_id = d[2]
- if start_or_end > 0:
- concurrent_calls.add(call_id)
- if len(concurrent_calls) > result["maxConcurrentCalls"]:
- # find a new peak
- result["maxConcurrentCalls"] = len(concurrent_calls)
- result["callIds"] = list(concurrent_calls)
- if ts > ts_for_the_day:
- result["timestamp"] = ts
- else:
- concurrent_calls.remove(call_id)
- return result
- def process_all_customer_for_all_day(data):
- data_by_customer_id_and_date = process_data(data)
- result = []
- for customer_id in data_by_customer_id_and_date:
- for date in data_by_customer_id_and_date[customer_id]:
- result.append(process_customer_for_a_day(data_by_customer_id_and_date[customer_id][date], date))
- return result
- def get_data(url):
- response = requests.get(url)
- return response.json()
- def send_data(url, data):
- response = requests.post(url, json=data)
- print(f"Response Status Code: {response.status_code}")
- print(f"Response Status Text: {response.text}")
- if __name__ == '__main__':
- raw_data = """
- [
- {
- "customerId": 123,
- "callId": "03",
- "startTimestamp": 1741755500000,
- "endTimestamp": 1741755600000
- },
- {
- "customerId": 123,
- "callId": "02",
- "startTimestamp": 1741564800000,
- "endTimestamp": 1741755600000
- },
- {
- "customerId": 123,
- "callId": "01",
- "startTimestamp": 1741669200000,
- "endTimestamp": 1741672800000
- },
- {
- "customerId": 123,
- "callId": "0",
- "startTimestamp": 1741564800000,
- "endTimestamp": 1741651200000
- },
- {
- "customerId": 123,
- "callId": "1",
- "startTimestamp": 1741582800000,
- "endTimestamp": 1741586400000
- },
- {
- "customerId": 123,
- "callId": "2",
- "startTimestamp": 1741586400000,
- "endTimestamp": 1741590000000
- },
- {
- "customerId": 123,
- "callId": "3",
- "startTimestamp": 1741582800000,
- "endTimestamp": 1741590000000
- },
- {
- "customerId": 123,
- "callId": "4",
- "startTimestamp": 1741564800000,
- "endTimestamp": 1741755600000
- },
- {
- "customerId": 111,
- "callId": "111-1",
- "startTimestamp": 1741564800000,
- "endTimestamp": 1741651200001
- }
- ]
- """
- # 1741564800000 3-10 0am
- # 1741582800000 3-10 5am
- # 1741586400000 3-10 6am
- # 1741590000000 3-10 7am
- # 1741651200000 3-11 0am
- # 1741669200000 3-11 5am
- # 1741672800000 3-11 6am
- # 1741676400000 3-11 7am
- # 1741737600000 3-12 0am
- # 1741755600000 3-12 5am
- json_data = json.loads(raw_data)
- ret = process_all_customer_for_all_day(json_data)
- print(json.dumps(ret, indent=4))
- </div>
复制代码 |