Skip to content
🤔prompts chat🧠
🔍
question:def digit_to_word(n: int) -> str: Converts an integer (0-9999) to its word representation in English. >>> digit_to_word(123) "one hundred twenty three" >>> digit_to_word(45) "forty five" >>> digit_to_word(900) "nine hundred" def process_cases(T: int, cases: List[int]) -> List[str]: Processes multiple test cases. >>> process_cases(3, [123, 45, 900]) ["one hundred twenty three", "forty five", "nine hundred"]

answer:def digit_to_word(n): Converts an integer (0-9999) to its word representation in English. if n == 0: return "zero" units = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] thousands = ["", "one thousand", "two thousand", "three thousand", "four thousand", "five thousand", "six thousand", "seven thousand", "eight thousand", "nine thousand"] words = [] if n // 1000 > 0: words.append(thousands[n // 1000]) n %= 1000 if n // 100 > 0: words.append(units[n // 100]) words.append("hundred") n %= 100 if 10 <= n <= 19: words.append(teens[n - 10]) else: if n // 10 > 0: words.append(tens[n // 10]) if n % 10 > 0: words.append(units[n % 10]) return ' '.join(words).strip() def process_cases(T, cases): results = [] for number in cases: results.append(digit_to_word(number)) return results

question:def has_robot_returned_to_origin(s: str) -> str: Determines if the robot returns to the origin after following the sequence of commands. Parameters: s: A string of commands 'U', 'D', 'L', 'R'. Returns: "YES" if robot returns to the origin, "NO" otherwise. Examples: >>> has_robot_returned_to_origin("UD") "YES" >>> has_robot_returned_to_origin("LL") "NO" >>> has_robot_returned_to_origin("UUDDLRLR") "YES" from has_robot_returned_to_origin import has_robot_returned_to_origin def test_robot_returns_to_origin(): assert has_robot_returned_to_origin("UD") == "YES" def test_robot_does_not_return_to_origin(): assert has_robot_returned_to_origin("LL") == "NO" def test_robot_returns_with_complex_movement(): assert has_robot_returned_to_origin("UUDDLRLR") == "YES" def test_robot_with_single_command(): assert has_robot_returned_to_origin("U") == "NO" assert has_robot_returned_to_origin("D") == "NO" assert has_robot_returned_to_origin("L") == "NO" assert has_robot_returned_to_origin("R") == "NO" def test_robot_with_imbalanced_commands(): assert has_robot_returned_to_origin("UUUUDDDDLLLLRRRR") == "YES" assert has_robot_returned_to_origin("UUULLLRRR") == "NO"

answer:def has_robot_returned_to_origin(s): Determines if the robot returns to the origin after following the sequence of commands. Parameters: s (str): A string of commands 'U', 'D', 'L', 'R'. Returns: str: "YES" if robot returns to the origin, "NO" otherwise. # Initialize starting position x, y = 0, 0 # Iterate through the command string for command in s: if command == 'U': y += 1 elif command == 'D': y -= 1 elif command == 'L': x -= 1 elif command == 'R': x += 1 # Check if robot is back at the origin return "YES" if x == 0 and y == 0 else "NO"

question:def longest_subarray_length(arr, N, D): Finds the length of the longest contiguous subarray where the difference between the maximum and minimum values in the subarray is less than or equal to D. >>> longest_subarray_length([1, 3, 6, 7, 9, 2], 6, 3) == 3 >>> longest_subarray_length([1, 2, 3, 4, 5], 5, 2) == 3 >>> longest_subarray_length([10, 1, 2, 4, 7, 2], 6, 5) == 4 >>> longest_subarray_length([1, 1, 1, 1, 1], 5, 0) == 5 >>> longest_subarray_length([5, 4, 3, 2, 1], 5, 4) == 5 >>> longest_subarray_length([10, 5, 2, 7, 8, 7], 6, 3) == 3

answer:def longest_subarray_length(arr, N, D): Finds the length of the longest contiguous subarray where the difference between the maximum and minimum values in the subarray is less than or equal to D. max_len = 0 start = 0 min_val = arr[0] max_val = arr[0] for end in range(N): min_val = min(min_val, arr[end]) max_val = max(max_val, arr[end]) while max_val - min_val > D: start += 1 min_val = min(arr[start:end+1]) max_val = max(arr[start:end+1]) max_len = max(max_len, end - start + 1) return max_len # Drive function to read input and call the core function if __name__ == "__main__": import sys input = sys.stdin.read data = input().split() N = int(data[0]) D = int(data[1]) arr = list(map(int, data[2:])) print(longest_subarray_length(arr, N, D))

question:def is_prime(n: int) -> bool: Check if a number is prime >>> is_prime(1) False >>> is_prime(2) True >>> is_prime(3) True >>> is_prime(4) False def is_tree_beautiful(h: int) -> str: Determines if a perfect binary tree of height h is beautiful. A tree is beautiful if the number of non-leaf nodes is a prime number. >>> is_tree_beautiful(1) "NO" >>> is_tree_beautiful(2) "YES" >>> is_tree_beautiful(3) "YES" >>> is_tree_beautiful(4) "NO" >>> is_tree_beautiful(10) "NO" >>> is_tree_beautiful(20) "NO"

answer:def is_prime(n): Check if a number is prime if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False i = 5 while i * i <= n: if n % i == 0 or n % (i + 2) == 0: return False i += 6 return True def is_tree_beautiful(h): Determines if a perfect binary tree of height h is beautiful. A tree is beautiful if the number of non-leaf nodes is a prime number. # Number of non-leaf nodes in a perfect binary tree of height h # The number of nodes at level i is 2^i # The total number of nodes in a perfect binary tree of height h is 2^(h+1) - 1 # The number of leaf nodes is 2^h # So, the number of non-leaf nodes is (2^(h+1) - 1) - 2^h = 2^h - 1 num_non_leaf_nodes = 2**h - 1 return "YES" if is_prime(num_non_leaf_nodes) else "NO"

Released under the chat License.

has loaded