-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_code.py
More file actions
executable file
·151 lines (132 loc) · 4.91 KB
/
Copy pathtest_code.py
File metadata and controls
executable file
·151 lines (132 loc) · 4.91 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 테스트 분석용 정렬 알고리즘
# 시간 복잡도 순으로 정렬
# 버블 정렬, 시간복잡도 O(n^2)
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
# 퀵 정렬, 시간 복잡도 O(n log n)
def quick_sort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[len(arr) // 2] # 피벗 선택
left = [x for x in arr if x < pivot] # 피벗보다 작은 요소
middle = [x for x in arr if x == pivot] # 피벗과 같은 요소
right = [x for x in arr if x > pivot] # 피벗보다 큰 요소
return quick_sort(left) + middle + quick_sort(right) # 재귀 호출
# 병합 정렬, 시간 복잡도 O(n log n)
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
return merge(merge_sort(left_half), merge_sort(right_half))
def merge(left, right):
merged = []
left_index = 0
right_index = 0
while left_index < len(left) and right_index < len(right):
if left[left_index] <= right[right_index]:
merged.append(left[left_index])
left_index += 1
else:
merged.append(right[right_index])
right_index += 1
merged.extend(left[left_index:])
merged.extend(right[right_index:])
return merged
def process_data(data_list, threshold=0.5):
result = []
for i, item in enumerate(data_list):
if isinstance(item, (int, float)):
if item > threshold:
processed = item * 1.5
if processed > 100:
processed = 100
elif processed < 0:
processed = 0
result.append((i, processed))
else:
if item < 0:
result.append((i, 0))
else:
result.append((i, item))
elif isinstance(item, str):
try:
num = float(item)
result.append((i, process_data([num], threshold)[0][1]))
except ValueError:
result.append((i, 0))
return result
class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def process_binary_tree(root, target_value):
if not root:
return None
result = []
stack = [(root, 0)]
while stack:
node, depth = stack.pop()
if node.value == target_value:
result.append((depth, node))
if depth > 10: # 깊이 제한
continue
if node.left:
if node.left.value < node.value:
stack.append((node.left, depth + 1))
else:
temp = node.left
node.left = None
process_binary_tree(temp, target_value)
if node.right:
if node.right.value > node.value:
stack.append((node.right, depth + 1))
else:
temp = node.right
node.right = None
process_binary_tree(temp, target_value)
return result
def validate_and_transform_data(data_dict):
result = {}
errors = []
for key, value in data_dict.items():
if not isinstance(key, str):
errors.append(f"Invalid key type: {type(key)}")
continue
if key.startswith('_'):
errors.append(f"Invalid key format: {key}")
continue
if isinstance(value, dict):
nested_result, nested_errors = validate_and_transform_data(value)
if nested_errors:
errors.extend([f"{key}.{err}" for err in nested_errors])
result[key] = nested_result
elif isinstance(value, list):
transformed_list = []
for i, item in enumerate(value):
if isinstance(item, dict):
item_result, item_errors = validate_and_transform_data(item)
if item_errors:
errors.extend([f"{key}[{i}].{err}" for err in item_errors])
transformed_list.append(item_result)
elif isinstance(item, (int, float, str)):
if isinstance(item, str) and not item.strip():
errors.append(f"Empty string in {key}[{i}]")
continue
transformed_list.append(item)
else:
errors.append(f"Invalid type in {key}[{i}]: {type(item)}")
result[key] = transformed_list
else:
if isinstance(value, str) and not value.strip():
errors.append(f"Empty string in {key}")
continue
result[key] = value
return result, errors