Appearance
question:from typing import List, Tuple def findKthMostFrequent(string: str, k: int) -> str: Write a function that accepts two parameters, i) a string (containing a list of words) and ii) an integer (k). The function should return the k-th most frequent word from the list. If there is a tie (two words with the same frequency), return the one that appeared first in the original list. The words should be compared case-insensitive, but the returned word should maintain its original case. Assume the list will contain at least k unique words. The format of the input string will be 'word word word'. >>> findKthMostFrequent('dog cat dog bird cat dog bird bird', 2) 'bird' >>> findKthMostFrequent('apple orange banana apple orange apple', 2) 'orange' >>> findKthMostFrequent('one two two three three three four four four four', 2) 'three' >>> findKthMostFrequent('cat bat cat bat', 1) 'cat' >>> findKthMostFrequent('apple Apple apple orange ORANGE orange', 1) 'apple' >>> findKthMostFrequent('alpha beta gamma delta', 3) 'gamma' >>> findKthMostFrequent('single', 1) 'single'
answer:def findKthMostFrequent(string, k): from collections import Counter # Split the string into words words = string.split() # Use a Counter to count the frequencies of the words count = Counter(word.lower() for word in words) # Get a list of unique words in the original order unique_words_ordered = [] seen_words = set() for word in words: word_lower = word.lower() if word_lower not in seen_words: seen_words.add(word_lower) unique_words_ordered.append(word) # Sort the unique words first by frequency (descending), then by their first occurrence sorted_words = sorted(unique_words_ordered, key=lambda word: (-count[word.lower()], words.index(word))) # Return the k-th most frequent word maintaining its original case return sorted_words[k-1]
question:def max_deliveries(grid): Given a grid representing delivery destinations, find the maximum deliveries possible in any valid circular route. >>> max_deliveries([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 40 >>> max_deliveries([[1, 2], [3, 4]]) 10 def process_input(input_data): result = [] lines = input_data.strip().split('n') T = int(lines[0]) index = 1 for _ in range(T): N, M = map(int, lines[index].split()) index += 1 grid = [] for i in range(N): row = list(map(int, lines[index].split())) index += 1 grid.append(row) result.append(max_deliveries(grid)) for res in result: print(res)
answer:def max_deliveries(grid): N = len(grid) M = len(grid[0]) # DP table for down-right path down_right = [[0] * M for _ in range(N)] down_right[0][0] = grid[0][0] for i in range(N): for j in range(M): if i == 0 and j == 0: continue if i == 0: down_right[i][j] = down_right[i][j-1] + grid[i][j] elif j == 0: down_right[i][j] = down_right[i-1][j] + grid[i][j] else: down_right[i][j] = max(down_right[i-1][j], down_right[i][j-1]) + grid[i][j] # Total point when going through bottom-right corner total_deliveries = down_right[-1][-1] * 2 - grid[0][0] - grid[-1][-1] return total_deliveries def process_input(input_data): result = [] lines = input_data.strip().split('n') T = int(lines[0]) index = 1 for _ in range(T): N, M = map(int, lines[index].split()) index += 1 grid = [] for i in range(N): row = list(map(int, lines[index].split())) index += 1 grid.append(row) result.append(max_deliveries(grid)) for res in result: print(res)
question:from typing import List def wave_array(arr: List[int]) -> List[int]: Transforms the array into a "wave" array. An array is defined as being in a "wave" form if each even-indexed element is greater than or equal to its adjacent odd-indexed elements. >>> wave_array([3, 6, 5, 10, 7, 20]) [6, 3, 10, 5, 20, 7] >>> wave_array([1, 2, 3, 4, 5, 6]) [2, 1, 4, 3, 6, 5]
answer:from typing import List def wave_array(arr: List[int]) -> List[int]: Transforms the array into a "wave" array. n = len(arr) for i in range(0, n, 2): # If the current even-indexed element is less than the previous odd-indexed element if i > 0 and arr[i] < arr[i - 1]: arr[i], arr[i - 1] = arr[i - 1], arr[i] # If the current even-indexed element is less than the next odd-indexed element if i < n - 1 and arr[i] < arr[i + 1]: arr[i], arr[i + 1] = arr[i + 1], arr[i] return arr
question:def is_leap_year(year: int) -> bool: Determine if a given year is a leap year. A leap year is exactly divisible by 4, except for years which are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. Args: year (int): the year to check Returns: bool: True if the year is a leap year, False otherwise Examples: >>> is_leap_year(2000) True >>> is_leap_year(1996) True >>> is_leap_year(1900) False >>> is_leap_year(2021) False
answer:def is_leap_year(year): Determine if a given year is a leap year. Args: year (int): the year to check Returns: bool: True if the year is a leap year, False otherwise if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): return True else: return False