Skip to content
🤔prompts chat🧠
🔍
question:def perform_operations(Q: int, operations: List[str]) -> List[int]: Perform a series of append and delete operations on an initially empty string, and return a list of the counts of each character from 'a' to 'z' in the final string. Parameters: Q (int): the number of operations. operations (List[str]): the list of operations to perform. Returns: List[int]: a list of 26 integers where the i-th integer denotes the number of times the i-th character (from 'a' to 'z') appears in the final string. Examples: >>> perform_operations(6, ["append(a)", "append(b)", "append(a)", "delete()", "append(c)", "append(c)"]) [1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >>> perform_operations(4, ["append(a)", "append(b)", "delete()", "delete()"]) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

answer:def perform_operations(Q, operations): from collections import defaultdict S = [] char_counts = defaultdict(int) for operation in operations: if operation.startswith("append"): _, char = operation.split('(') char = char.rstrip(')') S.append(char) char_counts[char] += 1 elif operation == "delete()": if S: removed_char = S.pop() char_counts[removed_char] -= 1 result = [char_counts[chr(i)] for i in range(ord('a'), ord('z') + 1)] return result

question:def reverse_alternating(s: str) -> str: This function takes a single string as input and returns a new string with alternating words reversed, starting from the second word. >>> reverse_alternating("hello world") 'hello dlrow' >>> reverse_alternating("keep calm and code on") 'keep mlac and edoc on' >>> reverse_alternating("a quick brown fox") 'a kciuq brown xof'

answer:def reverse_alternating(s): This function takes a single string as input and returns a new string with alternating words reversed, starting from the second word. words = s.split() for i in range(1, len(words), 2): words[i] = words[i][::-1] return " ".join(words)

question:def find_path(maze, n, m): Determine if there is a path from the starting point 'S' to the ending point 'E' in a maze. The maze is represented as a grid where: 'S' is the starting point 'E' is the ending point '.' is a passable cell '#' is an impassable cell Args: maze (List[List[str]]): The maze represented by a 2D list of characters. n (int): Number of rows in the maze. m (int): Number of columns in the maze. Returns: str: "YES" if there is a path from 'S' to 'E', "NO" otherwise. Examples: >>> find_path([ ... ["S", ".", ".", "#", "."], ... [".", "#", ".", ".", "#"], ... [".", "#", ".", ".", "#"], ... [".", ".", "#", "#", "#"], ... [".", ".", ".", "E", "."] ... ], 5, 5) "YES" >>> find_path([ ... ["S", ".", "#", "."], ... ["#", ".", ".", "#"], ... [".", ".", "#", "."], ... [".", "#", "E", "#"] ... ], 4, 4) "NO"

answer:def find_path(maze, n, m): from collections import deque # Find the starting point 'S' for i in range(n): for j in range(m): if maze[i][j] == 'S': start = (i, j) if maze[i][j] == 'E': end = (i, j) # Directions for moving (right, left, down, up) directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] # BFS to find the path queue = deque([start]) visited = set() visited.add(start) while queue: current = queue.popleft() if current == end: return "YES" for direction in directions: ni, nj = current[0] + direction[0], current[1] + direction[1] if 0 <= ni < n and 0 <= nj < m and (ni, nj) not in visited and maze[ni][nj] != '#': queue.append((ni, nj)) visited.add((ni, nj)) return "NO" # Example usage: n, m = 5, 5 maze = [ "S..#.", ".#..#", ".#..#", "..#", "...E." ] print(find_path([list(row) for row in maze], n, m)) # Output: YES n, m = 4, 4 maze = [ "S.#.", "#..#", "..#.", ".#E#" ] print(find_path([list(row) for row in maze], n, m)) # Output: NO

question:def max_elements_with_sum_lte_k(T, test_cases): Given an array of N integers a[1], a[2], ..., a[N] and an integer K, find the maximum number of elements you can choose such that their sum is less than or equal to K. Args: T (int): the number of test cases. test_cases (List[Tuple[int, int, List[int]]]): a list of test cases, where each test case consists of: - an integer N, - an integer K, - a list of N integers. Returns: List[int]: a list of integers where each integer is the results of the corresponding test case. >>> max_elements_with_sum_lte_k(2, [(5, 9, [1, 2, 3, 4, 5]), (4, 7, [2, 2, 2, 2])]) [3, 3] >>> max_elements_with_sum_lte_k(1, [(4, 100, [1, 2, 3, 4])]) [4]

answer:def max_elements_with_sum_lte_k(T, test_cases): results = [] for i in range(T): N, K, array = test_cases[i] array.sort() current_sum = 0 count = 0 for num in array: if current_sum + num <= K: current_sum += num count += 1 else: break results.append(count) return results

Released under the chat License.

has loaded