Skip to content
🤔prompts chat🧠
🔍
question:def findLongestConcatenatedWord(words): Given an array of words where each word is represented as a string, find the longest concatenated word in the list such that it can be entirely constructed by concatenating the other words from the list. If more than one such longest word exists, return the first one found. If no concatenated word exists, return an empty string. Args: words: List[str] - List of words Returns: str - The longest concatenated word (or first one found if multiple exist), or an empty string Example: >>> findLongestConcatenatedWord(["cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"]) 'ratcatdogcat' >>> findLongestConcatenatedWord(["cat", "dog", "catdog"]) 'catdog' pass # Unit Tests def test_example_1(): assert findLongestConcatenatedWord(["cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"]) == "ratcatdogcat" def test_example_2(): assert findLongestConcatenatedWord(["cat", "dog", "catdog"]) == "catdog" def test_no_concatenated_word(): assert findLongestConcatenatedWord(["a", "b", "c"]) == "" def test_two_longest_words(): assert findLongestConcatenatedWord(["cat", "dog", "catdog", "dogcat"]) == "catdog" def test_single_word(): assert findLongestConcatenatedWord(["a"]) == "" def test_words_with_same_length(): assert findLongestConcatenatedWord(["abab", "ab", "ab", "cdcd", "cdcdcdcd"]) == "cdcdcdcd" def test_prefix_cases(): assert findLongestConcatenatedWord(["star", "fish", "starfish", "starfishfish", "starfishfishstar"]) == "starfishfishstar"

answer:def findLongestConcatenatedWord(words): words_set = set(words) def can_form(word, original_word): if word in words_set and word != original_word: return True for i in range(1, len(word)): prefix, suffix = word[:i], word[i:] if prefix in words_set and suffix != original_word and can_form(suffix, original_word): return True return False words.sort(key=lambda x: len(x), reverse=True) for word in words: if can_form(word, word): return word return ""

question:def smallest_diff_for_queries(T, test_cases): Takes the number of test cases and the test case data, computes smallest performance difference for specified employee pairs. Args: T (int): Number of test cases. test_cases (list): List of tuples where each tuple contains: - N (int): Number of employees. - M (int): Number of projects. - performance_ratings (list of list of int): The performance ratings for each employee. - Q (int): Number of queries. - queries (list of tuple of int): List of tuples where each tuple contains pair of employee indices. Returns: list: List of smallest performance differences for each query of each test case. pass # Test cases from solution import smallest_diff_for_queries def test_single_test_case(): T = 1 test_cases = [ (3, 4, [ [20, 30, 40, 50], [25, 35, 45, 55], [10, 20, 30, 40] ], 2, [ (1, 2), (1, 3) ]) ] result = smallest_diff_for_queries(T, test_cases) assert result == [5, 10] def test_multiple_test_cases(): T = 2 test_cases = [ (3, 4, [ [20, 30, 40, 50], [25, 35, 45, 55], [10, 20, 30, 40] ], 2, [ (1, 2), (1, 3) ]), (2, 3, [ [50, 60, 70], [55, 65, 75] ], 1, [ (1, 2) ]) ] result = smallest_diff_for_queries(T, test_cases) assert result == [5, 10, 5] def test_no_difference(): T = 1 test_cases = [ (2, 3, [ [20, 20, 20], [20, 20, 20] ], 1, [ (1, 2) ]) ] result = smallest_diff_for_queries(T, test_cases) assert result == [0] def test_large_difference(): T = 1 test_cases = [ (3, 3, [ [0, 100, 50], [100, 0, 50], [50, 50, 50] ], 2, [ (1, 2), (1, 3) ]) ] result = smallest_diff_for_queries(T, test_cases) assert result == [0, 0] def test_single_project(): T = 1 test_cases = [ (3, 1, [ [20], [25], [10] ], 2, [ (1, 2), (1, 3) ]) ] result = smallest_diff_for_queries(T, test_cases) assert result == [5, 10]

answer:def smallest_diff_for_queries(T, test_cases): Takes the number of test cases and the test case data, computes smallest performance difference for specified employee pairs. results = [] for i in range(T): case = test_cases[i] N, M, performance_ratings, Q, queries = case for u, v in queries: u -= 1 # Convert 1-based index to 0-based v -= 1 # Convert 1-based index to 0-based min_diff = float('inf') for m in range(M): diff = abs(performance_ratings[u][m] - performance_ratings[v][m]) if diff < min_diff: min_diff = diff results.append(min_diff) return results # Example usage T = 1 test_cases = [ (3, 4, [ [20, 30, 40, 50], [25, 35, 45, 55], [10, 20, 30, 40] ], 2, [ (1, 2), (1, 3) ]) ] print(smallest_diff_for_queries(T, test_cases))

question:def is_rotation(s1: str, s2: str) -> bool: Determines if s1 is a rotation of s2. >>> is_rotation("waterbottle", "erbottlewat") True >>> is_rotation("hello", "ohell") True >>> is_rotation("hello", "world") False >>> is_rotation("abc", "cab") True >>> is_rotation("abc", "bca") True

answer:def is_rotation(s1: str, s2: str) -> bool: Determines if s1 is a rotation of s2. if len(s1) != len(s2): return False concatenated = s1 + s1 return s2 in concatenated

question:class ScoreManager: A class to manage and query scores of students efficiently. >>> sm = ScoreManager() >>> sm.add(10) >>> sm.add(20) >>> sm.scores [10, 20] >>> sm.remove(10) >>> sm.scores [20] >>> sm.find(20) 1 >>> sm.find(10) 0 >>> sm.min() 20 >>> sm.max() 20 >>> sm.add(30) >>> sm.average() 25 >>> sm.count() 2 >>> sm.remove(20) >>> sm.min() 30 def __init__(self): self.scores = [] def add(self, score): pass def remove(self, score): pass def find(self, score): pass def min(self): pass def max(self): pass def average(self): pass def count(self): pass

answer:class ScoreManager: def __init__(self): self.scores = [] def add(self, score): self.scores.append(score) def remove(self, score): if score in self.scores: self.scores.remove(score) def find(self, score): return 1 if score in self.scores else 0 def min(self): return min(self.scores) if self.scores else "EMPTY" def max(self): return max(self.scores) if self.scores else "EMPTY" def average(self): return sum(self.scores) // len(self.scores) if self.scores else "EMPTY" def count(self): return len(self.scores)

Released under the chat License.

has loaded