Skip to content
🤔prompts chat🧠
🔍
question:In this problem, you are required to manipulate strings in Python. Write a function named `reverse_words` that takes a single string containing a sentence as input, and returns a new string in which the order of the words is reversed. Each word in the sentence is separated by a single space. The function should perform the following steps: 1. Split the sentence into individual words. 2. Reverse the order of the words. 3. Join the reversed words back into a single string, with each word separated by a single space. **Function Signature:** ```python def reverse_words(sentence: str) -> str: pass ``` **Parameters:** - `sentence` (str): A string containing a sentence of words separated by spaces. **Return:** - `str`: A new string with the order of words reversed. **Example Usage:** ```python sentence = "Hello world this is a test" result = reverse_words(sentence) print(result) # Output: "test a is this world Hello" ``` **Requirements:** - Do not use any external libraries for reversing the words. - Ensure that the input and output strings do not have leading or trailing spaces. - The function should handle multiple spaces between words correctly.

answer:def reverse_words(sentence: str) -> str: Reverses the order of words in the given sentence. Args: sentence (str): A string containing a sentence of words separated by spaces. Returns: str: A new string with the order of words reversed. words = sentence.split() reversed_words = words[::-1] return ' '.join(reversed_words)

question:**Problem Title: String Permutation Checker** Create a function named `is_permutation` that takes two arguments: `str1` and `str2`, representing two strings. The function should determine if `str2` is a permutation (anagram) of `str1`. A permutation of a string is another string that contains the same characters, only in a different order. The comparison should be case-sensitive and should not ignore any spaces or special characters. Your program should ensure the following: 1. Both input strings can contain any printable ASCII characters including spaces. 2. If `str2` is a permutation of `str1`, return `True`; otherwise, return `False`. 3. The function should handle edge cases where the input strings have different lengths immediately by returning `False`. **Sample Function Signature:** ```python def is_permutation(str1: str, str2: str) -> bool: pass ``` **Example:** ```python str1 = "dog" str2 = "god" is_permutation(str1, str2) # Should return True str1 = "hello" str2 = "billion" is_permutation(str1, str2) # Should return False str1 = "12345" str2 = "54321" is_permutation(str1, str2) # Should return True ``` **Explanation:** - In the first example, "god" is a permutation of "dog". - In the second example, "billion" is not a permutation of "hello" as they have different characters. - In the third example, "54321" is a permutation of "12345" since it contains the same characters in a different order.

answer:def is_permutation(str1: str, str2: str) -> bool: Checks if str2 is a permutation of str1. :param str1: First string :param str2: Second string :return: True if str2 is a permutation of str1, False otherwise # Early return since permutations must be of the same length if len(str1) != len(str2): return False # Use a dictionary to count occurrences of each character from collections import Counter return Counter(str1) == Counter(str2)

question:You need to write a function `is_perfect_string` that checks if a given string is a "perfect string". A "perfect string" meets the following criteria: 1. The string contains only lowercase alphabets. 2. The string forms a palindrome when converted to a list of its alphabetical indices (consider 'a' as 1, 'b' as 2, ..., 'z' as 26). Your function should: 1. Return `True` if the string is a perfect string. 2. Return `False` otherwise. Here is the function signature: ```python def is_perfect_string(s: str) -> bool: # Your code here ``` Examples: ```python is_perfect_string("abba") # Returns: True is_perfect_string("abc") # Returns: False ``` Explanation: - For the string "abba", the corresponding indices are [1, 2, 2, 1]. This forms a palindrome. - For the string "abc", the corresponding indices are [1, 2, 3]. This does not form a palindrome.

answer:def is_perfect_string(s: str) -> bool: Checks if a given string is a "perfect string". A "perfect string": 1. Contains only lowercase alphabets. 2. Forms a palindrome when converted to a list of its alphabetical indices. Parameters: s (str): The input string to be checked. Returns: bool: True if the string is a perfect string, False otherwise. if not s.isalpha() or not s.islower(): return False alpha_indices = [ord(char) - ord('a') + 1 for char in s] return alpha_indices == alpha_indices[::-1]

question:Write a Python function that determines if a given number is a Narcissistic number. A Narcissistic number (or Armstrong number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, the number 153 is a Narcissistic number because (153 = 1^3 + 5^3 + 3^3) and it has 3 digits. Write a function `is_narcissistic(num)` that checks if a given integer is a Narcissistic number. The function should take one argument: - `num` (int): The number to be checked. The function should return: - `True` if the number is a Narcissistic number. - `False` otherwise. **Constraints:** - The input number is a non-negative integer. **Example:** ```python is_narcissistic(153) ``` Output: ```python True ``` **Example:** ```python is_narcissistic(123) ``` Output: ```python False ``` **Hint:** To determine if a number is a Narcissistic number, sum its digits each raised to the power of the number of digits in the number and compare this sum to the original number.

answer:def is_narcissistic(num): Check if the given number is a Narcissistic number. A Narcissistic number is a number that is equal to the sum of its own digits each raised to the power of the number of digits. Parameters: num (int): The number to be checked. Returns: bool: True if the number is a Narcissistic number, False otherwise. digits = [int(d) for d in str(num)] num_digits = len(digits) return num == sum(d ** num_digits for d in digits)

Released under the chat License.

has loaded