-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
83 lines (66 loc) · 2.44 KB
/
helpers.py
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
import re, json
initial_prompt = """
You are an expert Python programmer with a top rank on Codeforces (Grandmaster or International Grandmaster level). Your task is to create optimal Python code to solve the given problem, ensuring it can handle large inputs within the time constraints.
Problem Statement:
{statement}
Possible Time Complexity:
{time_complexity}
Possible Concepts:
{concepts}
## Sample Test cases for reference:
Input:
{sample_input}
Output:
{sample_output}
Your Task:
1. Create a python program that returns the correct output for the given input.
2. Make the code efficient and fast, so we can solve large inputs.
3. The file should have a single `solve` method that has the following signature:
input: str: The Input in the same format provided above
output: str: The Output in the same format provided above
```python
<insert necessary imports>
def solve(input: str) -> str:
```
"""
error_prompt = """The code execution failed due to an error: {str(e)}. Please fix the code to handle this error.
Current code:
{last_run_code}
Problem description:
{statement}
Sample input:
{sample_input}
Expected output:
{sample_output}
Please think out loud on what went wrong, and provide a corrected version of the code."""
def clean_invalid_escapes(json_string):
valid_escape_sequences = ['\\\\', '\\"', '\\/', '\\b', '\\f', '\\n', '\\r']
escape_sequence_regex = r'\\.'
def replace_invalid_escapes(match):
escape_seq = match.group(0)
if escape_seq not in valid_escape_sequences:
return escape_seq[1:]
return escape_seq
cleaned_json_string = re.sub(escape_sequence_regex, replace_invalid_escapes, json_string)
return cleaned_json_string
def parse_code(response: str) -> str:
if "```" not in response:
return response
code_pattern = r"```python((.|\n)*?)```"
code_blocks = re.findall(code_pattern, response, re.DOTALL)
if code_blocks:
return code_blocks[-1][0].strip()
return response
def parse_json(response: str) -> dict:
try:
return json.loads(clean_invalid_escapes(response))
except json.JSONDecodeError as e:
json_pattern = r"```json((.|\n)*?)```"
json_blocks = re.findall(json_pattern, response, re.DOTALL)
response = clean_invalid_escapes(json_blocks[-1][0].strip())
print(response)
try:
return json.loads(response)
except json.JSONDecodeError as e:
print(e)
return ""