This Chat is read-only. Login to resume chatting.
def apply_operation(a, b, operation):
return operation(a, b)
def add(x, y):
return x + y
def multiply(x, y):
return x * y
result_add = apply_operation(3, 4, add)
result_multiply = apply_operation(3, 4, multiply)
print(result_add) # Вывод: 7
print(result_multiply) # Вывод: 12def command_interpreter(command):
if command == "start":
return "Starting the process..."
elif command == "stop":
return "Stopping the process..."
elif command == "status":
return "Current status: Running"
else:
return "Unknown command"
print(command_interpreter("start")) # Вывод: Starting the process...import json
def interpret_json_data(json_string):
data = json.loads(json_string)
if "action" in data:
if data["action"] == "greet":
return f"Hello, {data.get('name', 'User')}"
return "Unknown action"
json_data = '{"action": "greet", "name": "Alice"}'
print(interpret_json_data(json_data)) # Вывод: Hello, Alicedef calculate(a, b, operation):
if operation == "add":
return a + b
elif operation == "subtract":
return a - b
elif operation == "multiply":
return a * b
elif operation == "divide":
return a / b
else:
return "Unknown operation"
result = calculate(10, 5, "add")
print(result) # Вывод: 15def sort_list(lst, reverse=False):
return sorted(lst, reverse=reverse)
numbers = [3, 1, 4, 1, 5, 9, 2]
sorted_numbers = sort_list(numbers, reverse=True)
print(sorted_numbers) # Вывод: [9, 5, 4, 3, 2, 1, 1]import json
def format_data(data, format_type):
if format_type == "json":
return json.dumps(data)
elif format_type == "string":
return str(data)
else:
return "Unknown format"
data = {"name": "Alice", "age": 30}
formatted_data = format_data(data, "json")
print(formatted_data) # Вывод: {"name": "Alice", "age": 30}def quicksort(arr):
if len(arr) <= 1:
return arr
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 quicksort(left) + middle + quicksort(right)
numbers = [3, 6, 8, 10, 1, 2, 1]
sorted_numbers = quicksort(numbers)
print(sorted_numbers) # Вывод: [1, 1, 2, 3, 6, 8, 10]def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] < target:
left = mid + 1
elif arr[mid] > target:
right = mid - 1
else:
return mid
return -1
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 5
index = binary_search(numbers, target)
print(index) # Вывод: 4def filter_even_numbers(numbers):
return [num for num in numbers if num % 2 == 0]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_numbers = filter_even_numbers(numbers)
print(filtered_numbers) # Вывод: [2, 4, 6, 8, 10]