Appearance
question:You are working on a collaborative text editor, and one of the cooler features you want to add is "highlighting positives". Basically, you're performing sentiment analysis on text and want to bold any phrases that have a positive sentiment so that they're easy to spot. The challenge here is to write a function that will search through a given text and wrap any positively-connotated word in a pair of `<b></b>` tags. For simplicity, let's assume we have a predefined list of positive words as follows: ``` positive_words = ["good", "great", "excellent", "awesome", "fantastic", "marvelous", "wonderful"] ``` Write a function `highlight_positives(text: str) -> str` that takes a `text` string and returns the `text` with all occurrences of any words in the `positive_words` list wrapped in `<b></b>` tags. For example: ``` input: "This is a good day. An excellent opportunity awaits." output: "This is a <b>good</b> day. An <b>excellent</b> opportunity awaits." input: "Everything is awesome! We are having a fantastic time." output: "Everything is <b>awesome</b>! We are having a <b>fantastic</b> time." ``` Notes: - The function should be case insensitive; however, the original case of the text should be preserved in the output. - Words should be matched exactly; substrings should not be highlighted. For example, "good" should not match "goodbye". - Assume the given text consists only of alphabetic characters and spaces. No need to handle punctuation, numbers, or other special characters.
answer:import re positive_words = ["good", "great", "excellent", "awesome", "fantastic", "marvelous", "wonderful"] def highlight_positives(text: str) -> str: Wraps all occurrences of positive words in the text with <b></b> tags. def bold_match(match): return f"<b>{match.group(0)}</b>" # Create pattern to match whole words from positive_words pattern = r'b(' + '|'.join(re.escape(word) for word in positive_words) + r')b' # Use re.sub with case insensitive flag to substitute matches with bold tags highlighted_text = re.sub(pattern, bold_match, text, flags=re.IGNORECASE) return highlighted_text
question:Galaxy Explorers have discovered a new alien language on planet Xenon. The language follows an interesting structure; each word is composed of a sequence of repetitive units. A unit is defined as one or more characters that repeat to form the word. Your task is to determine the smallest unit of repetition for a given word. For instance, the word "ababab" is made up of the repeating unit "ab". If no smaller repeating unit exists, the word itself is the smallest repeating unit. -----Input----- Input to the program consists of a single line containing the string word (where the size of word should be <= 50). The string consists of only lowercase alphabets. -----Output----- Output should consist of a single line indicating the smallest unit of repetition for the given word. -----Example----- Input: abcabcabc Output: abc Input: aaaaaa Output: a
answer:def smallest_repetitive_unit(word): Determines the smallest unit of repetition for a given word. Parameters: word (str): The input word composed of lowercase alphabets. Returns: str: The smallest unit of repetition. length = len(word) for i in range(1, length // 2 + 1): unit = word[:i] if length % i == 0: if unit * (length // i) == word: return unit return word
question:# Background A common task in statistical analysis is to sort a dataset to quickly find the median value. The median is the middle number in a sorted, ascending or descending, list of numbers and can be more descriptive of that data set than the average. # Task: Given an unsorted list of integers, write a function that returns the median value. If the list has an odd number of elements, the median is the middle element. If the list has an even number of elements, the median is the average of the two middle elements. # Assumptions: - The input list will contain at least one integer. - You can use built-in sorting functions. # Example: Input: ``` [12, 4, 5, 3, 8, 7] ``` Output: ``` 6.0 ``` Input: ``` [7, 1, 3, 4] ``` Output: ``` 3.5 ```
answer:def find_median(nums): Returns the median value of a list of integers. If the list has an odd number of elements, the median is the middle element. If the list has an even number of elements, the median is the average of the two middle elements. nums = sorted(nums) n = len(nums) mid = n // 2 if n % 2 == 1: return nums[mid] else: return (nums[mid - 1] + nums[mid]) / 2
question:# Scrambled Words! Your task in this kata is to write a function that takes a scrambled sentence and returns the original sentence by rearranging the words based on their positions. # Description Each word in the input string will contain exactly one digit `1-9` that signifies its position in the original sentence. The function should remove these digits and return the words in the correct order. # Input The input will be a single string containing scrambled words, separated by spaces. Each word will have exactly one digit `1-9`. # Output The function should return a single string containing the words ordered by their digits and separated by a single space. The digits should be removed in the output. Examples ```python unscramble("is2 Thi1s T4est 3a") ``` should return: ```python "This is a Test" ``` ```python unscramble("4of Fo1r pe6ople g3ood th5e the2") ``` should return: ```python "For the good of the people" ``` # Invalid input - If any word does not contain exactly one digit, return an empty string. # Additional information - The input string will not be empty. - There will be no repeated position digits. - The words will not contain any other digits outside the `1-9` range. - Words may contain punctuation or other symbols which should not be altered or removed. The function should adhere to these rules and provide the correct unscrambled sentence or an empty string for invalid inputs.
answer:import re def unscramble(sentence): Unscramble the sentence based on the digit in each word. Args: sentence (str): A scrambled sentence with each word containing exactly one digit (1-9). Returns: str: The original sentence with words ordered by their digits and digits removed. words = sentence.split() ordered_words = [] # Create a dictionary to store words based on their position word_dict = {} for word in words: match = re.search(r'd', word) if match is None or len(re.findall(r'd', word)) != 1: return "" # if word does not contain exactly one digit position = int(match.group()) word_dict[position] = re.sub(r'd', '', word) # Sort words by their position and join into a sentence ordered_sentence = ' '.join(word_dict[pos] for pos in sorted(word_dict.keys())) return ordered_sentence