Appearance
question:In a faraway kingdom, there are n castles, each in need of protection from possible invasions. The king decides to deploy a certain number of knights to guard the borders. However, due to budget constraints and strategic planning, the knights can only be deployed in specific patterns. The king wants his knights deployed such that: 1. No two knights protect the same castle. 2. Knights can only be placed in castles that are not immediately next to one another (i.e., if a knight is placed in castle i, then no knight can be in castles i-1 and i+1). Given the number of castles, n, determine the number of distinct ways to deploy the knights under these conditions. -----Input----- The input consists of a single integer n (1 leq n leq 40) denoting the number of castles. -----Output----- Output a single integer w, the number of distinct ways to deploy the knights in the castles. -----Examples----- Sample Input 1: 3 Sample Output 1: 3 Sample Input 2: 5 Sample Output 2: 8 Note: - For n = 3, the valid deployments are: - Place knights in castles [1] and [3] - Place knights in castle [2] - Do not place any knights (empty placement) - For n = 5, the valid deployments are: - Place knights in castles [1] and [3] - Place knights in castles [1] and [4] - Place knights in castles [2] and [4] - Place knights in castle [1] only - Place knights in castle [2] only - Place knights in castle [3] only - Place knights in castle [4] only - Do not place any knights (empty placement)
answer:def deploy_knights(n): Returns the number of distinct ways to deploy the knights in the castles such that no two knights are adjacent. Args: - n (int): The number of castles. Returns: - int: The number of distinct deployment ways. # If n is 0 or 1, there's only one way to deploy: no knights. if n == 0: return 1 if n == 1: return 2 # either one knight or no knight # dp[i] denotes the number of ways to deploy knights in the first i castles dp = [0] * (n + 1) dp[0] = 1 # No castles, one way (doing nothing) dp[1] = 2 # One castle, two ways (no knight or one knight) for i in range(2, n + 1): dp[i] = dp[i - 1] + dp[i - 2] return dp[n]
question:Longest Palindrome Substring Find the longest substring of a given string that is a palindrome. A palindrome is a string that reads the same backward as forward. Write a function `longest_palindrome_substring(s: str) -> str` that takes a string `s` as input and returns the longest palindromic substring. If there are multiple substrings with the maximum length, return any one of them. Input: - The first line contains an integer T, the number of test cases. - Each test case consists of a single line containing a string `s`. Output: - For each test case, output the longest palindromic substring. Constraints: - 1 ≤ T ≤ 100 - 1 ≤ |s| ≤ 2000 - The string `s` contains only lowercase English letters. SAMPLE INPUT 1 abacdfgdcaba SAMPLE OUTPUT aba
answer:def longest_palindrome_substring(s: str) -> str: Function to find the longest palindromic substring in the given string. n = len(s) if n == 0: return "" start, max_length = 0, 1 for i in range(1, n): low = i - 1 high = i while low >= 0 and high < n and s[low] == s[high]: if high - low + 1 > max_length: start = low max_length = high - low + 1 low -= 1 high += 1 low = i - 1 high = i + 1 while low >= 0 and high < n and s[low] == s[high]: if high - low + 1 > max_length: start = low max_length = high - low + 1 low -= 1 high += 1 return s[start:start + max_length]
question:Background - Substring with Concatenation of All Words Imagine you have a string, `s`, and a list of words, `words`, all of the same length. You need to find the starting indices of all substrings in `s` that are a concatenation of each word in `words` exactly once and without any intervening characters. Your task: **Given a string and a list of words, return all starting indices of substrings in the string that are a concatenation of each word in the list exactly once.** Example: ```python find_substring_indices("barfoothefoobarman", ["foo", "bar"]) == [0, 9] ``` Explanation: Starting at index 0 in the string, you find the substring "barfoo" which is a concatenation of "foo" and "bar". The next valid starting index is at index 9 where the substring is "foobar". Note: - You can return the indices in any order. - Words in the list are of the same length. - The order of concatenation in the substring does not have to be the same as the order of words in the list.
answer:def find_substring_indices(s, words): if not s or not words: return [] word_length = len(words[0]) words_count = len(words) substring_length = word_length * words_count word_map = {} for word in words: if word in word_map: word_map[word] += 1 else: word_map[word] = 1 def is_valid(start): seen_words = {} for i in range(start, start + substring_length, word_length): current_word = s[i:i + word_length] if current_word in word_map: if current_word in seen_words: seen_words[current_word] += 1 else: seen_words[current_word] = 1 if seen_words[current_word] > word_map[current_word]: return False else: return False return True result = [] for i in range(len(s) - substring_length + 1): if is_valid(i): result.append(i) return result
question:Problem You have discovered an ancient encrypted message stored as a series of alphanumeric characters. The encryption process involved substituting each character in the message with its corresponding position in the alphabet for letters and retaining the digits as they are. However, lowercase and uppercase letters are treated as distinct characters. For instance: - 'a' is substituted by '1', 'b' by '2', ..., 'z' by '26' - 'A' is substituted by '27', 'B' by '28', ..., 'Z' by '52' - Digits '0' to '9' remain unchanged. Given an encrypted message, write a function to decrypt it. The decrypted message will be a space-separated string of numbers corresponding to each character's position or the digit itself. Constraints * The input string will consist of 1 to 1000 alphanumeric characters. Function Signature: ```python def decrypt_message(encrypted: str) -> str: pass ``` Input A single string `encrypted` which represents the encrypted message. Output A single string representing the decrypted message where each substitution is separated by a space. Examples Input "abcXYZ123" Output "1 2 3 50 51 52 1 2 3" Input "A1b" Output "27 1 2"
answer:def decrypt_message(encrypted: str) -> str: Takes an encrypted message and returns the decrypted message. Lowercase and uppercase letters are substituted by their respective position in the alphabet. Digits remain unchanged. The result is a space-separated string of numbers. decrypted = [] for char in encrypted: if char.isdigit(): decrypted.append(char) elif char.islower(): decrypted.append(str(ord(char) - ord('a') + 1)) elif char.isupper(): decrypted.append(str(ord(char) - ord('A') + 27)) return ' '.join(decrypted)