File size: 3,083 Bytes
24c2665 |
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 |
#!/usr/bin/env python3
"""
Test the behavior of duplicate function definitions
"""
from typing import List
# Case 1: ์ค๋ณต ํจ์ ์ ์ (์ค์ฒฉ๋ ํจ์)
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:
# ์ธ๋ถ ํจ์๋ฅผ ํธ์ถํ๋ฉด ์ค์ ๋ก๋ ๋ณธ๋ฌธ์ด ์์ด์ None์ ๋ฐํ
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}")
# Case 2: ์ ์์ ์ธ ํจ์ ์ ์
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'}")
# Case 3: ๋ด๋ถ ํจ์์ return ๋ฌธ ์ถ๊ฐ
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'}") |