Skip to content

Commit 28ef13c

Browse files
authored
Merge pull request #18 from ilyas829/update_3
Update 3
2 parents eb2fbff + 1a14149 commit 28ef13c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

projects/code-gen automation/code_generator.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ def generate_code(program_name: str, language: str, token: str, endpoint: str =
6666

6767
raise Exception(f"Failed to generate after retries: {last_error}")
6868

69+
70+
def _safe_filename(program_name: str, language: str) -> str:
71+
base = "".join(c for c in program_name if c.isalnum() or c in (" ", "-", "_")).strip()
72+
base = base.replace(" ", "_").lower() or "generated_code"
73+
return f"{base}_in_{language.lower()}.txt"
74+
75+
76+
def save_output(program_name: str, language: str, content: str) -> str:
77+
"""Save generated content to output/<safe>_<lang>.txt and return the path."""
78+
out_dir = Path("output")
79+
out_dir.mkdir(parents=True, exist_ok=True)
80+
filename = _safe_filename(program_name, language)
81+
out_path = out_dir / filename
82+
out_path.write_text(content, encoding="utf-8")
83+
return str(out_path)
84+
6985
import os
7086
def debug_connection(token: str, endpoint: str = DEFAULT_ENDPOINT) -> None:
7187
"""Deep connectivity diagnostics with extensive logging."""
@@ -147,7 +163,9 @@ def main() -> None:
147163
debug_connection(token, DEFAULT_ENDPOINT)
148164
try:
149165
code = generate_code(args.program_name, args.language, token)
166+
saved_path = save_output(args.program_name, args.language, code)
150167
print(code)
168+
print(f"\nSaved to: {saved_path}")
151169
except KeyboardInterrupt:
152170
print("\nOperation cancelled.")
153171
sys.exit(1)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
// Function to find two indices such that their values add up to the target
5+
// Returns dynamic array of indices or NULL if no solution is found
6+
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
7+
// Allocate memory for the return array
8+
int* indices = (int*)malloc(2 * sizeof(int));
9+
*returnSize = 0; // Initialize returnSize to 0
10+
11+
// Check all pairs of numbers
12+
for (int i = 0; i < numsSize; i++) {
13+
for (int j = i + 1; j < numsSize; j++) {
14+
// Check if the sum of the two numbers equals the target
15+
if (nums[i] + nums[j] == target) {
16+
indices[0] = i; // First index
17+
indices[1] = j; // Second index
18+
*returnSize = 2; // Set returnSize to 2
19+
return indices; // Return the indices
20+
}
21+
}
22+
}
23+
24+
// Return NULL if no indices found
25+
free(indices);
26+
return NULL;
27+
}
28+
29+
// Example usage of the twoSum function
30+
int main() {
31+
int nums[] = {2, 7, 11, 15}; // Example array
32+
int target = 9; // Example target sum
33+
int returnSize = 0; // Size of the return array
34+
35+
// Call the twoSum function
36+
int* result = twoSum(nums, sizeof(nums) / sizeof(nums[0]), target, &returnSize);
37+
38+
// Check if result is found
39+
if (result != NULL) {
40+
printf("Indices: %d, %d\n", result[0], result[1]); // Output the indices
41+
free(result); // Free allocated memory
42+
} else {
43+
printf("No two sum solution found.\n"); // Output if no solution
44+
}
45+
46+
return 0; // Exit the program
47+
}

0 commit comments

Comments
 (0)