Appearance
question:def remove_duplicates(head): Remove duplicates from a sorted linked list. Args: head (ListNode): Head of the sorted linked list. Returns: ListNode: Head of the modified linked list with duplicates removed. current = head # Iterate through the list while current and current.next: if current.val == current.next.val: # Skip the next node because it's a duplicate current.next = current.next.next else: # Move to the next node current = current.next return head
answer:class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def remove_duplicates(head): Remove duplicates from a sorted linked list. Args: head (ListNode): Head of the sorted linked list. Returns: ListNode: Head of the modified linked list with duplicates removed. current = head # Iterate through the list while current and current.next: if current.val == current.next.val: # Skip the next node because it's a duplicate current.next = current.next.next else: # Move to the next node current = current.next return head
question:class ListNode: def __init__(self, x): self.val = x self.next = None def detect_and_remove_cycle(head: ListNode) -> ListNode: Detects a cycle in a linked list and removes the node causing the cycle. Returns the head node of the modified list. Example: >>> head = ListNode(3) >>> head.next = ListNode(2) >>> head.next.next = ListNode(0) >>> head.next.next.next = ListNode(-4) >>> head.next.next.next.next = head.next >>> detect_and_remove_cycle(head) # Output: head -> 3 -> 2 -> 0 -> -4 >>> head = ListNode(1) >>> head.next = ListNode(2) >>> head.next.next = head >>> detect_and_remove_cycle(head) # Output: head -> 1 -> 2 >>> head = ListNode(1) >>> detect_and_remove_cycle(head) # Output: head -> 1
answer:class ListNode: def __init__(self, x): self.val = x self.next = None def detect_and_remove_cycle(head: ListNode) -> ListNode: if not head or not head.next: return head slow, fast = head, head cycle_detected = False # Detect cycle using Floyd's Tortoise and Hare algorithm while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: cycle_detected = True break if not cycle_detected: return head # Find the start node of the cycle slow = head while slow != fast: slow = slow.next fast = fast.next # To remove the cycle, find the node just before the start of the cycle # and set its next to None iterator = slow while iterator.next != slow: iterator = iterator.next iterator.next = None return head
question:def sort_by_frequency(arr: list[int]) -> list[int]: Sort elements by frequency in descending order. Parameters ---------- arr: list of integers. A list containing integers. Returns ------- list of integers: A list of integers sorted based on the frequency of occurrence in descending order. If two elements have the same frequency, the one that appears first in the array should come first. pass # Test cases def test_empty_array(): assert sort_by_frequency([]) == [] def test_single_element_array(): assert sort_by_frequency([42]) == [42] def test_multiple_elements_with_same_frequency(): assert sort_by_frequency([4, 5, 6, 5, 4, 3]) == [4, 4, 5, 5, 6, 3] def test_frequency_and_order(): assert sort_by_frequency([1, 2, 3, 4, 4, 5, 5, 5]) == [5, 5, 5, 4, 4, 1, 2, 3] def test_negative_numbers(): assert sort_by_frequency([-1, -2, -1, -2, 3]) == [-1, -1, -2, -2, 3] def test_single_high_frequency_element(): assert sort_by_frequency([1, 1, 1, 1, 2, 3, 4]) == [1, 1, 1, 1, 2, 3, 4] def test_mixed_sign_numbers(): assert sort_by_frequency([1, -1, 1, -1, 2, 2, 2, 3, -1, -1]) == [-1, -1, -1, -1, 2, 2, 2, 1, 1, 3]
answer:def sort_by_frequency(arr): Sort elements by frequency in descending order. Parameters ---------- arr: list of integers. A list containing integers. Returns ------- list of integers: A list of integers sorted based on the frequency of occurrence in descending order. If two elements have the same frequency, the one that appears first in the array should come first. from collections import Counter if not arr: return [] # Count frequency of each element freq = Counter(arr) # Sort based on frequency and first appearance in the array sorted_arr = sorted(arr, key=lambda x: (-freq[x], arr.index(x))) return sorted_arr
question:def is_point_in_circle(cx: int, cy: int, radius: int, px: int, py: int) -> bool: Check if the circle with center at (cx, cy) and radius `radius` contains the point (px, py). >>> is_point_in_circle(0, 0, 5, 3, 4) True >>> is_point_in_circle(0, 0, 5, 6, 8) False >>> is_point_in_circle(0, 0, 5, 5, 0) True >>> is_point_in_circle(10, 10, 5, 10, 10) True
answer:def is_point_in_circle(cx: int, cy: int, radius: int, px: int, py: int) -> bool: Check if the circle with center at (cx, cy) and radius `radius` contains the point (px, py). # Calculate the squared distance between the center and the point dist_squared = (px - cx)**2 + (py - cy)**2 # Compare it to the squared radius to avoid floating-point arithmetic return dist_squared <= radius**2