Spaces:
Build error
Build error
File size: 8,087 Bytes
ccbe1a9 ac5aeb2 a1bd46a ac5aeb2 | 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | import requests
from urllib.parse import urlencode
from typing import List, Dict, Set
BASE_URL = "https://codeforces.com/api/"
import requests
from urllib.parse import urlencode
from typing import List, Dict, Set
from smolagents.tools import tool # Ensure this is the correct import
BASE_URL = "https://codeforces.com/api/"
@tool
def get_contest_data(cid:int, show_unofficial:bool=False)-> Dict: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Fetches the contest standings data for a given contest ID
Args:
cid: The contest ID
show_unofficial: Whether to include unofficial participants
"""
params = {
"contestId": str(cid),
"showUnofficial": str(show_unofficial).lower(),
}
response = requests.get(f"{BASE_URL}contest.standings?{urlencode(params)}").json()
return response.get("result", {})
@tool
def get_rating_data(handle:str)-> List[Dict]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Fetches rating change history for a given user handle
Args:
handle: The Codeforces user handle
"""
response = requests.get(f"{BASE_URL}user.rating?handle={handle}").json()
return response.get("result", [])
@tool
def count_of_rated_matches(handle:str)-> int: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Counts the number of rated contests a user has participated in
Args:
handle: The Codeforces user handle
"""
return len(get_rating_data(handle))
@tool
def get_user_info(handle:str)-> Dict: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Fetches user information from Codeforces
Args:
handle: The Codeforces user handle
"""
response = requests.get(f"{BASE_URL}user.info?handles={handle}").json()
return response.get("result", [{}])[0]
@tool
def get_handle(standings_obj:Dict)-> str: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Extracts the handle from a standings object
Args:
standings_obj: A dictionary representing a user's contest standings
"""
return standings_obj["party"]["members"][0]["handle"]
@tool
def solve_cutoff(cid:int, show_unofficial:bool, L:int, R:int)-> List[str]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Returns all handles that solved between L and R number of problems in a contest
Args:
cid: The contest ID
show_unofficial: Whether to include unofficial participants
L: The minimum number of problems solved
R: The maximum number of problems solved
"""
standings = get_contest_data(cid, show_unofficial)
return [
get_handle(entry)
for entry in standings.get("rows", [])
if L <= entry.get("points", 0) <= R
]
@tool
def get_all_rated_users()-> List[List]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Fetches all rated users with their highest rating
"""
response = requests.get(
f"{BASE_URL}user.ratedList?activeOnly=true&includeRetired=false"
).json()
return [[user["handle"], user["maxRating"]] for user in response.get("result", [])]
@tool
def filter_rated_users(L:int, R:int)-> List[str]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Filters users who have a max rating within a given range
Args:
L: The minimum rating
R: The maximum rating
"""
all_handles = get_all_rated_users()
return [handle for handle, max_rating in all_handles if L <= max_rating <= R]
@tool
def intersect(sets:List[List[str]])-> List[str]: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""Finds the intersection of multiple lists of handles
Args:
sets: A list of lists containing handles
"""
sorted_lists = sorted(sets, key=len)
result = set(sorted_lists[0]) if sorted_lists else set()
for s in sorted_lists:
result &= set(s)
return list(result)
from typing import Dict
import requests
# Assume BASE_URL is defined elsewhere; for example:
BASE_URL = "https://codeforces.com/api/"
@tool
def get_user_info(handle: str) -> Dict:
"""Fetches user information from Codeforces
Args:
handle: The Codeforces user handle
"""
response = requests.get(f"{BASE_URL}user.info?handles={handle}").json()
return response.get("result", [{}])[0]
@tool
def get_contribution(handle: str) -> int:
"""Fetches the contribution of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("contribution", 0)
@tool
def get_lastOnlineTimeSeconds(handle: str) -> int:
"""Fetches the last online time (in seconds) of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("lastOnlineTimeSeconds", 0)
@tool
def get_organization(handle: str) -> str:
"""Fetches the organization of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("organization", "")
@tool
def get_rating(handle: str) -> int:
"""Fetches the current rating of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("rating", 0)
@tool
def get_friendOfCount(handle: str) -> int:
"""Fetches the friend count of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("friendOfCount", 0)
@tool
def get_titlePhoto(handle: str) -> str:
"""Fetches the title photo URL of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("titlePhoto", "")
@tool
def get_rank(handle: str) -> str:
"""Fetches the rank of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("rank", "")
@tool
def get_handle(handle: str) -> str:
"""Fetches the handle (username) of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("handle", "")
@tool
def get_maxRating(handle: str) -> int:
"""Fetches the maximum rating of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("maxRating", 0)
@tool
def get_avatar(handle: str) -> str:
"""Fetches the avatar URL of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("avatar", "")
@tool
def get_registrationTimeSeconds(handle: str) -> int:
"""Fetches the registration time (in seconds) of a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("registrationTimeSeconds", 0)
@tool
def get_maxRank(handle: str) -> str:
"""Fetches the maximum rank achieved by a Codeforces user.
Args:
handle: The Codeforces user handle
"""
user_info = get_user_info(handle)
return user_info.get("maxRank", "")
|