Appearance
question:Write a function to find the shortest path from the top-left corner to the bottom-right corner of an m x n grid. Each cell in the grid contains a non-negative integer. You can move either down or right at any point in time.
answer:def shortest_path(grid): Returns the shortest path from the top-left corner to the bottom-right corner of a grid. Each cell contains a non-negative integer representing the cost to move through that cell. You can move either down or right at any point in time. if not grid or not grid[0]: return 0 m, n = len(grid), len(grid[0]) dp = [[0] * n for _ in range(m)] dp[0][0] = grid[0][0] for i in range(1, m): dp[i][0] = dp[i-1][0] + grid[i][0] for j in range(1, n): dp[0][j] = dp[0][j-1] + grid[0][j] for i in range(1, m): for j in range(1, n): dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j] return dp[m-1][n-1]
question:Write a function to output the first N numbers of the Fibonacci sequence. Could you provide a solution in Python?
answer:def fibonacci(n): Returns the first n numbers of the Fibonacci sequence. Parameters: n (int): The number of elements in the Fibonacci sequence to return Returns: list: A list containing the first n numbers of the Fibonacci sequence if n <= 0: return [] elif n == 1: return [0] fib_seq = [0, 1] while len(fib_seq) < n: fib_seq.append(fib_seq[-1] + fib_seq[-2]) return fib_seq
question:Write a function to clean the input text from HTML tags. Use Python and provide a solution that correctly handles nested tags and multiple instances of the same tag within the input text.
answer:from bs4 import BeautifulSoup def clean_html_tags(input_text): Cleans the input text from HTML tags. Parameters: input_text (str): The text with HTML tags. Returns: str: The cleaned text without HTML tags. soup = BeautifulSoup(input_text, "html.parser") clean_text = soup.get_text() return clean_text
question:Write a function to implement FizzBuzz, a classic problem in software engineering interviews. For numbers that are multiples of 3, output "Fizz" instead of the number. For numbers that are multiples of 5, output "Buzz". For numbers that are multiples of both 3 and 5, output "FizzBuzz". For all other numbers, output the number itself.
answer:def fizz_buzz(n): Returns the FizzBuzz output for numbers from 1 to n. result = [] for i in range(1, n + 1): if i % 3 == 0 and i % 5 == 0: result.append("FizzBuzz") elif i % 3 == 0: result.append("Fizz") elif i % 5 == 0: result.append("Buzz") else: result.append(str(i)) return result