Appearance
question:class Node: def __init__(self, value): self.value = value self.next = None class Queue: A Queue implementation using a singly linked list. >>> q = Queue() >>> q.is_empty() True >>> q.enqueue(1) >>> q.enqueue(2) >>> q.peek() 1 >>> q.is_empty() False >>> q.dequeue() 1 >>> q.dequeue() 2 >>> try: ... q.dequeue() ... except IndexError: ... pass # Expected behavior, as the queue is empty >>> try: ... q.peek() ... except IndexError: ... pass # Expected behavior, as the queue is empty def __init__(self): pass def enqueue(self, value): pass def dequeue(self): pass def is_empty(self): pass def peek(self): pass if __name__ == "__main__": import doctest doctest.testmod()
answer:class Node: def __init__(self, value): self.value = value self.next = None class Queue: def __init__(self): self.front = None self.rear = None def enqueue(self, value): new_node = Node(value) if self.rear is None: self.front = self.rear = new_node return self.rear.next = new_node self.rear = new_node def dequeue(self): if self.is_empty(): raise IndexError("Dequeue from empty queue") temp = self.front self.front = temp.next if self.front is None: self.rear = None return temp.value def is_empty(self): return self.front is None def peek(self): if self.is_empty(): raise IndexError("Peek from empty queue") return self.front.value
question:def calculate_final_score(scores: list) -> float: Calculates the average of valid scores in the list, rounded to two decimal places. A valid score is between 0 and 100 inclusive. Parameters: scores (list): A list of integers or floats representing the scores. Returns: float: The average of valid scores rounded to two decimal places. Raises: ValueError: If there are no valid scores in the list. Examples: >>> calculate_final_score([95, 85, 92, 88, 76]) 87.20 >>> calculate_final_score([100, 100, 100, 0, 0, 0]) 50.00 >>> calculate_final_score([-10, 105, 80, 90]) 85.00 >>> calculate_final_score([45.5, 78.3, 86.0]) 69.93 >>> calculate_final_score([55]) 55.00 >>> calculate_final_score([-10, 105, 101, -5]) ValueError: No valid scores available.
answer:def calculate_final_score(scores): Calculates the average of valid scores in the list, rounded to two decimal places. A valid score is between 0 and 100 inclusive. Parameters: scores (list): A list of integers or floats representing the scores. Returns: float: The average of valid scores rounded to two decimal places. Raises: ValueError: If there are no valid scores in the list. valid_scores = [score for score in scores if 0 <= score <= 100] if not valid_scores: raise ValueError("No valid scores available.") average_score = sum(valid_scores) / len(valid_scores) return round(average_score, 2)
question:def reconstruct_itinerary(tickets: List[Tuple[str, str]]) -> List[str]: Reconstruct the itinerary from flight tickets in lexicographical order starting from 'JFK'. >>> reconstruct_itinerary([("MUC", "LHR"), ("JFK", "MUC"), ("SFO", "SJC"), ("LHR", "SFO")]) ["JFK", "MUC", "LHR", "SFO", "SJC"] >>> reconstruct_itinerary([("JFK", "SFO"), ("JFK", "ATL"), ("SFO", "ATL"), ("ATL", "JFK"), ("ATL", "SFO")]) ["JFK", "ATL", "JFK", "SFO", "ATL", "SFO"] # Example Usage: tickets = [("MUC", "LHR"), ("JFK", "MUC"), ("SFO", "SJC"), ("LHR", "SFO")] print(reconstruct_itinerary(tickets)) # Expected Output: ["JFK", "MUC", "LHR", "SFO", "SJC"] tickets = [("JFK", "SFO"), ("JFK", "ATL"), ("SFO", "ATL"), ("ATL", "JFK"), ("ATL", "SFO")] print(reconstruct_itinerary(tickets)) # Expected Output: ["JFK", "ATL", "JFK", "SFO", "ATL", "SFO"]
answer:from typing import List, Tuple from collections import defaultdict, deque def reconstruct_itinerary(tickets: List[Tuple[str, str]]) -> List[str]: graph = defaultdict(deque) # Create the graph, sorting each adjacency list lexicographically for start, end in sorted(tickets): graph[start].append(end) itinerary = [] def dfs(airport): while graph[airport]: next_airport = graph[airport].popleft() dfs(next_airport) itinerary.append(airport) # Start the journey from 'JFK' dfs('JFK') # Since we add airports after visiting the end of their edges, reverse the result return itinerary[::-1]
question:def min_coins(coins: list[int], sum_amount: int) -> int: Calculate the minimum number of coins required to make the given sum. Args: - coins (list[int]): Available denominations of coins. - sum_amount (int): Target sum to be made using the coins. Returns: - int: Minimum number of coins needed to reach the given sum. >>> min_coins([1, 2, 5], 11) 3 >>> min_coins([2], 3) -1 pass def get_coin_combination(coins: list[int], sum_amount: int) -> list[int]: Retrieve the combination of coins that make up the given sum using the minimum number of coins. Args: - coins (list[int]): Available denominations of coins. - sum_amount (int): Target sum to be made using the coins. Returns: - list[int]: A list of coins that sums up to the given sum using the minimum number of coins. >>> sorted(get_coin_combination([1, 2, 5], 11)) [1, 5, 5] >>> get_coin_combination([2], 3) [] pass
answer:def min_coins(coins, sum_amount): Calculate the minimum number of coins required to make the given sum. Args: - coins (list[int]): Available denominations of coins. - sum_amount (int): Target sum to be made using the coins. Returns: - int: Minimum number of coins needed to reach the given sum, or -1 if it is not possible. dp = [float('inf')] * (sum_amount + 1) dp[0] = 0 # Zero coins needed to make sum 0 for coin in coins: for x in range(coin, sum_amount + 1): dp[x] = min(dp[x], dp[x - coin] + 1) return dp[sum_amount] if dp[sum_amount] != float('inf') else -1 def get_coin_combination(coins, sum_amount): Retrieve the combination of coins that make up the given sum using the minimum number of coins. Args: - coins (list[int]): Available denominations of coins. - sum_amount (int): Target sum to be made using the coins. Returns: - list[int]: A list of coins that sums up to the given sum using the minimum number of coins, or an empty list if it is not possible. dp = [float('inf')] * (sum_amount + 1) dp[0] = 0 # Zero coins needed to make sum 0 backtrack = [-1] * (sum_amount + 1) for coin in coins: for x in range(coin, sum_amount + 1): if dp[x - coin] + 1 < dp[x]: dp[x] = dp[x - coin] + 1 backtrack[x] = coin if dp[sum_amount] == float('inf'): return [] result = [] while sum_amount > 0: coin = backtrack[sum_amount] result.append(coin) sum_amount -= coin return result