evapilotno17 commited on
Commit
ccbe1a9
·
verified ·
1 Parent(s): ae7a494

Create codeforces.py

Browse files
Files changed (1) hide show
  1. tools/codeforces.py +128 -0
tools/codeforces.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from urllib.parse import urlencode
3
+ from typing import List, Dict, Set
4
+
5
+ BASE_URL = "https://codeforces.com/api/"
6
+
7
+
8
+ import requests
9
+ from urllib.parse import urlencode
10
+ from typing import List, Dict, Set
11
+ from smolagents.tools import tool # Ensure this is the correct import
12
+
13
+ BASE_URL = "https://codeforces.com/api/"
14
+
15
+
16
+ @tool
17
+ def get_contest_data(cid:int, show_unofficial:bool=False)-> Dict: #it's import to specify the return type
18
+ #Keep this format for the description / args / args description but feel free to modify the tool
19
+ """Fetches the contest standings data for a given contest ID
20
+ Args:
21
+ cid: The contest ID
22
+ show_unofficial: Whether to include unofficial participants
23
+ """
24
+ params = {
25
+ "contestId": str(cid),
26
+ "showUnofficial": str(show_unofficial).lower(),
27
+ }
28
+ response = requests.get(f"{BASE_URL}contest.standings?{urlencode(params)}").json()
29
+ return response.get("result", {})
30
+
31
+
32
+ @tool
33
+ def get_rating_data(handle:str)-> List[Dict]: #it's import to specify the return type
34
+ #Keep this format for the description / args / args description but feel free to modify the tool
35
+ """Fetches rating change history for a given user handle
36
+ Args:
37
+ handle: The Codeforces user handle
38
+ """
39
+ response = requests.get(f"{BASE_URL}user.rating?handle={handle}").json()
40
+ return response.get("result", [])
41
+
42
+
43
+ @tool
44
+ def count_of_rated_matches(handle:str)-> int: #it's import to specify the return type
45
+ #Keep this format for the description / args / args description but feel free to modify the tool
46
+ """Counts the number of rated contests a user has participated in
47
+ Args:
48
+ handle: The Codeforces user handle
49
+ """
50
+ return len(get_rating_data(handle))
51
+
52
+
53
+ @tool
54
+ def get_user_info(handle:str)-> Dict: #it's import to specify the return type
55
+ #Keep this format for the description / args / args description but feel free to modify the tool
56
+ """Fetches user information from Codeforces
57
+ Args:
58
+ handle: The Codeforces user handle
59
+ """
60
+ response = requests.get(f"{BASE_URL}user.info?handles={handle}").json()
61
+ return response.get("result", [{}])[0]
62
+
63
+
64
+ @tool
65
+ def get_handle(standings_obj:Dict)-> str: #it's import to specify the return type
66
+ #Keep this format for the description / args / args description but feel free to modify the tool
67
+ """Extracts the handle from a standings object
68
+ Args:
69
+ standings_obj: A dictionary representing a user's contest standings
70
+ """
71
+ return standings_obj["party"]["members"][0]["handle"]
72
+
73
+
74
+ @tool
75
+ def solve_cutoff(cid:int, show_unofficial:bool, L:int, R:int)-> List[str]: #it's import to specify the return type
76
+ #Keep this format for the description / args / args description but feel free to modify the tool
77
+ """Returns all handles that solved between L and R number of problems in a contest
78
+ Args:
79
+ cid: The contest ID
80
+ show_unofficial: Whether to include unofficial participants
81
+ L: The minimum number of problems solved
82
+ R: The maximum number of problems solved
83
+ """
84
+ standings = get_contest_data(cid, show_unofficial)
85
+ return [
86
+ get_handle(entry)
87
+ for entry in standings.get("rows", [])
88
+ if L <= entry.get("points", 0) <= R
89
+ ]
90
+
91
+
92
+ @tool
93
+ def get_all_rated_users()-> List[List]: #it's import to specify the return type
94
+ #Keep this format for the description / args / args description but feel free to modify the tool
95
+ """Fetches all rated users with their highest rating
96
+ """
97
+ response = requests.get(
98
+ f"{BASE_URL}user.ratedList?activeOnly=true&includeRetired=false"
99
+ ).json()
100
+ return [[user["handle"], user["maxRating"]] for user in response.get("result", [])]
101
+
102
+
103
+ @tool
104
+ def filter_rated_users(L:int, R:int)-> List[str]: #it's import to specify the return type
105
+ #Keep this format for the description / args / args description but feel free to modify the tool
106
+ """Filters users who have a max rating within a given range
107
+ Args:
108
+ L: The minimum rating
109
+ R: The maximum rating
110
+ """
111
+ all_handles = get_all_rated_users()
112
+ return [handle for handle, max_rating in all_handles if L <= max_rating <= R]
113
+
114
+
115
+ @tool
116
+ def intersect(sets:List[List[str]])-> List[str]: #it's import to specify the return type
117
+ #Keep this format for the description / args / args description but feel free to modify the tool
118
+ """Finds the intersection of multiple lists of handles
119
+ Args:
120
+ sets: A list of lists containing handles
121
+ """
122
+ sorted_lists = sorted(sets, key=len)
123
+ result = set(sorted_lists[0]) if sorted_lists else set()
124
+
125
+ for s in sorted_lists:
126
+ result &= set(s)
127
+
128
+ return list(result)