Skip to content
🤔prompts chat🧠
🔍
question:from typing import List def has_cycle(n: int, adjacency_matrix: List[List[int]]) -> str: Determines if the undirected graph contains a cycle. Parameters: n (int): number of vertices in the graph. adjacency_matrix (list of list of int): The adjacency matrix of the graph. Returns: str: "Yes" if the graph contains a cycle, "No" otherwise. pass # Unit Test def test_cycle_in_graph(): n = 5 adjacency_matrix = [ [0, 1, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 0, 0, 0] ] assert has_cycle(n, adjacency_matrix) == "Yes" def test_no_cycle_in_graph(): n = 3 adjacency_matrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ] assert has_cycle(n, adjacency_matrix) == "No" def test_single_node_graph(): n = 1 adjacency_matrix = [ [0] ] assert has_cycle(n, adjacency_matrix) == "No" def test_disconnected_graph_with_cycle(): n = 6 adjacency_matrix = [ [0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 0, 1], [0, 0, 0, 1, 1, 0] ] assert has_cycle(n, adjacency_matrix) == "Yes" def test_empty_graph(): n = 2 adjacency_matrix = [ [0, 0], [0, 0] ] assert has_cycle(n, adjacency_matrix) == "No"

answer:def has_cycle(n, adjacency_matrix): Determines if the undirected graph contains a cycle. Parameters: n (int): number of vertices in the graph. adjacency_matrix (list of list of int): The adjacency matrix of the graph. Returns: str: "Yes" if the graph contains a cycle, "No" otherwise. visited = [False] * n def dfs(v, parent): visited[v] = True for neigh in range(n): if adjacency_matrix[v][neigh] == 1: if not visited[neigh]: if dfs(neigh, v): return True elif neigh != parent: return True return False for i in range(n): if not visited[i]: if dfs(i, -1): return "Yes" return "No"

question:def is_palindrome(s: str) -> bool: Determine whether the given string is a palindrome, considering only alphanumeric characters and ignoring cases. Args: s (str): Input string. Returns: bool: True if the string is a palindrome, False otherwise. Examples: >>> is_palindrome("A man, a plan, a canal: Panama") True >>> is_palindrome("race a car") False

answer:def is_palindrome(s): Determine if the string s is a palindrome, considering only alphanumeric characters and ignoring cases. # Filter out non-alphanumeric characters and convert to lowercase filtered_s = ''.join(char.lower() for char in s if char.isalnum()) # Compare the filtered string with its reverse return filtered_s == filtered_s[::-1]

question:import string def wordCount(text: str) -> dict: Write a function named "wordCount" that takes a single string as input and returns a dictionary where keys are unique words in the string, and values are the number of times each word appears in the string. Consider words as being separated by spaces and ignore case (i.e., treat "Word" and "word" as the same word). Ignore punctuation. >>> wordCount("Hello world hello") {'hello': 2, 'world': 1} >>> wordCount("This is a test. This is only a test.") {'this': 2, 'is': 2, 'a': 2, 'test': 2, 'only': 1}

answer:import string def wordCount(text): Returns a dictionary with unique words as keys and their count as values. Words are separated by spaces and case is ignored. Punctuation is ignored. # Convert text to lowercase and remove punctuation text = text.lower() translator = str.maketrans('', '', string.punctuation) text = text.translate(translator) # Split the text into words words = text.split() # Create a dictionary to hold word counts word_counts = {} for word in words: if word in word_counts: word_counts[word] += 1 else: word_counts[word] = 1 return word_counts

question:def reverse_segments(S: str, N: int) -> str: Given a string S and an integer N, split the string into segments each of length N and reverse each segment independently. pass def process_test_cases(test_cases: List[Tuple[str, int]]) -> List[str]: Process multiple test cases and return the result for each. pass # Example unit tests def test_reverse_segments(): assert reverse_segments("abcdefghij", 5) == "edcbajihgf" assert reverse_segments("abcdefghij", 2) == "badcfehgji" assert reverse_segments("abcdefghi", 3) == "cbafedihg" def test_process_test_cases(): test_cases = [ ("abcdefghij", 5), ("abcdefghij", 2), ("abcdefghi", 3) ] expected_output = [ "edcbajihgf", "badcfehgji", "cbafedihg" ] assert process_test_cases(test_cases) == expected_output if __name__ == "__main__": import pytest pytest.main()

answer:def reverse_segments(S, N): Given a string S and an integer N, split the string into segments each of length N and reverse each segment independently. segments = [S[i:i+N] for i in range(0, len(S), N)] reversed_segments = [segment[::-1] for segment in segments] return ''.join(reversed_segments) def process_test_cases(test_cases): Process multiple test cases and return the result for each. results = [] for S, N in test_cases: results.append(reverse_segments(S, N)) return results # Example execution with provided input format if __name__ == "__main__": import sys input = sys.stdin.read data = input().split() T = int(data[0]) index = 1 test_cases = [] for _ in range(T): S = data[index] N = int(data[index + 1]) test_cases.append((S, N)) index += 2 results = process_test_cases(test_cases) for result in results: print(result)

Released under the chat License.

has loaded