Appearance
question:Write a function to solve the N-Queens problem in Python. The N-Queens problem is a classic combinatorial problem where the goal is to place N queens on an N×N chessboard such that no two queens threaten each other. Thus, a queen can only be placed on a cell that is not in the same row, the same column, or the same diagonal as any other queen. You should return all distinct solutions to the N-Queens puzzle the board is represented as a 2D list where ‘Q’ represents a queen and ‘.’ represents an empty space.
answer:def solveNQueens(n): def is_safe(board, row, col): # Check if no other queens are in the same column for i in range(row): if board[i][col] == 'Q': return False # Check upper left diagonal i, j = row-1, col-1 while i >= 0 and j >= 0: if board[i][j] == 'Q': return False i -= 1 j -= 1 # Check upper right diagonal i, j = row-1, col+1 while i >= 0 and j < n: if board[i][j] == 'Q': return False i -= 1 j += 1 return True def solve(board, row, solutions): if row == n: solutions.append([''.join(r) for r in board]) return for col in range(n): if is_safe(board, row, col): board[row][col] = 'Q' solve(board, row+1, solutions) board[row][col] = '.' solutions = [] board = [['.' for _ in range(n)] for _ in range(n)] solve(board, 0, solutions) return solutions
question:Write a function to convert a decimal number to its binary equivalent using recursion. The function should handle both positive and negative integers and should not use loops. The function should also handle floating-point numbers but should only consider the integer part of the number for conversion.
answer:def decimal_to_binary(n): Converts a decimal number to its binary equivalent using recursion. This function handles both positive and negative integers, and for floating-point numbers, only the integer part is considered. # Handling the base case for zero if n == 0: return "0" # This inner function will handle the actual conversion using recursion def convert_to_binary(num): if num == 0: return "" else: return convert_to_binary(num // 2) + str(num % 2) # Handling negative numbers if n < 0: return '-' + convert_to_binary(abs(int(n))) else: return convert_to_binary(int(n))
question:Write a function to find the given value `v` in a 2D matrix `A` of size `n x m`. If `v` is found, return its position `(i, j)`, where `i` and `j` are the row and column indices of `v`. If `v` is not found, return `(-1, -1)`. The function should have a time complexity of `O(n * m)`.
answer:def find_value_in_matrix(A, v): Finds the value `v` in the 2D matrix `A`. If found, returns the position (i, j). Otherwise, returns (-1, -1). :param A: List[List[int]] - 2D matrix of size n x m :param v: int - value to find in the matrix :return: Tuple[int, int] - position of value in the matrix or (-1, -1) if not found n = len(A) for i in range(n): m = len(A[i]) for j in range(m): if A[i][j] == v: return (i, j) return (-1, -1)
question:Write a function to reverse a singly linked list using the iterative method in Python.
answer:class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def reverse_linked_list(head): Reverses a singly linked list using the iterative method. :param head: ListNode, the head of the linked list :return: ListNode, the new head of the reversed linked list prev = None current = head while current: next_node = current.next current.next = prev prev = current current = next_node return prev