Spaces:
Sleeping
Sleeping
File size: 16,438 Bytes
678aaed a5fc2da 678aaed a5fc2da 678aaed a5fc2da | 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | import pandas as pd
from datetime import datetime, timedelta
# ---------------------------------------------------------------------------
# Cluster A: filter β sort β extract
# ---------------------------------------------------------------------------
_A1_df = pd.DataFrame({
"product": ["Widget", "Gadget", "Doohickey", "Sprocket", "Thingamajig", "Gizmo"],
"revenue": [5000, 8000, 3000, 7500, 2000, 6000],
"region": ["West", "West", "East", "West", "East", "North"],
})
_A2_df = pd.DataFrame({
"name": ["Alice", "Bob", "Carol", "Dan", "Eve", "Frank", "Grace"],
"dept": ["Eng", "Sales", "Eng", "Eng", "HR", "Eng", "Sales"],
"tenure": [5, 3, 8, 2, 4, 10, 1],
})
_A3_df = pd.DataFrame({
"order_id": ["ORD-101", "ORD-102", "ORD-103", "ORD-104", "ORD-105", "ORD-106"],
"status": ["shipped", "pending", "shipped", "shipped", "cancelled", "shipped"],
"quantity": [50, 20, 80, 30, 10, 60],
})
_A4_df = pd.DataFrame({
"student_id": ["S01", "S02", "S03", "S04", "S05", "S06", "S07"],
"grade": ["A", "B", "A", "A", "C", "A", "B"],
"gpa": [3.9, 3.2, 3.95, 3.7, 2.8, 3.85, 3.1],
})
_A5_df = pd.DataFrame({
"item_name": ["Bolts", "Nails", "Screws", "Washers", "Rivets", "Pins"],
"stock": [5, 50, 8, 3, 100, 7],
"reorder_priority": [2, 10, 3, 1, 15, 4],
})
# ---------------------------------------------------------------------------
# Cluster B: normalize β filter β extract
# ---------------------------------------------------------------------------
_B1_df = pd.DataFrame({
"user_id": ["U1", "U2", "U3", "U4", "U5", "U6"],
"email": ["Alice@Gmail.COM", "bob@yahoo.com", "Carol@GMAIL.com",
"dan@gmail.COM", "eve@outlook.com", "frank@Gmail.com"],
})
_B2_df = pd.DataFrame({
"id": [1, 2, 3, 4, 5, 6, 7],
"name": [" Alice ", " Bob", " Andrew ", "Anna ", " Carl ", " Amy", " Brian "],
})
_B3_df = pd.DataFrame({
"user_id": ["U1", "U2", "U3", "U4", "U5", "U6"],
"country_code": ["us", "UK", "Us", "ca", "US", "us"],
})
_B4_df = pd.DataFrame({
"product_id": ["P1", "P2", "P3", "P4", "P5", "P6"],
"product_name": ["widget pro", "basic gadget", "pro sprocket",
"mega pro tool", "simple bolt", "pro widget x"],
})
_B5_df = pd.DataFrame({
"contact_id": ["C1", "C2", "C3", "C4", "C5", "C6"],
"phone": ["(555) 123-4567", "555.987.6543", "(555)111-2222",
"5551234", "555-999-8888", "(555) 000 1111"],
})
# ---------------------------------------------------------------------------
# Cluster C: date delta β filter β count
# ---------------------------------------------------------------------------
_today = datetime(2026, 3, 8)
_C1_df = pd.DataFrame({
"user_id": ["U1", "U2", "U3", "U4", "U5", "U6", "U7"],
"signup_date": [
_today - timedelta(days=10),
_today - timedelta(days=45),
_today - timedelta(days=5),
_today - timedelta(days=90),
_today - timedelta(days=25),
_today - timedelta(days=3),
_today - timedelta(days=60),
],
"active": [True, True, True, False, True, True, False],
})
_C2_df = pd.DataFrame({
"order_id": ["O1", "O2", "O3", "O4", "O5", "O6"],
"order_date": [
_today - timedelta(days=2),
_today - timedelta(days=10),
_today - timedelta(days=5),
_today - timedelta(days=1),
_today - timedelta(days=14),
_today - timedelta(days=6),
],
"amount": [100, 200, 150, 50, 300, 75],
})
_C3_df = pd.DataFrame({
"emp_id": ["E1", "E2", "E3", "E4", "E5", "E6"],
"hire_date": [
_today - timedelta(days=365),
_today - timedelta(days=900),
_today - timedelta(days=200),
_today - timedelta(days=500),
_today - timedelta(days=100),
_today - timedelta(days=1500),
],
"dept": ["Eng", "Sales", "Eng", "HR", "Eng", "Sales"],
})
_C4_df = pd.DataFrame({
"event_id": ["EV1", "EV2", "EV3", "EV4", "EV5", "EV6"],
"event_date": [
_today + timedelta(days=5),
_today + timedelta(days=20),
_today + timedelta(days=10),
_today + timedelta(days=3),
_today + timedelta(days=30),
_today + timedelta(days=12),
],
"venue": ["Hall A", "Hall B", "Hall A", "Hall C", "Hall B", "Hall A"],
})
_C5_df = pd.DataFrame({
"user_id": ["U1", "U2", "U3", "U4", "U5", "U6", "U7", "U8"],
"birthdate": [
_today - timedelta(days=365 * 20),
_today - timedelta(days=365 * 30),
_today - timedelta(days=365 * 22),
_today - timedelta(days=365 * 17),
_today - timedelta(days=365 * 19),
_today - timedelta(days=365 * 25),
_today - timedelta(days=365 * 24),
_today - timedelta(days=365 * 15),
],
})
# ---------------------------------------------------------------------------
# Cluster D: groupby β aggregate β sort β slice
# ---------------------------------------------------------------------------
_D1_df = pd.DataFrame({
"region": ["West", "East", "West", "North", "East", "North", "West", "East", "South", "South"],
"revenue": [5000, 3000, 7000, 4000, 6000, 2000, 3000, 8000, 1000, 5000],
"product": ["A", "B", "C", "A", "B", "C", "A", "B", "C", "A"],
})
_D2_df = pd.DataFrame({
"customer": ["Alice", "Bob", "Alice", "Carol", "Bob", "Alice",
"Carol", "Bob", "Alice", "Bob", "Carol", "Bob",
"Alice", "Bob"],
"order_id": [f"O{i}" for i in range(1, 15)],
"amount": [100, 50, 200, 150, 75, 300, 125, 80, 90, 60, 200, 45, 110, 95],
})
_D3_df = pd.DataFrame({
"dept": ["Eng", "Sales", "Eng", "HR", "Sales", "Eng", "HR", "Sales"],
"employee": ["A", "B", "C", "D", "E", "F", "G", "H"],
"salary": [120000, 80000, 110000, 70000, 90000, 130000, 75000, 85000],
})
_D4_df = pd.DataFrame({
"category": ["Electronics", "Clothing", "Electronics", "Food",
"Clothing", "Food", "Electronics", "Books", "Books"],
"product": ["P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9"],
"units_sold": [500, 200, 300, 150, 100, 400, 250, 50, 80],
})
_D5_df = pd.DataFrame({
"venue": ["Arena", "Hall", "Arena", "Park", "Hall", "Arena", "Park", "Hall"],
"event": ["E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8"],
"attendees": [500, 200, 300, 100, 250, 400, 150, 300],
"capacity": [1000, 600, 1000, 200, 600, 1000, 200, 600],
})
# ---------------------------------------------------------------------------
# Compute expected outputs
# ---------------------------------------------------------------------------
# Cluster A
_A1_expected = _A1_df[_A1_df["region"] == "West"].sort_values("revenue", ascending=False)["product"].tolist()
_A2_expected = _A2_df[_A2_df["dept"] == "Eng"].sort_values("tenure", ascending=False)["name"].tolist()
_A3_expected = _A3_df[_A3_df["status"] == "shipped"].sort_values("quantity", ascending=False)["order_id"].tolist()
_A4_expected = _A4_df[_A4_df["grade"] == "A"].sort_values("gpa", ascending=False)["student_id"].tolist()
_A5_expected = _A5_df[_A5_df["stock"] < 10].sort_values("reorder_priority")["item_name"].tolist()
# Cluster B
_B1_expected = _B1_df.assign(email=_B1_df["email"].str.lower())\
.query("email.str.endswith('@gmail.com')")["email"].tolist()
_B2_expected = _B2_df.assign(name=_B2_df["name"].str.strip())\
.query("name.str.startswith('A')")["name"].tolist()
_B3_expected = _B3_df.assign(country_code=_B3_df["country_code"].str.upper())\
.query("country_code == 'US'")["user_id"].tolist()
_B4_expected = _B4_df.assign(product_name=_B4_df["product_name"].str.title())\
.query("product_name.str.contains('Pro')")["product_id"].tolist()
_B5_expected = _B5_df.assign(phone=_B5_df["phone"].str.replace(r"\D", "", regex=True))\
.query("phone.str.len() == 10")["phone"].tolist()
# Cluster C
_C1_expected = int(_C1_df.assign(days_since=(pd.Timestamp(_today) - _C1_df["signup_date"]).dt.days)\
.query("days_since < 30").shape[0])
_C2_expected = int(_C2_df.assign(order_age=(pd.Timestamp(_today) - _C2_df["order_date"]).dt.days)\
.query("order_age <= 7").shape[0])
_C3_expected = int(_C3_df.assign(tenure_years=(pd.Timestamp(_today) - _C3_df["hire_date"]).dt.days / 365)\
.query("tenure_years < 2").shape[0])
_C4_expected = int(_C4_df.assign(days_until=(_C4_df["event_date"] - pd.Timestamp(_today)).dt.days)\
.query("days_until <= 14").shape[0])
_C5_expected = int(_C5_df.assign(age=(pd.Timestamp(_today) - _C5_df["birthdate"]).dt.days / 365)\
.query("18 <= age <= 25").shape[0])
# Cluster D
_D1_expected = _D1_df.groupby("region")["revenue"].sum()\
.sort_values(ascending=False).head(3).index.tolist()
_D2_expected = _D2_df.groupby("customer")["order_id"].count()\
.loc[lambda x: x > 5].index.tolist()
_D3_expected = _D3_df.groupby("dept")["salary"].mean()\
.sort_values(ascending=False).index[0]
_D4_expected = _D4_df.groupby("category")["units_sold"].sum()\
.sort_values().head(2).index.tolist()
_D5_expected = _D5_df.assign(over=_D5_df["attendees"] > _D5_df["capacity"])\
.query("over").groupby("venue")["event"].count().index.tolist()
# D5 recompute: venues where total attendees > total capacity
_D5_agg = _D5_df.groupby("venue").agg({"attendees": "sum", "capacity": "first"}).reset_index()
_D5_expected = _D5_agg[_D5_agg["attendees"] > _D5_agg["capacity"]]["venue"].tolist()
# ---------------------------------------------------------------------------
# TASKS list
# ---------------------------------------------------------------------------
TASKS = [
# --- Cluster A: filter β sort β extract ---
{
"id": "A1",
"cluster": "A",
"description": "Given a sales dataframe with columns [product, revenue, region], "
"return the product names for the West region sorted by revenue descending.",
"dataframe": _A1_df,
"expected_output": _A1_expected,
},
{
"id": "A2",
"cluster": "A",
"description": "Given an employees dataframe with columns [name, dept, tenure], "
"return the names of Engineering employees sorted by tenure descending.",
"dataframe": _A2_df,
"expected_output": _A2_expected,
},
{
"id": "A3",
"cluster": "A",
"description": "Given an orders dataframe with columns [order_id, status, quantity], "
"return the order IDs for shipped orders sorted by quantity descending.",
"dataframe": _A3_df,
"expected_output": _A3_expected,
},
{
"id": "A4",
"cluster": "A",
"description": "Given a students dataframe with columns [student_id, grade, gpa], "
"return the student IDs of students with grade A sorted by GPA descending.",
"dataframe": _A4_df,
"expected_output": _A4_expected,
},
{
"id": "A5",
"cluster": "A",
"description": "Given an inventory dataframe with columns [item_name, stock, reorder_priority], "
"return item names where stock is below 10, sorted by reorder priority ascending.",
"dataframe": _A5_df,
"expected_output": _A5_expected,
},
# --- Cluster B: normalize β filter β extract ---
{
"id": "B1",
"cluster": "B",
"description": "Given a users dataframe with columns [user_id, email], "
"lowercase all emails, keep only @gmail.com addresses, return the email list.",
"dataframe": _B1_df,
"expected_output": _B1_expected,
},
{
"id": "B2",
"cluster": "B",
"description": "Given a contacts dataframe with columns [id, name], "
"strip whitespace from names, keep names starting with 'A', return the name list.",
"dataframe": _B2_df,
"expected_output": _B2_expected,
},
{
"id": "B3",
"cluster": "B",
"description": "Given a users dataframe with columns [user_id, country_code], "
"uppercase all country codes, keep only 'US', return the user_id list.",
"dataframe": _B3_df,
"expected_output": _B3_expected,
},
{
"id": "B4",
"cluster": "B",
"description": "Given a products dataframe with columns [product_id, product_name], "
"title-case all product names, keep those containing 'Pro', return the product_id list.",
"dataframe": _B4_df,
"expected_output": _B4_expected,
},
{
"id": "B5",
"cluster": "B",
"description": "Given a contacts dataframe with columns [contact_id, phone], "
"remove all non-digit characters from phone numbers, keep only 10-digit ones, return the phone list.",
"dataframe": _B5_df,
"expected_output": _B5_expected,
},
# --- Cluster C: date delta β filter β count ---
{
"id": "C1",
"cluster": "C",
"description": "Given a users dataframe with columns [user_id, signup_date, active], "
"compute days since signup (from 2026-03-08), keep users who signed up within the last 30 days, return the count.",
"dataframe": _C1_df,
"expected_output": _C1_expected,
},
{
"id": "C2",
"cluster": "C",
"description": "Given an orders dataframe with columns [order_id, order_date, amount], "
"compute order age in days (from 2026-03-08), keep orders within the last 7 days, return the count.",
"dataframe": _C2_df,
"expected_output": _C2_expected,
},
{
"id": "C3",
"cluster": "C",
"description": "Given an employees dataframe with columns [emp_id, hire_date, dept], "
"compute tenure in years (from 2026-03-08), keep employees with less than 2 years, return the count.",
"dataframe": _C3_df,
"expected_output": _C3_expected,
},
{
"id": "C4",
"cluster": "C",
"description": "Given an events dataframe with columns [event_id, event_date, venue], "
"compute days until event (from 2026-03-08), keep events within the next 14 days, return the count.",
"dataframe": _C4_df,
"expected_output": _C4_expected,
},
{
"id": "C5",
"cluster": "C",
"description": "Given a users dataframe with columns [user_id, birthdate], "
"compute age in years (from 2026-03-08), keep users aged 18 to 25 inclusive, return the count.",
"dataframe": _C5_df,
"expected_output": _C5_expected,
},
# --- Cluster D: groupby β aggregate β sort β slice ---
{
"id": "D1",
"cluster": "D",
"description": "Given a sales dataframe with columns [region, revenue, product], "
"group by region, sum revenue, return the top 3 regions by total revenue.",
"dataframe": _D1_df,
"expected_output": _D1_expected,
},
{
"id": "D2",
"cluster": "D",
"description": "Given an orders dataframe with columns [customer, order_id, amount], "
"group by customer, count orders, return customers with more than 5 orders.",
"dataframe": _D2_df,
"expected_output": _D2_expected,
},
{
"id": "D3",
"cluster": "D",
"description": "Given an employees dataframe with columns [dept, employee, salary], "
"group by department, compute average salary, return the department with the highest average.",
"dataframe": _D3_df,
"expected_output": _D3_expected,
},
{
"id": "D4",
"cluster": "D",
"description": "Given a products dataframe with columns [category, product, units_sold], "
"group by category, sum units_sold, return the bottom 2 categories by total units.",
"dataframe": _D4_df,
"expected_output": _D4_expected,
},
{
"id": "D5",
"cluster": "D",
"description": "Given an events dataframe with columns [venue, event, attendees, capacity], "
"group by venue summing attendees and taking the first capacity, "
"return venues where total attendees exceed capacity.",
"dataframe": _D5_df,
"expected_output": _D5_expected,
},
]
|