Skip to content
🤔prompts chat🧠
🔍
question:def is_subsequence(s: str, word: str) -> bool: Check if `word` is a subsequence of `s`. >>> is_subsequence("abcde", "a") True >>> is_subsequence("abcde", "ace") True >>> is_subsequence("abcde", "acd") True >>> is_subsequence("abcde", "bb") False def num_matching_subseq(s: str, words: List[str]) -> int: Returns the count of words that are subsequences of `s`. >>> num_matching_subseq("abcde", ["a", "bb", "acd", "ace"]) 3 >>> num_matching_subseq("abc", ["a", "b", "c", "ab", "ac", "bc", "abc"]) 7 >>> num_matching_subseq("test", ["t", "e", "st", "te", "es", "tes"]) 6 >>> num_matching_subseq("xyz", ["x", "y", "z", "xy", "xz", "yz", "xyz", "yza"]) 7 from solution import is_subsequence, num_matching_subseq def test_is_subsequence(): assert is_subsequence("abcde", "a") == True assert is_subsequence("abcde", "ace") == True assert is_subsequence("abcde", "acd") == True assert is_subsequence("abcde", "bb") == False assert is_subsequence("abcdefg", "abcdg") == True assert is_subsequence("abcdefg", "gfedcba") == False def test_num_matching_subseq_example_1(): s = "abcde" words = ["a", "bb", "acd", "ace"] assert num_matching_subseq(s, words) == 3 def test_num_matching_subseq_example_2(): s = "abc" words = ["a", "b", "c", "ab", "ac", "bc", "abc"] assert num_matching_subseq(s, words) == 7 def test_num_matching_subseq_example_3(): s = "test" words = ["t", "e", "st", "te", "es", "tes"] assert num_matching_subseq(s, words) == 6 def test_num_matching_subseq_example_4(): s = "xyz" words = ["x", "y", "z", "xy", "xz", "yz", "xyz", "yza"] assert num_matching_subseq(s, words) == 7 def test_edge_cases(): s = "a" words = ["a", "aa", "aaa"] assert num_matching_subseq(s, words) == 1 s = "abcdef" words = ["f", "ef", "def", "cdef"] assert num_matching_subseq(s, words) == 4 s = "mnop" words = ["m", "n", "o", "p", "mn", "mo", "mp", "mnop"] assert num_matching_subseq(s, words) == 8

answer:def is_subsequence(s, word): Returns True if word is a subsequence of s. it = iter(s) return all(char in it for char in word) def num_matching_subseq(s, words): Returns the count of words that are subsequences of s. return sum(is_subsequence(s, word) for word in words)

question:from collections import defaultdict, Counter from typing import List, Tuple, Dict def find_most_frequent_product(t: int, test_cases: List[Dict[str, List[Tuple[int, int]]]]) -> List[int]: Find the most frequently clicked product in a given range of click events. t: int - the number of test cases test_cases: List[Dict[str, List[Union[int, List[Tuple[int, int]]]]]] - description of each test case For each test case: - n: int - the number of products - q: int - the number of queries - products: List[int] - the identifiers of the products clicked by the users in sequence - queries: List[Tuple[int, int]] - each tuple contains two integers l and r, describing the subsequence of click events for the query Returns: List[int] - For each query in each test case, a single integer that is the most frequently clicked product in the specified subsequence. Example: >>> t = 1 >>> cases = [ >>> { >>> "n": 10, >>> "q": 2, >>> "products": [3, 1, 2, 2, 3, 3, 4, 4, 4, 1], >>> "queries": [(1, 5), (2, 10)] >>> } >>> ] >>> find_most_frequent_product(t, cases) [2, 4]

answer:from collections import defaultdict, Counter def find_most_frequent_product(t, test_cases): results = [] for test in test_cases: n, q, products, queries = test["n"], test["q"], test["products"], test["queries"] for query in queries: l, r = query subsequence = products[l-1:r] frequency_counter = Counter(subsequence) most_frequent = min(frequency_counter.items(), key=lambda x: (-x[1], x[0]))[0] results.append(most_frequent) return results

question:from typing import List def min_operations_to_equalize_array(n: int, a: List[int]) -> int: Returns the minimum number of operations required to make all elements of the array equal. Args: n : int : the length of the array a : List[int] : the elements of the array Returns: int : the minimum number of operations required Examples: >>> min_operations_to_equalize_array(5, [1, 2, 3, 4, 5]) 4 >>> min_operations_to_equalize_array(4, [10, 10, 10, 10]) 0 >>> min_operations_to_equalize_array(6, [4, 4, 4, 4, 4, 4]) 0 >>> min_operations_to_equalize_array(3, [1, 1, 2]) 1

answer:def min_operations_to_equalize_array(n, a): Returns the minimum number of operations required to make all elements of the array equal. Args: n : int : the length of the array a : List[int] : the elements of the array Returns: int : the minimum number of operations required # The number of operations required is the difference between the maximum value and the minimum value in the array. return max(a) - min(a)

question:from collections import Counter def can_form_palindrome(s: str) -> bool: Determines if the characters in the string `s` can be rearranged to form a palindrome. :param s: A string consisting of lowercase English letters :return: True if the input string can be rearranged to form a palindrome, otherwise False >>> can_form_palindrome("civic") True >>> can_form_palindrome("ivicc") True >>> can_form_palindrome("hello") False >>> can_form_palindrome("a") True >>> can_form_palindrome("ab") False >>> can_form_palindrome("aabb") True >>> can_form_palindrome("aabbc") True >>> can_form_palindrome("abcdefghijklmnopqrstuvwxyz") False

answer:from collections import Counter def can_form_palindrome(s): Determines if the characters in the string `s` can be rearranged to form a palindrome. :param s: A string consisting of lowercase English letters :return: True if the input string can be rearranged to form a palindrome, otherwise False # Count occurrences of each character char_count = Counter(s) # A string can form a palindrome if it has at most one character with an odd count odd_counts = sum(1 for count in char_count.values() if count % 2 != 0) return odd_counts <= 1

Released under the chat License.

has loaded