Week 4 : Using LLMs to translate Python Code to C++ compiled code

This commit is contained in:
The Top Dev
2025-10-23 16:56:58 +03:00
parent 0ceb9136d5
commit 2c6af4a783
5 changed files with 2264 additions and 0 deletions

View File

@@ -0,0 +1,190 @@
"""
Simple calculator class with history tracking.
"""
import math
from typing import List, Union
class Calculator:
"""A simple calculator with history tracking."""
def __init__(self):
"""Initialize calculator with empty history."""
self.history: List[str] = []
self.memory: float = 0.0
def add(self, a: float, b: float) -> float:
"""Add two numbers."""
result = a + b
self.history.append(f"{a} + {b} = {result}")
return result
def subtract(self, a: float, b: float) -> float:
"""Subtract b from a."""
result = a - b
self.history.append(f"{a} - {b} = {result}")
return result
def multiply(self, a: float, b: float) -> float:
"""Multiply two numbers."""
result = a * b
self.history.append(f"{a} * {b} = {result}")
return result
def divide(self, a: float, b: float) -> float:
"""Divide a by b."""
if b == 0:
raise ValueError("Cannot divide by zero")
result = a / b
self.history.append(f"{a} / {b} = {result}")
return result
def power(self, base: float, exponent: float) -> float:
"""Calculate base raised to the power of exponent."""
result = base ** exponent
self.history.append(f"{base} ^ {exponent} = {result}")
return result
def square_root(self, number: float) -> float:
"""Calculate square root of a number."""
if number < 0:
raise ValueError("Cannot calculate square root of negative number")
result = math.sqrt(number)
self.history.append(f"{number} = {result}")
return result
def factorial(self, n: int) -> int:
"""Calculate factorial of n."""
if n < 0:
raise ValueError("Factorial is not defined for negative numbers")
if n == 0 or n == 1:
return 1
result = 1
for i in range(2, n + 1):
result *= i
self.history.append(f"{n}! = {result}")
return result
def memory_store(self, value: float) -> None:
"""Store value in memory."""
self.memory = value
self.history.append(f"Memory stored: {value}")
def memory_recall(self) -> float:
"""Recall value from memory."""
self.history.append(f"Memory recalled: {self.memory}")
return self.memory
def memory_clear(self) -> None:
"""Clear memory."""
self.memory = 0.0
self.history.append("Memory cleared")
def get_history(self) -> List[str]:
"""Get calculation history."""
return self.history.copy()
def clear_history(self) -> None:
"""Clear calculation history."""
self.history.clear()
def get_last_result(self) -> Union[float, None]:
"""Get the result of the last calculation."""
if not self.history:
return None
last_entry = self.history[-1]
# Extract result from history entry
if "=" in last_entry:
return float(last_entry.split("=")[-1].strip())
return None
class ScientificCalculator(Calculator):
"""Extended calculator with scientific functions."""
def sine(self, angle: float) -> float:
"""Calculate sine of angle in radians."""
result = math.sin(angle)
self.history.append(f"sin({angle}) = {result}")
return result
def cosine(self, angle: float) -> float:
"""Calculate cosine of angle in radians."""
result = math.cos(angle)
self.history.append(f"cos({angle}) = {result}")
return result
def tangent(self, angle: float) -> float:
"""Calculate tangent of angle in radians."""
result = math.tan(angle)
self.history.append(f"tan({angle}) = {result}")
return result
def logarithm(self, number: float, base: float = math.e) -> float:
"""Calculate logarithm of number with given base."""
if number <= 0:
raise ValueError("Logarithm is not defined for non-positive numbers")
if base <= 0 or base == 1:
raise ValueError("Logarithm base must be positive and not equal to 1")
result = math.log(number, base)
self.history.append(f"log_{base}({number}) = {result}")
return result
def degrees_to_radians(self, degrees: float) -> float:
"""Convert degrees to radians."""
return degrees * math.pi / 180
def radians_to_degrees(self, radians: float) -> float:
"""Convert radians to degrees."""
return radians * 180 / math.pi
def main():
"""Main function to demonstrate calculator functionality."""
print("Calculator Demo")
print("=" * 30)
# Basic calculator
calc = Calculator()
print("Basic Calculator Operations:")
print(f"5 + 3 = {calc.add(5, 3)}")
print(f"10 - 4 = {calc.subtract(10, 4)}")
print(f"6 * 7 = {calc.multiply(6, 7)}")
print(f"15 / 3 = {calc.divide(15, 3)}")
print(f"2 ^ 8 = {calc.power(2, 8)}")
print(f"√64 = {calc.square_root(64)}")
print(f"5! = {calc.factorial(5)}")
print(f"\nCalculation History:")
for entry in calc.get_history():
print(f" {entry}")
# Scientific calculator
print("\n" + "=" * 30)
print("Scientific Calculator Operations:")
sci_calc = ScientificCalculator()
# Convert degrees to radians for trigonometric functions
angle_deg = 45
angle_rad = sci_calc.degrees_to_radians(angle_deg)
print(f"sin({angle_deg}°) = {sci_calc.sine(angle_rad):.4f}")
print(f"cos({angle_deg}°) = {sci_calc.cosine(angle_rad):.4f}")
print(f"tan({angle_deg}°) = {sci_calc.tangent(angle_rad):.4f}")
print(f"ln(10) = {sci_calc.logarithm(10):.4f}")
print(f"log₁₀(100) = {sci_calc.logarithm(100, 10):.4f}")
print(f"\nScientific Calculator History:")
for entry in sci_calc.get_history():
print(f" {entry}")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,64 @@
"""
Fibonacci sequence implementation in Python.
"""
def fibonacci(n):
"""Calculate the nth Fibonacci number using recursion."""
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
def fibonacci_iterative(n):
"""Calculate the nth Fibonacci number using iteration."""
if n <= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
def fibonacci_sequence(count):
"""Generate a sequence of Fibonacci numbers."""
sequence = []
for i in range(count):
sequence.append(fibonacci(i))
return sequence
def main():
"""Main function to demonstrate Fibonacci calculations."""
print("Fibonacci Sequence Demo")
print("=" * 30)
# Calculate first 10 Fibonacci numbers
for i in range(10):
result = fibonacci(i)
print(f"fibonacci({i}) = {result}")
print("\nFirst 15 Fibonacci numbers:")
sequence = fibonacci_sequence(15)
print(sequence)
# Performance comparison
import time
n = 30
print(f"\nPerformance comparison for fibonacci({n}):")
start_time = time.time()
recursive_result = fibonacci(n)
recursive_time = time.time() - start_time
start_time = time.time()
iterative_result = fibonacci_iterative(n)
iterative_time = time.time() - start_time
print(f"Recursive: {recursive_result} (took {recursive_time:.4f}s)")
print(f"Iterative: {iterative_result} (took {iterative_time:.4f}s)")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,150 @@
"""
Various sorting algorithms implemented in Python.
"""
import random
import time
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
"""Sort array using bubble sort algorithm."""
n = len(arr)
arr = arr.copy() # Don't modify original array
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
def selection_sort(arr: List[int]) -> List[int]:
"""Sort array using selection sort algorithm."""
n = len(arr)
arr = arr.copy()
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
def insertion_sort(arr: List[int]) -> List[int]:
"""Sort array using insertion sort algorithm."""
arr = arr.copy()
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
def quick_sort(arr: List[int]) -> List[int]:
"""Sort array using quick sort algorithm."""
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
def merge_sort(arr: List[int]) -> List[int]:
"""Sort array using merge sort algorithm."""
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left: List[int], right: List[int]) -> List[int]:
"""Merge two sorted arrays."""
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
def benchmark_sorting_algorithms():
"""Benchmark different sorting algorithms."""
sizes = [100, 500, 1000, 2000]
algorithms = {
"Bubble Sort": bubble_sort,
"Selection Sort": selection_sort,
"Insertion Sort": insertion_sort,
"Quick Sort": quick_sort,
"Merge Sort": merge_sort
}
print("Sorting Algorithm Benchmark")
print("=" * 50)
for size in sizes:
print(f"\nArray size: {size}")
print("-" * 30)
# Generate random array
test_array = [random.randint(1, 1000) for _ in range(size)]
for name, algorithm in algorithms.items():
start_time = time.time()
sorted_array = algorithm(test_array)
end_time = time.time()
# Verify sorting is correct
is_sorted = all(sorted_array[i] <= sorted_array[i+1] for i in range(len(sorted_array)-1))
print(f"{name:15}: {end_time - start_time:.4f}s {'' if is_sorted else ''}")
def main():
"""Main function to demonstrate sorting algorithms."""
print("Sorting Algorithms Demo")
print("=" * 30)
# Test with small array
test_array = [64, 34, 25, 12, 22, 11, 90]
print(f"Original array: {test_array}")
algorithms = {
"Bubble Sort": bubble_sort,
"Selection Sort": selection_sort,
"Insertion Sort": insertion_sort,
"Quick Sort": quick_sort,
"Merge Sort": merge_sort
}
for name, algorithm in algorithms.items():
sorted_array = algorithm(test_array)
print(f"{name}: {sorted_array}")
# Run benchmark
print("\n" + "=" * 50)
benchmark_sorting_algorithms()
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,580 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python to C++ Code Translator using LLMs\n",
"\n",
"This notebook translates Python code to compilable C++ using GPT, Gemini, or Claude.\n",
"\n",
"## Features:\n",
"- 🤖 Multiple LLM support (GPT, Gemini, Claude)\n",
"- ✅ Automatic compilation testing with g++\n",
"- 🔄 Comparison mode to test all LLMs\n",
"- 💬 Interactive translation mode"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Install Required Packages\n",
"\n",
"Run this cell first to install all dependencies:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[2mResolved \u001b[1m267 packages\u001b[0m \u001b[2min 10ms\u001b[0m\u001b[0m\n",
"\u001b[2mAudited \u001b[1m243 packages\u001b[0m \u001b[2min 467ms\u001b[0m\u001b[0m\n"
]
}
],
"source": [
"!uv add openai anthropic python-dotenv google-generativeai\n",
"#!pip install openai anthropic python-dotenv google-generativeai"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Import Libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import subprocess\n",
"import tempfile\n",
"from pathlib import Path\n",
"from dotenv import load_dotenv\n",
"import openai\n",
"from anthropic import Anthropic\n",
"import google.generativeai as genai"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Load API Keys\n",
"\n",
"Make sure you have a `.env` file with:\n",
"```\n",
"OPENAI_API_KEY=your_key_here\n",
"GEMINI_API_KEY=your_key_here\n",
"ANTHROPIC_API_KEY=your_key_here\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load API keys from .env file\n",
"load_dotenv()\n",
"\n",
"# Initialize API clients\n",
"openai_client = openai.OpenAI(api_key=os.getenv('OPENAI_API_KEY'))\n",
"anthropic_client = Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))\n",
"genai.configure(api_key=os.getenv('GEMINI_API_KEY'))\n",
"\n",
"print(\"✓ API keys loaded successfully\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Define System Prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"SYSTEM_PROMPT = \"\"\"You are an expert programmer that translates Python code to C++.\n",
"Translate the given Python code to efficient, compilable C++ code.\n",
"\n",
"Requirements:\n",
"- The C++ code must compile without errors\n",
"- Include all necessary headers\n",
"- Use modern C++ (C++11 or later) features where appropriate\n",
"- Add proper error handling\n",
"- Maintain the same functionality as the Python code\n",
"- Include a main() function if the Python code has executable statements\n",
"\n",
"Only return the C++ code, no explanations unless there are important notes about compilation.\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: LLM Translation Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def translate_with_gpt(python_code, model=\"gpt-4o\"):\n",
" \"\"\"Translate Python to C++ using OpenAI's GPT models\"\"\"\n",
" try:\n",
" response = openai_client.chat.completions.create(\n",
" model=model,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
" {\"role\": \"user\", \"content\": f\"Translate this Python code to C++:\\n\\n{python_code}\"}\n",
" ],\n",
" temperature=0.2\n",
" )\n",
" return response.choices[0].message.content\n",
" except Exception as e:\n",
" return f\"Error with GPT: {str(e)}\"\n",
"\n",
"def translate_with_gemini(python_code, model=\"gemini-2.0-flash-exp\"):\n",
" \"\"\"Translate Python to C++ using Google's Gemini\"\"\"\n",
" try:\n",
" model_instance = genai.GenerativeModel(model)\n",
" prompt = f\"{SYSTEM_PROMPT}\\n\\nTranslate this Python code to C++:\\n\\n{python_code}\"\n",
" response = model_instance.generate_content(prompt)\n",
" return response.text\n",
" except Exception as e:\n",
" return f\"Error with Gemini: {str(e)}\"\n",
"\n",
"def translate_with_claude(python_code, model=\"claude-sonnet-4-20250514\"):\n",
" \"\"\"Translate Python to C++ using Anthropic's Claude\"\"\"\n",
" try:\n",
" response = anthropic_client.messages.create(\n",
" model=model,\n",
" max_tokens=4096,\n",
" temperature=0.2,\n",
" system=SYSTEM_PROMPT,\n",
" messages=[\n",
" {\"role\": \"user\", \"content\": f\"Translate this Python code to C++:\\n\\n{python_code}\"}\n",
" ]\n",
" )\n",
" return response.content[0].text\n",
" except Exception as e:\n",
" return f\"Error with Claude: {str(e)}\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Main Translation Function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def translate_python_to_cpp(python_code, llm=\"gpt\", model=None):\n",
" \"\"\"\n",
" Translate Python code to C++ using specified LLM\n",
" \n",
" Args:\n",
" python_code (str): Python code to translate\n",
" llm (str): LLM to use ('gpt', 'gemini', or 'claude')\n",
" model (str): Specific model version (optional)\n",
" \n",
" Returns:\n",
" str: Translated C++ code\n",
" \"\"\"\n",
" print(f\"🔄 Translating with {llm.upper()}...\")\n",
" \n",
" if llm.lower() == \"gpt\":\n",
" model = model or \"gpt-4o\"\n",
" cpp_code = translate_with_gpt(python_code, model)\n",
" elif llm.lower() == \"gemini\":\n",
" model = model or \"gemini-2.0-flash-exp\"\n",
" cpp_code = translate_with_gemini(python_code, model)\n",
" elif llm.lower() == \"claude\":\n",
" model = model or \"claude-sonnet-4-20250514\"\n",
" cpp_code = translate_with_claude(python_code, model)\n",
" else:\n",
" return \"Error: Invalid LLM. Choose 'gpt', 'gemini', or 'claude'\"\n",
" \n",
" return cpp_code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Compilation Testing Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def extract_cpp_code(text):\n",
" \"\"\"Extract C++ code from markdown code blocks if present\"\"\"\n",
" if \"```cpp\" in text:\n",
" start = text.find(\"```cpp\") + 6\n",
" end = text.find(\"```\", start)\n",
" return text[start:end].strip()\n",
" elif \"```c++\" in text:\n",
" start = text.find(\"```c++\") + 6\n",
" end = text.find(\"```\", start)\n",
" return text[start:end].strip()\n",
" elif \"```\" in text:\n",
" start = text.find(\"```\") + 3\n",
" end = text.find(\"```\", start)\n",
" return text[start:end].strip()\n",
" return text.strip()\n",
"\n",
"def compile_cpp_code(cpp_code, output_name=\"translated_program\"):\n",
" \"\"\"\n",
" Compile C++ code and return compilation status\n",
" \n",
" Args:\n",
" cpp_code (str): C++ code to compile\n",
" output_name (str): Name of output executable\n",
" \n",
" Returns:\n",
" dict: Compilation result with status and messages\n",
" \"\"\"\n",
" # Extract code from markdown if present\n",
" cpp_code = extract_cpp_code(cpp_code)\n",
" \n",
" # Create temporary directory\n",
" with tempfile.TemporaryDirectory() as tmpdir:\n",
" cpp_file = Path(tmpdir) / \"program.cpp\"\n",
" exe_file = Path(tmpdir) / output_name\n",
" \n",
" # Write C++ code to file\n",
" with open(cpp_file, 'w') as f:\n",
" f.write(cpp_code)\n",
" \n",
" # Try to compile\n",
" try:\n",
" result = subprocess.run(\n",
" ['g++', '-std=c++17', str(cpp_file), '-o', str(exe_file)],\n",
" capture_output=True,\n",
" text=True,\n",
" timeout=10\n",
" )\n",
" \n",
" if result.returncode == 0:\n",
" return {\n",
" 'success': True,\n",
" 'message': '✓ Compilation successful!',\n",
" 'executable': str(exe_file),\n",
" 'stdout': result.stdout,\n",
" 'stderr': result.stderr\n",
" }\n",
" else:\n",
" return {\n",
" 'success': False,\n",
" 'message': '✗ Compilation failed',\n",
" 'stdout': result.stdout,\n",
" 'stderr': result.stderr\n",
" }\n",
" except subprocess.TimeoutExpired:\n",
" return {\n",
" 'success': False,\n",
" 'message': '✗ Compilation timed out'\n",
" }\n",
" except FileNotFoundError:\n",
" return {\n",
" 'success': False,\n",
" 'message': '✗ g++ compiler not found. Please install g++ to compile C++ code.'\n",
" }\n",
" except Exception as e:\n",
" return {\n",
" 'success': False,\n",
" 'message': f'✗ Compilation error: {str(e)}'\n",
" }"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Complete Pipeline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def translate_and_compile(python_code, llm=\"gpt\", model=None, verbose=True):\n",
" \"\"\"\n",
" Translate Python to C++ and attempt compilation\n",
" \n",
" Args:\n",
" python_code (str): Python code to translate\n",
" llm (str): LLM to use\n",
" model (str): Specific model version\n",
" verbose (bool): Print detailed output\n",
" \n",
" Returns:\n",
" dict: Results including translated code and compilation status\n",
" \"\"\"\n",
" # Translate\n",
" cpp_code = translate_python_to_cpp(python_code, llm, model)\n",
" \n",
" if verbose:\n",
" print(\"\\n\" + \"=\"*60)\n",
" print(\"TRANSLATED C++ CODE:\")\n",
" print(\"=\"*60)\n",
" print(cpp_code)\n",
" print(\"=\"*60 + \"\\n\")\n",
" \n",
" # Compile\n",
" print(\"🔨 Attempting to compile...\")\n",
" compilation_result = compile_cpp_code(cpp_code)\n",
" \n",
" if verbose:\n",
" print(compilation_result['message'])\n",
" if not compilation_result['success'] and 'stderr' in compilation_result:\n",
" print(\"\\nCompilation errors:\")\n",
" print(compilation_result['stderr'])\n",
" \n",
" return {\n",
" 'cpp_code': cpp_code,\n",
" 'compilation': compilation_result\n",
" }"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 1: Factorial Function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"python_code_1 = \"\"\"\n",
"def factorial(n):\n",
" if n <= 1:\n",
" return 1\n",
" return n * factorial(n - 1)\n",
"\n",
"# Test the function\n",
"print(factorial(5))\n",
"\"\"\"\n",
"\n",
"print(\"Example 1: Factorial Function\")\n",
"print(\"=\"*60)\n",
"result1 = translate_and_compile(python_code_1, llm=\"gpt\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 2: Sum of Squares"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"python_code_2 = \"\"\"\n",
"def sum_of_squares(numbers):\n",
" return sum(x**2 for x in numbers)\n",
"\n",
"numbers = [1, 2, 3, 4, 5]\n",
"result = sum_of_squares(numbers)\n",
"print(f\"Sum of squares: {result}\")\n",
"\"\"\"\n",
"\n",
"print(\"Example 2: Sum of Squares\")\n",
"print(\"=\"*60)\n",
"result2 = translate_and_compile(python_code_2, llm=\"claude\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 3: Fibonacci with Gemini"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"python_code_3 = \"\"\"\n",
"def fibonacci(n):\n",
" if n <= 1:\n",
" return n\n",
" a, b = 0, 1\n",
" for _ in range(2, n + 1):\n",
" a, b = b, a + b\n",
" return b\n",
"\n",
"print(f\"Fibonacci(10) = {fibonacci(10)}\")\n",
"\"\"\"\n",
"\n",
"print(\"Example 3: Fibonacci with Gemini\")\n",
"print(\"=\"*60)\n",
"result3 = translate_and_compile(python_code_3, llm=\"gemini\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compare All LLMs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def compare_llms(python_code):\n",
" \"\"\"Compare all three LLMs on the same Python code\"\"\"\n",
" llms = [\"gpt\", \"gemini\", \"claude\"]\n",
" results = {}\n",
" \n",
" for llm in llms:\n",
" print(f\"\\n{'='*60}\")\n",
" print(f\"Testing with {llm.upper()}\")\n",
" print('='*60)\n",
" results[llm] = translate_and_compile(python_code, llm=llm, verbose=False)\n",
" print(results[llm]['compilation']['message'])\n",
" \n",
" return results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Test code for comparison\n",
"python_code_compare = \"\"\"\n",
"def is_prime(n):\n",
" if n < 2:\n",
" return False\n",
" for i in range(2, int(n**0.5) + 1):\n",
" if n % i == 0:\n",
" return False\n",
" return True\n",
"\n",
"primes = [x for x in range(2, 20) if is_prime(x)]\n",
"print(f\"Primes under 20: {primes}\")\n",
"\"\"\"\n",
"\n",
"print(\"COMPARING ALL LLMs\")\n",
"print(\"=\"*60)\n",
"comparison_results = compare_llms(python_code_compare)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interactive Translation Mode\n",
"\n",
"Use this cell to translate your own Python code interactively:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your custom Python code here\n",
"your_python_code = \"\"\"\n",
"# Paste your Python code here\n",
"def hello_world():\n",
" print(\"Hello, World!\")\n",
"\n",
"hello_world()\n",
"\"\"\"\n",
"\n",
"# Choose your LLM: \"gpt\", \"gemini\", or \"claude\"\n",
"chosen_llm = \"gpt\"\n",
"\n",
"result = translate_and_compile(your_python_code, llm=chosen_llm)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"You now have a complete Python to C++ translator! \n",
"\n",
"### Main Functions:\n",
"- `translate_python_to_cpp(code, llm, model)` - Translate only\n",
"- `translate_and_compile(code, llm, model)` - Translate and compile\n",
"- `compare_llms(code)` - Compare all three LLMs\n",
"\n",
"### Supported LLMs:\n",
"- **gpt** - OpenAI GPT-4o\n",
"- **gemini** - Google Gemini 2.0 Flash\n",
"- **claude** - Anthropic Claude Sonnet 4\n",
"\n",
"Happy translating! 🚀"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}