Appearance
question:Given a list of numbers in Python, sort the numbers in ascending order using a self-designed sorting algorithm instead of using built-in functions.
answer:def bubble_sort(arr): Sorts a list of numbers in ascending order using the bubble sort algorithm. 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] return arr
question:Transform a randomly sorted list into Heap form using the construction method, after this, implement the heap sort algorithm to sort the list in ascending order. The expected output should be a sorted list, however ensuring that the heap properties are maintained throughout the sorting process.
answer:def heapify(arr, n, i): Helper function to maintain the heap property of a subtree rooted at node i. Given n is the size of the heap. largest = i left = 2 * i + 1 right = 2 * i + 2 if left < n and arr[left] > arr[largest]: largest = left if right < n and arr[right] > arr[largest]: largest = right if largest != i: arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest) def build_heap(arr): Function to build a heap from the given array. n = len(arr) for i in range(n // 2 - 1, -1, -1): heapify(arr, n, i) def heap_sort(arr): Function to sort an array using heap sort algorithm. n = len(arr) build_heap(arr) for i in range(n - 1, 0, -1): arr[i], arr[0] = arr[0], arr[i] heapify(arr, i, 0) return arr
question:Design a python function that creates two-dimensional arrays in the format [[a11, a12, ..., a1n], [a21, a22, ..., a2n], ..., [am1, am2, ..., amn]] from three one-dimensional arrays. One array contains values while the other two contain row and column indices, respectively. This enhanced function should also validate whether the provided indices and values can map to a valid 2-dimensional structure before proceeding with further operations. Furthermore, devise a method to insert an element into a specific position in the created 2-D array as a sub-task, without shifting other elements, thus updating the array directly.
answer:def create_2d_array(values, row_indices, col_indices): Creates a 2D array from values and their row and column indices. Validates the indices and values. if not (len(values) == len(row_indices) == len(col_indices)): raise ValueError("All input arrays must have the same length.") max_row = max(row_indices) if row_indices else 0 max_col = max(col_indices) if col_indices else 0 # Initialize the matrix with None values matrix = [[None for _ in range(max_col + 1)] for _ in range(max_row + 1)] for val, row, col in zip(values, row_indices, col_indices): matrix[row][col] = val return matrix def insert_into_2d_array(matrix, value, row, col): Inserts a value into the specified position in the 2D array. if row >= len(matrix) or col >= len(matrix[0]): raise IndexError("Row/Column index out of bounds.") matrix[row][col] = value return matrix
question:Given a list of n vertices representing a non-conex graph in 3D Euclidean space, design a Python function named findShortestPaths3D. This function should apply a variant of Dijkstra's algorithm to compute the shortest paths between any two points in the graph. However, unlike the classical algorithm, this function should handle three dimensions and should be capable to estimate the distance between any two points with the sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2) formula rather than just considering vertices that are directly connected.
answer:import heapq import math def findShortestPaths3D(vertices): Computes the shortest paths between any two points in a 3D non-conex graph using a variant of Dijkstra's algorithm. Parameters: vertices (List[Tuple[float, float, float]]): A list of n vertices representing points in 3D space. Returns: dict: A dictionary where the keys are tuples (i, j) representing vertex indices and values are the shortest path distances. def euclidean_distance(p1, p2): return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2 + (p2[2] - p1[2])**2) n = len(vertices) shortest_paths = {} for i in range(n): distances = {i: 0} pq = [(0, i)] # Priority queue of (distance, vertex) visited = set() while pq: current_distance, u = heapq.heappop(pq) if u in visited: continue visited.add(u) for v in range(n): if v == u: continue distance = euclidean_distance(vertices[u], vertices[v]) if v not in distances or current_distance + distance < distances[v]: distances[v] = current_distance + distance heapq.heappush(pq, (distances[v], v)) for j in range(n): if i != j: shortest_paths[(i, j)] = distances[j] return shortest_paths