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