Skip to content
🤔prompts chat🧠
🔍
question:def second_largest_unique(numbers: List[int]) -> Optional[int]: Returns the second largest unique number in the list. If the list contains less than two unique numbers, returns None. >>> second_largest_unique([10, 4, 10, 1, 4, -1, -3, 3]) == 4 >>> second_largest_unique([10]) == None >>> second_largest_unique([]) == None >>> second_largest_unique([5, 5, 5, 5]) == None >>> second_largest_unique([-10, -2, -3, -1, -4]) == -2 >>> second_largest_unique([5, -5, 5, -10, 10]) == 5 >>> second_largest_unique([1, 2, 2, 3, 3, 4, 4, 5, 5]) == 4 >>> second_largest_unique([10, 5, 10, 5]) == 5

answer:def second_largest_unique(numbers): Returns the second largest unique number in the list. If less than two unique numbers are present, returns None. if len(numbers) < 2: return None unique_numbers = list(set(numbers)) # Remove duplicates unique_numbers.sort(reverse=True) # Sort in descending order if len(unique_numbers) < 2: return None return unique_numbers[1]

question:def look_and_say_sequence(s: str, n: int) -> str: Generate the nth term of the look-and-say sequence starting with the string s. :param s: The starting string of the sequence. :param n: The number of terms to generate. :return: The nth term in the look-and-say sequence. >>> look_and_say_sequence("1", 1) '1' >>> look_and_say_sequence("1", 2) '11' >>> look_and_say_sequence("1", 3) '21' >>> look_and_say_sequence("1", 4) '1211' >>> look_and_say_sequence("1", 5) '111221' >>> look_and_say_sequence("2", 1) '2' >>> look_and_say_sequence("2", 2) '12' >>> look_and_say_sequence("1", 6) '312211' # Your code here

answer:def look_and_say_sequence(s, n): Generate the nth term of the look-and-say sequence starting with the string s. :param s: The starting string of the sequence. :param n: The number of terms to generate. :return: The nth term in the look-and-say sequence. def next_term(s): result = [] i = 0 while i < len(s): count = 1 while i + 1 < len(s) and s[i] == s[i + 1]: i += 1 count += 1 result.append(f"{count}{s[i]}") i += 1 return ''.join(result) current_term = s for _ in range(n - 1): current_term = next_term(current_term) return current_term

question:def two_sum_sorted(lst, target): ''' Finds two distinct indices in a sorted list such that their values add up to the target. Parameters: lst (list): A sorted list of integers. target (int): The target sum. Returns: tuple: A tuple containing two distinct indices, or an empty tuple if no appropriate pair exists. >>> two_sum_sorted([1, 2, 3, 4, 5], 9) (3, 4) >>> two_sum_sorted([1, 2, 3, 4, 5], 5) (0, 3) >>> two_sum_sorted([1, 2, 3, 4, 5], 10) () >>> two_sum_sorted([1, 2, 3, 4, 5], 1) () >>> two_sum_sorted([], 5) () >>> two_sum_sorted([2], 2) () >>> large_list = list(range(1, 1000001)) >>> two_sum_sorted(large_list, 1999999) (999998, 999999) '''

answer:def two_sum_sorted(lst, target): ''' Finds two distinct indices in a sorted list such that their values add up to the target. Parameters: lst (list): A sorted list of integers. target (int): The target sum. Returns: tuple: A tuple containing two distinct indices, or an empty tuple if no appropriate pair exists. ''' left, right = 0, len(lst) - 1 while left < right: current_sum = lst[left] + lst[right] if current_sum == target: return (left, right) elif current_sum < target: left += 1 else: right -= 1 return ()

question:class ConferenceRoomBooking: A class to simulate a booking system for a small conference room. This system allows users to book the room for specific time slots throughout a day, check for available time slots, cancel existing bookings, and view all bookings for a particular day. Example usage: >>> crb = ConferenceRoomBooking() >>> crb.book_slot("2023-10-01", 9, 12) True >>> crb.get_schedule("2023-10-01") [(9, 12)] >>> crb.book_slot("2023-10-01", 11, 13) False >>> crb.cancel_booking("2023-10-01", 9, 12) True >>> crb.get_schedule("2023-10-01") [] def __init__(self): Initialize the ConferenceRoomBooking with an empty bookings dictionary. def is_available(self, day: str, start_time: int, end_time: int) -> bool: Check if the time slot is available on the given day. def book_slot(self, day: str, start_time: int, end_time: int) -> bool: Book a slot if it's available. def cancel_booking(self, day: str, start_time: int, end_time: int) -> bool: Cancel a booking if it exists. def get_schedule(self, day: str) -> list: Get all bookings for the given day.

answer:class ConferenceRoomBooking: def __init__(self): # Dictionary to hold bookings, keyed by day. self.bookings = {} def is_available(self, day, start_time, end_time): Check if the time slot is available on the given day. if day not in self.bookings: return True for booking in self.bookings[day]: if not (end_time <= booking[0] or start_time >= booking[1]): return False return True def book_slot(self, day, start_time, end_time): Book a slot if it's available. if self.is_available(day, start_time, end_time): if day not in self.bookings: self.bookings[day] = [] self.bookings[day].append((start_time, end_time)) return True return False def cancel_booking(self, day, start_time, end_time): Cancel a booking if it exists. if day in self.bookings: if (start_time, end_time) in self.bookings[day]: self.bookings[day].remove((start_time, end_time)) return True return False def get_schedule(self, day): Get all bookings for the given day. return self.bookings[day] if day in self.bookings else []

Released under the chat License.

has loaded