A Python utility that automatically generates C code solutions for programming problems, compiles them, and creates a comprehensive PDF report.
This tool takes a list of programming problems from a text file, uses AI to generate C code solutions, compiles and runs the code locally, and produces a professional PDF document containing all problems, solutions, and execution outputs.
- AI-Powered Code Generation: Utilizes the G4F client API to generate C code solutions
- Local Compilation & Testing: Automatically compiles and runs the generated code
- Input Simulation: Intelligently detects and simulates user input requirements
- Parallel Processing: Processes multiple problems simultaneously for efficiency
- Comprehensive PDF Reports: Creates well-formatted PDF documentation with all solutions
- Error Handling: Gracefully handles compilation errors and execution timeouts
- Python 3.6+
- GCC compiler (accessible via PATH)
- Required Python packages:
- fpdf
- g4f
- Clone this repository or download the script
- Install required Python packages:
pip install fpdf g4f
- Ensure GCC is installed and accessible from your command line
- Create a
questions.txt
file with one programming problem per line - Run the script:
python3 main.py
- A PDF file named
code_solutions.pdf
will be generated with all solutions
You can adjust the following parameters at the top of the script:
QUESTIONS_FILE
: Path to the file containing programming problems (default: 'questions.txt')OUTPUT_FOLDER
: Directory for storing temporary outputs (default: 'output')FINAL_PDF
: Name of the output PDF file (default: 'code_solutions.pdf')MAX_WORKERS
: Maximum number of parallel processes (default: 4)
- The script reads programming problems from the
questions.txt
file - For each problem, it:
- Sends the problem to an AI model via G4F API to generate C code
- Compiles the generated code using GCC
- Detects input requirements from scanf statements
- Generates appropriate random inputs when needed
- Captures the execution output
- All problems, code solutions, and outputs are compiled into a PDF document
- The PDF includes proper formatting with:
- Problem statements
- C code solutions with syntax formatting
- Execution outputs
- Input/output simulation details
Input file (questions.txt
):
Write a program to find the factorial of a number.
Create a program that checks if a number is prime.
Output: A PDF file containing each problem, its C code solution, and execution output.
- Student information can be modified in the
get_c_code_from_g4f
function - PDF formatting can be adjusted in the
ProblemPDF
class - Input generation can be customized in the
generate_random_input
function
- If compilation fails, check that GCC is properly installed
- If the AI fails to generate code, try adjusting the prompt in
get_c_code_from_g4f
- For timeout errors, increase the timeout values in the
run_code_locally
function
- The program automatically adds student identification information (name, roll number, section)
- Unicode characters in code or output are cleaned and converted to ASCII equivalents
- The tool handles timeouts for both compilation (10 seconds) and execution (5 seconds)