Skip to content
🤔prompts chat🧠
🔍
question:def binary_sum(a: str, b: str) -> str: Returns the sum of two binary strings a and b. The function manually handles binary addition without using in-built libraries. >>> binary_sum("1101", "101") == "10010" >>> binary_sum("111", "111") == "1110" >>> binary_sum("0", "0") == "0" >>> binary_sum("1", "1") == "10" >>> binary_sum("10110", "110") == "11100" >>> binary_sum("1010", "0101") == "1111" >>> binary_sum("1111", "1111") == "11110"

answer:def binary_sum(a: str, b: str) -> str: Returns the sum of two binary strings a and b. max_len = max(len(a), len(b)) a = a.zfill(max_len) b = b.zfill(max_len) result = [] carry = 0 for i in range(max_len-1, -1, -1): bit_sum = carry bit_sum += 1 if a[i] == '1' else 0 bit_sum += 1 if b[i] == '1' else 0 result.append('1' if bit_sum % 2 == 1 else '0') carry = 0 if bit_sum < 2 else 1 if carry != 0: result.append('1') result.reverse() return ''.join(result)

question:def can_distribute_evenly(D: int, E: int, departments: List[List[int]]) -> str: Determines if it's possible to evenly distribute employees such that each department has the same total skill level. Args: D (int): Number of departments. E (int): Total number of employees. departments (List[List[int]]): A list of lists where each sublist contains the skill levels of employees in each department. Returns: str: "YES" if it is possible to reorder employees to have the same total skill level per department, otherwise "NO". Examples: >>> can_distribute_evenly(3, 6, [[10, 20, 30], [30, 10, 20], [20, 30, 10]]) "YES" >>> can_distribute_evenly(3, 4, [[4, 2], [2, 4], [4, 4, 2]]) "NO"

answer:def can_distribute_evenly(D, E, departments): Determines if it's possible to evenly distribute employees such that each department has the same total skill level. total_skill_level = sum(sum(dept) for dept in departments) if total_skill_level % D != 0: return "NO" target_skill_level = total_skill_level // D def can_balance(skill_levels, target, count): if count == 1: return sum(skill_levels) == target if not skill_levels: return False for i in range(1 << len(skill_levels)): subset_sum = sum(skill_levels[j] for j in range(len(skill_levels)) if (i & (1 << j))) if subset_sum == target: remaining = [skill_levels[j] for j in range(len(skill_levels)) if not (i & (1 << j))] if can_balance(remaining, target, count - 1): return True return False all_skills = [skill for dept in departments for skill in dept] return "YES" if can_balance(all_skills, target_skill_level, D) else "NO"

question:def is_palindrome(s: str) -> bool: Checks if a given string s is a palindrome. >>> is_palindrome("abccba") True >>> is_palindrome("abc") False >>> is_palindrome("a") True >>> is_palindrome("aa") True def can_be_divided_into_three_palindromic_parts(s: str) -> str: Determines if a given string s can be divided into three non-empty palindromic parts. >>> can_be_divided_into_three_palindromic_parts("abacaba") "YES" >>> can_be_divided_into_three_palindromic_parts("abccba") "YES" >>> can_be_divided_into_three_palindromic_parts("abcd") "NO" >>> can_be_divided_into_three_palindromic_parts("aabbaa") "YES" >>> can_be_divided_into_three_palindromic_parts("a") "NO" def process_test_cases(t: int, test_cases: List[str]) -> List[str]: Processes multiple test cases and returns the results. >>> process_test_cases(3, ["abacaba", "abccba", "abcd"]) ['YES', 'YES', 'NO']

answer:def is_palindrome(s): Checks if a given string s is a palindrome. return s == s[::-1] def can_be_divided_into_three_palindromic_parts(s): Determines if a given string s can be divided into three non-empty palindromic parts. n = len(s) for i in range(1, n - 1): for j in range(i + 1, n): part1 = s[:i] part2 = s[i:j] part3 = s[j:] if is_palindrome(part1) and is_palindrome(part2) and is_palindrome(part3): return "YES" return "NO" def process_test_cases(t, test_cases): Processes multiple test cases and returns the results. results = [] for s in test_cases: results.append(can_be_divided_into_three_palindromic_parts(s)) return results

question:class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None def build_tree(node_map): Build the binary tree from the given node map. pass def lowest_common_ancestor(root, p, q): Find the lowest common ancestor (LCA) of two nodes in a binary tree. >>> lowest_common_ancestor(root, 5, 1) 3 >>> lowest_common_ancestor(root, 7, 4) 2 pass def find_lca(n, tree_data, p, q): Find the LCA from the given tree data and nodes. >>> find_lca(9, [(3, 5, 1), (5, 6, 2), (1, 0, 8), (6, -1, -1), (2, 7, 4), (0, -1, -1), (8, -1, -1), (7, -1, -1), (4, -1, -1)], 5, 1) 3 >>> find_lca(9, [(3, 5, 1), (5, 6, 2), (1, 0, 8), (6, -1, -1), (2, 7, 4), (0, -1, -1), (8, -1, -1), (7, -1, -1), (4, -1, -1)], 7, 4) 2 pass

answer:class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None def build_tree(node_map): if not node_map: return None nodes = {val: TreeNode(val) for val in node_map} for val, (left, right) in node_map.items(): if left != -1: nodes[val].left = nodes[left] if right != -1: nodes[val].right = nodes[right] return nodes def lowest_common_ancestor(root, p, q): if not root or root.val == p or root.val == q: return root left = lowest_common_ancestor(root.left, p, q) right = lowest_common_ancestor(root.right, p, q) if left and right: return root return left if left else right def find_lca(n, tree_data, p, q): node_map = {} for data in tree_data: x, y, z = data node_map[x] = (y, z) nodes = build_tree(node_map) root = next(iter(nodes.values())) # Get any node to start with, as all are connected. lca_node = lowest_common_ancestor(root, p, q) return lca_node.val if lca_node else None

Released under the chat License.

has loaded