#!/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'}")