Skip to content
🤔prompts chat🧠
🔍
question:def shortest_subsequence_length(s: str) -> int: Returns the length of the shortest subsequence that contains all the unique characters in the string. >>> shortest_subsequence_length("abac") 3 >>> shortest_subsequence_length("aaaaa") 1 >>> shortest_subsequence_length("aabcabc") 3

answer:def shortest_subsequence_length(s): Returns the length of the shortest subsequence that contains all the unique characters in the string. # A set to keep track of all unique chars in the string unique_chars = set(s) # The length of the shortest subsequence is just the number of unique characters return len(unique_chars)

question:class AccountManager: Implement an AccountManager class that keeps track of bank accounts and their balances. Methods: create(account_id: str, balance: int) -> None: Create a new account with the specified account_id and an initial balance. deposit(account_id: str, amount: int) -> None: Deposit the specified amount into the given account. withdraw(account_id: str, amount: int) -> None: Withdraw the specified amount from the given account. balance(account_id: str) -> Union[int, str]: Return the current balance of the given account or "Account does not exist" if the account does not exist. >>> manager = AccountManager() >>> manager.create("acc123", 500) >>> manager.deposit("acc123", 200) >>> manager.balance("acc123") 700 >>> manager.withdraw("acc123", 100) >>> manager.balance("acc123") 600 >>> manager.balance("acc456") "Account does not exist" def __init__(self): pass def create(self, account_id, balance): pass def deposit(self, account_id, amount): pass def withdraw(self, account_id, amount): pass def balance(self, account_id): pass # Test cases to validate the solution if __name__ == "__main__": manager = AccountManager() # Test create and balance manager.create("acc123", 1000) assert manager.balance("acc123") == 1000 # Test deposit manager.deposit("acc123", 500) assert manager.balance("acc123") == 1500 # Test withdraw manager.withdraw("acc123", 500) assert manager.balance("acc123") == 1000 # Test insufficient funds withdraw manager.withdraw("acc123", 1500) assert manager.balance("acc123") == 1000 # Test account does not exist assert manager.balance("acc456") == "Account does not exist" # Testing multiple operations manager.create("acc123", 500) manager.deposit("acc123", 200) assert manager.balance("acc123") == 700 manager.withdraw("acc123", 100) assert manager.balance("acc123") == 600 assert manager.balance("acc456") == "Account does not exist" manager.deposit("acc456", 100) manager.withdraw("acc123", 700) assert manager.balance("acc123") == 600

answer:class AccountManager: def __init__(self): self.accounts = {} def create(self, account_id, balance): # If the account does not exist, create it with the initial balance if account_id not in self.accounts: self.accounts[account_id] = balance def deposit(self, account_id, amount): # If the account exists, deposit the amount if account_id in self.accounts: self.accounts[account_id] += amount def withdraw(self, account_id, amount): # If the account exists and has enough balance, withdraw the amount if account_id in self.accounts and self.accounts[account_id] >= amount: self.accounts[account_id] -= amount def balance(self, account_id): # Print the balance if the account exists, otherwise print "Account does not exist" if account_id in self.accounts: return self.accounts[account_id] else: return "Account does not exist"

question:def determine_winner(t: int, test_cases: List[int]) -> List[str]: Determines who will win the game given the board lengths and optimal play. Parameters: t (int): Number of test cases test_cases (List[int]): List of integers representing the board lengths Returns: List[str]: List of results "Alice" or "Bob" for each test case >>> determine_winner(2, [3, 4]) ["Alice", "Bob"] >>> determine_winner(3, [5, 6, 7]) ["Alice", "Bob", "Alice"]

answer:def determine_winner(t, test_cases): Determines who will win the game given the board lengths and optimal play. Parameters: t (int): Number of test cases test_cases (List[int]): List of integers representing the board lengths Returns: List[str]: List of results "Alice" or "Bob" for each test case results = [] for n in test_cases: # In the first turn, Alice will: # - Win immediately if n is 2 since she will be on space 1 and can roll a 1 to win. # - Otherwise, if Alice cannot win immediately, check the space: # - If n is odd, Alice will be the one to make this extra step to win. # - If n is even, Bob will be the one to make this extra step to win. if n % 2 == 0: results.append("Bob") else: results.append("Alice") return results

question:def perform_operations(T: int, test_cases: List[Tuple[str, int]]) -> List[str]: Sarah is learning about strings and character manipulations. Today, she encountered a problem where she has to repeatedly modify a string based on a set of rules. The task is as follows: given a string `s` consisting of lowercase English letters, perform a series of operations. Each operation will replace every instance of letter 'a' with 'b', every 'b' with 'c', every 'c' with 'd', and so on until every 'z' is replaced with 'a'. Given the number of operations `K`, write a program that will output the string after applying all `K` operations. Args: T (int): The number of test cases. test_cases (List[Tuple[str, int]]): A list of test cases where each test case is a tuple containing a string `s` and an integer `K`. Returns: List[str]: A list of modified strings after `K` operations for each test case. >>> perform_operations(2, [("abc", 2), ("xyz", 3)]) == ["cde", "abc"] >>> perform_operations(1, [("", 5)]) == [""]

answer:def shift_characters(s, K): Shift each character in the string `s` K positions ahead in the alphabet. K = K % 26 # Since after 26 operations, each letter returns to its original position shifted_string = '' for char in s: new_char = chr((ord(char) - ord('a') + K) % 26 + ord('a')) shifted_string += new_char return shifted_string def perform_operations(T, test_cases): results = [] for i in range(T): s, K = test_cases[i] results.append(shift_characters(s, K)) return results

Released under the chat License.

has loaded