Upload CodeNet_testcases.py with huggingface_hub
Browse files- CodeNet_testcases.py +40 -0
CodeNet_testcases.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
|
| 5 |
+
def extract_test_cases(html_file):
|
| 6 |
+
"""Extracts input/output test cases from a problem HTML file."""
|
| 7 |
+
with open(html_file, "r", encoding="utf-8") as f:
|
| 8 |
+
soup = BeautifulSoup(f, "html.parser")
|
| 9 |
+
|
| 10 |
+
# Find all <pre> blocks (sample test cases)
|
| 11 |
+
pre_blocks = [pre.get_text().strip() for pre in soup.find_all("pre")]
|
| 12 |
+
|
| 13 |
+
# Assume inputs and outputs alternate
|
| 14 |
+
inputs, outputs = pre_blocks[::2], pre_blocks[1::2]
|
| 15 |
+
|
| 16 |
+
return {"input": inputs, "output": outputs}
|
| 17 |
+
|
| 18 |
+
def process_problem_descriptions(folder_path):
|
| 19 |
+
"""Processes all HTML files in a folder and generates JSON."""
|
| 20 |
+
problems = {}
|
| 21 |
+
|
| 22 |
+
for filename in os.listdir(folder_path):
|
| 23 |
+
if filename.endswith(".html"):
|
| 24 |
+
problem_id = filename.replace(".html", "") # Extract problem ID
|
| 25 |
+
file_path = os.path.join(folder_path, filename)
|
| 26 |
+
|
| 27 |
+
test_cases = extract_test_cases(file_path)
|
| 28 |
+
problems[problem_id] = {"public_tests": test_cases}
|
| 29 |
+
|
| 30 |
+
return problems
|
| 31 |
+
|
| 32 |
+
# Example usage
|
| 33 |
+
FOLDER_PATH = "path/to/your/html/files" # Change this to the actual folder
|
| 34 |
+
problems_json = process_problem_descriptions(FOLDER_PATH)
|
| 35 |
+
|
| 36 |
+
# Save to JSON file
|
| 37 |
+
with open("problems.json", "w", encoding="utf-8") as f:
|
| 38 |
+
json.dump(problems_json, f, indent=4)
|
| 39 |
+
|
| 40 |
+
print("Test cases extracted and saved to problems.json!")
|