|
|
|
""" |
|
Test the behavior of duplicate function definitions |
|
""" |
|
|
|
from typing import List |
|
|
|
|
|
print("=== Case 1: Duplicate function definition ===") |
|
|
|
code_duplicate = ''' |
|
def has_close_elements(numbers: List[float], threshold: float) -> bool: |
|
""" Check if in given list of numbers, are any two numbers closer to each other than |
|
given threshold. |
|
>>> has_close_elements([1.0, 2.0, 3.0], 0.5) |
|
False |
|
>>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3) |
|
True |
|
""" |
|
def has_close_elements(numbers: List[float], threshold: float) -> bool: |
|
for i in range(len(numbers) - 1): |
|
for j in range(i + 1, len(numbers)): |
|
if abs(numbers[i] - numbers[j]) < threshold: |
|
return True |
|
return False |
|
''' |
|
|
|
|
|
exec(code_duplicate) |
|
try: |
|
|
|
result = has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3) |
|
print(f"Result for [1.0, 2.8, 3.0, 4.0, 5.0, 2.0] with threshold 0.3: {result}") |
|
print(f"Expected: True, Got: {result}") |
|
print(f"Test {'PASSED' if result == True else 'FAILED'}") |
|
except Exception as e: |
|
print(f"Error: {e}") |
|
|
|
|
|
print("\n=== Case 2: Normal function definition ===") |
|
|
|
code_normal = ''' |
|
def has_close_elements_normal(numbers: List[float], threshold: float) -> bool: |
|
""" Check if in given list of numbers, are any two numbers closer to each other than |
|
given threshold. |
|
""" |
|
for i in range(len(numbers) - 1): |
|
for j in range(i + 1, len(numbers)): |
|
if abs(numbers[i] - numbers[j]) < threshold: |
|
return True |
|
return False |
|
''' |
|
|
|
exec(code_normal) |
|
result = has_close_elements_normal([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3) |
|
print(f"Result for [1.0, 2.8, 3.0, 4.0, 5.0, 2.0] with threshold 0.3: {result}") |
|
print(f"Expected: True, Got: {result}") |
|
print(f"Test {'PASSED' if result == True else 'FAILED'}") |
|
|
|
|
|
print("\n=== Case 3: Duplicate with explicit return ===") |
|
|
|
code_duplicate_with_return = ''' |
|
def has_close_elements_fixed(numbers: List[float], threshold: float) -> bool: |
|
""" Check if in given list of numbers, are any two numbers closer to each other than |
|
given threshold. |
|
""" |
|
def has_close_elements_inner(numbers: List[float], threshold: float) -> bool: |
|
for i in range(len(numbers) - 1): |
|
for j in range(i + 1, len(numbers)): |
|
if abs(numbers[i] - numbers[j]) < threshold: |
|
return True |
|
return False |
|
|
|
# ๋ด๋ถ ํจ์๋ฅผ ํธ์ถํด์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ |
|
return has_close_elements_inner(numbers, threshold) |
|
''' |
|
|
|
exec(code_duplicate_with_return) |
|
result = has_close_elements_fixed([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3) |
|
print(f"Result for [1.0, 2.8, 3.0, 4.0, 5.0, 2.0] with threshold 0.3: {result}") |
|
print(f"Expected: True, Got: {result}") |
|
print(f"Test {'PASSED' if result == True else 'FAILED'}") |