Skip to content

Commit

Permalink
Merge pull request #1095 from gpt-engineer-org/improve-function-hot-f…
Browse files Browse the repository at this point in the history
…ix-for-app-benchmark

Improve function hot fix for app benchmark
  • Loading branch information
ATheorell authored Apr 7, 2024
2 parents df593e1 + 605e499 commit 2bda71b
Show file tree
Hide file tree
Showing 25 changed files with 349 additions and 89 deletions.
11 changes: 8 additions & 3 deletions gpt_engineer/core/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,13 @@ def find_start_line(self, lines_dict: dict, problems: list) -> bool:
if is_similar(line[1], line_content) and line[1] != "":
start_line = line_number - 1
break
# if the start line is not found, this should be a comment from LLM
# if the start line is not found, append a problem message
if start_line is None:
self.relabel_line(index, ADD)
continue
problems.append(
f"In {self.hunk_to_string()}:can not find the starting line of the diff"
)
return False

else:
# the line prior to the start line is found now we insert it to the first place as the start line
self.start_line_pre_edit = start_line
Expand Down Expand Up @@ -323,6 +326,8 @@ def __init__(self, filename_pre, filename_post) -> None:

def is_new_file(self) -> bool:
"""Determines if the diff represents a new file."""
if self.filename_pre == "/dev/null":
return True
return any(hunk.is_new_file for hunk in self.hunks)

def diff_to_string(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions gpt_engineer/preprompts/file_format_diff
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ Example of a git diff creating a new file:
-LINES TO BE REMOVED (starting with single -) AND LINES TO BE RETAIN (no starting symbol) HAVE TO REPLICATE THE DIFFED HUNK OF THE CODE EXACTLY LINE BY LINE. KEEP THE NUMBER OF RETAIN LINES SMALL IF POSSIBLE.
-EACH LINE IN THE SOURCE FILES STARTS WITH A LINE NUMBER, WHICH IS NOT PART OF THE SOURCE CODE. NEVER TRANSFER THESE LINE NUMBERS TO THE DIFF HUNKS.
-AVOID STARTING A HUNK WITH AN EMPTY LINE.
-ALL DIFFS WILL BE APPLIED SIMULTANEOUSLY, AVOID SEQUENTIAL HUNKS.
65 changes: 65 additions & 0 deletions tests/core/improve_function_test_cases/apps_benchmark_6_chat
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
To implement this program, we will follow a structured approach. We will create a Python script that takes command-line arguments for the inputs, parses them, and then processes each query to determine the minimum number of blows required to defeat Zmei Gorynich. The program will consist of a main function that handles input parsing and output formatting, and a separate function that calculates the minimum number of blows for each query.

We will need to:
1. Parse the command-line arguments to extract the queries.
2. For each query, calculate the minimum number of blows required.
3. Print the result for each query.

Let's start by creating the `main.py` file with the necessary imports and the skeleton of our program.

```diff
--- /dev/null
+++ main.py
@@ -0,0 +1,36 @@
+import sys
+
+def calculate_min_blows(n, x, blows):
+ """
+ Calculate the minimum number of blows to defeat Zmei Gorynich.
+ :param n: Number of types of blows.
+ :param x: Initial number of heads.
+ :param blows: List of tuples (d_i, h_i) for each blow type.
+ :return: Minimum number of blows or -1 if impossible.
+ """
+ # Implementation will be added here
+ pass
+
+def main():
+ # Parse command-line arguments
+ args = sys.argv[1].split()
+ t = int(args[0])
+ queries = []
+ idx = 1
+ for _ in range(t):
+ n, x = map(int, args[idx:idx+2])
+ idx += 2
+ blows = [tuple(map(int, args[idx:idx+2])) for _ in range(n)]
+ idx += 2*n
+ queries.append((n, x, blows))
+
+ # Process each query
+ for n, x, blows in queries:
+ result = calculate_min_blows(n, x, blows)
+ print(result)
+
+if __name__ == "__main__":
+ main()
```
Now, let's implement the logic in calculate_min_blows function to determine the minimum number of blows required to defeat Zmei Gorynich.
```diff
--- main.py
+++ main.py
@@ -8,7 +8,25 @@
+ # Implementation will be added here
+ max_damage = max(blows, key=lambda x: x[0])
+ max_diff = max(blows, key=lambda x: x[0] - x[1])
+ if max_damage[0] >= x:
+ return 1
+ if max_diff[0] <= max_diff[1] and max_damage[0] < x:
+ return -1
+ effective_damage = max_diff[0] - max_diff[1]
+ remaining_heads = x - max_damage[0]
+ blows_required = 1 + (remaining_heads + effective_damage - 1) // effective_damage
+ return blows_required
- pass
```
Empty file.
17 changes: 17 additions & 0 deletions tests/core/improve_function_test_cases/apps_benchmark_6_v2_chat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
```diff
--- main.py
+++ main.py
@@ -8,7 +8,25 @@
+ # Implementation will be added here
+ max_damage = max(blows, key=lambda x: x[0])
+ max_diff = max(blows, key=lambda x: x[0] - x[1])
+ if max_damage[0] >= x:
+ return 1
+ if max_diff[0] <= max_diff[1] and max_damage[0] < x:
+ return -1
+ effective_damage = max_diff[0] - max_diff[1]
+ remaining_heads = x - max_damage[0]
+ blows_required = 1 + (remaining_heads + effective_damage - 1) // effective_damage
+ return blows_required
- pass
```
35 changes: 35 additions & 0 deletions tests/core/improve_function_test_cases/apps_benchmark_6_v2_code
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys

def calculate_min_blows(n, x, blows):
"""
Calculate the minimum number of blows to defeat Zmei Gorynich.
:param n: Number of types of blows.
:param x: Initial number of heads.
:param blows: List of tuples (d_i, h_i) for each blow type.
:return: Minimum number of blows or -1 if impossible.
"""
# Implementation will be added here
pass

def main():
# Parse command-line arguments
args = sys.argv[1].split()
t = int(args[0])
queries = []
idx = 1
for _ in range(t):
n, x = map(int, args[idx:idx+2])
idx += 2
blows = [tuple(map(int, args[idx:idx+2])) for _ in range(n)]
idx += 2*n
queries.append((n, x, blows))

# Process each query
for n, x, blows in queries:
result = calculate_min_blows(n, x, blows)
print(result)

if __name__ == "__main__":
main()"""

print(code)
99 changes: 99 additions & 0 deletions tests/core/improve_function_test_cases/create_two_new_files_chat
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
To implement the requested functionality, we'll create a Python program that processes the input, evaluates the service history of a car, and outputs the appropriate message based on the criteria provided. We'll structure the program into several parts:

1. **Parsing Input**: A function to parse the input string into a structured format.
2. **Service History Evaluation**: A class to evaluate the service history entries and determine the status of the odometer and service frequency.
3. **Main Logic**: The main logic that ties everything together and outputs the final result.

Let's start by creating the necessary files and writing the initial code.

### File: requirements.txt
This file will list the dependencies required for the project. Since the project does not require external libraries, this file will be empty but included for completeness.

```diff
```

### File: service_history_evaluator.py
This file will contain the class responsible for evaluating the service history.

```diff
--- /dev/null
+++ service_history_evaluator.py
@@ -0,0 +1,47 @@
+from datetime import datetime, timedelta
+
+class ServiceHistoryEvaluator:
+ """
+ Evaluates the service history of a car to determine if the odometer has been tampered with
+ or if the service frequency is insufficient.
+ """
+ def __init__(self, entries):
+ self.entries = entries
+
+ def evaluate(self):
+ # Placeholder for the evaluation logic
+ pass
+
+ def _parse_date(self, year, month):
+ """
+ Parses the year and month into a datetime object.
+ """
+ return datetime(year, month, 1)
+
+ def _calculate_distance(self, start, end):
+ """
+ Calculates the minimum and maximum possible distance traveled between two services.
+ """
+ months_diff = (end.year - start.year) * 12 + end.month - start.month
+ min_distance = 2000 * months_diff
+ max_distance = 20000 * months_diff
+ return min_distance, max_distance
+
+ def _check_odometer_rollover(self, prev_reading, current_reading, max_distance):
+ """
+ Checks if the odometer could have rolled over between two readings.
+ """
+ if prev_reading > current_reading:
+ return current_reading + (99999 - prev_reading) <= max_distance
+ else:
+ return current_reading - prev_reading <= max_distance
+
+ def _check_service_frequency(self):
+ """
+ Checks if the service frequency is within the legal limits.
+ """
+ # Placeholder for the service frequency check logic
+ pass
```

### File: main.py
This file will contain the main logic and the entry point of the program.

```diff
--- /dev/null
+++ main.py
@@ -0,0 +1,29 @@
+import sys
+from service_history_evaluator import ServiceHistoryEvaluator
+
+def parse_input(input_str):
+ """
+ Parses the input string into a structured format.
+ """
+ lines = input_str.split('\n')
+ num_entries = int(lines[0])
+ entries = [tuple(map(int, line.split())) for line in lines[1:num_entries+1]]
+ return entries
+
+def main(input_str):
+ entries = parse_input(input_str)
+ evaluator = ServiceHistoryEvaluator(entries)
+ result = evaluator.evaluate()
+ print(result)
+
+if __name__ == "__main__":
+ if len(sys.argv) != 2:
+ print("Usage: python main.py 'input1 input2 etc'")
+ sys.exit(1)
+ input_str = sys.argv[1].replace("'", "")
+ main(input_str)
```
Empty file.
Loading

0 comments on commit 2bda71b

Please sign in to comment.