What is 4/7 wallaby, 1/4 parakeet and 3/5 perch?
Solution: wallaby + parakeet + perch = wallpaper
In other words, we take four out of seven ($$\frac{4}{7}$$) letters of the word wallaby, a quarter ($$\frac{2}{8} = \frac{1}{4}$$) of the letters of the word parakeet, and three out of five ($$\frac{3}{5}$$) letters of the word perch. In each case, letters are taken at the front of the word.
A word $$w$$ is a string (str) containing zero or more letters (uppercase and lowercase). We denote the length of a word $$w$$ as $$||w||$$.
A word $$p$$ is a prefix of a word $$w$$ if a word $$s$$ exists such that $$w = ps$$, or in other words if the word $$w$$ starts with the word $$p$$.
A word $$s$$ is a suffix of a word $$w$$ if a word $$p$$ exists such that $$w = ps$$, or in other words if the word $$w$$ ends with the word $$s$$.
A fraction $$f$$ is an object of the data type Fraction that is defined in the module fractions1 of the Python Standard Library2. The string representation of a fraction with numerator $$n$$ and denominator $$d$$ is n/d. The string representation of a negative fraction has a minus sign in front of its numerator (e.g. -3/4).
A question is a string (str) of the form
What is word_fraction1, word_fraction2, …, word_fractionn-1 and word_fractionn?
where word_fractioni consists of the string representation of a fraction $$f_i$$, followed by a space and a word $$w_i$$ for $$i = 1, 2, \ldots, n$$ and $$n \geq 2$$.
Your task:
Write a function word_fraction that takes a word $$w$$ and a fraction $$f$$. If $$f \geq 0$$, the function must return the prefix $$p$$ of $$w$$ such that $$\frac{||p||}{||w||} = f$$. Otherwise, the function must return the suffix $$s$$ of $$w$$ such that $$\frac{||s||}{||w||} = |f|$$, where $$|f|$$ denotes the absolute value of fraction $$f$$. The result of this function is called the word fraction of word $$w$$ and fraction $$f$$.
Write a function combine that takes two lists (list) of length $$n \in \mathbb{N}$$ ($$n \geq 2$$): i) a list containing words $$w_1, w_2, \ldots, w_n$$ (str) and ii) a list containing fractions $$f_1, f_2, \ldots, f_n$$ (Fraction). The function must return the string (str) that results from concatenating the word fractions of word $$w_i$$ and fraction $$f_i$$ for $$i = 1, 2, \ldots, n$$.
Write a function decompose that takes a question (str). The function must return a tuple (tuple) with two elements: i) a list (list) containing all words $$w_1, w_2, \ldots, w_n$$ (str) from the question and ii) a list (list) containing all fractions $$f_1, f_2, \ldots, f_n$$ (Fraction) from the question. Both the words and the fractions must be listed in their order of appearance in the question.
Write a function answer that takes a question (str). If the question contains the words $$w_1, w_2, \ldots, w_n$$ and the fractions $$b_1, b_2, \ldots, b_n$$, in that order, the function must return the string (str) that results from concatenating the word fractions of word $$w_i$$ and fraction $$f_i$$ for $$i = 1, 2, \ldots, n$$.
If an impossible word fraction must be determined (e.g. it is impossible to select $$\frac{3}{5}$$ of the letters in the word spam) when calling the functions word_fraction, combine or answer, an AssertionError must be raised with the message invalid fraction.
>>> word_fraction('wallaby', Fraction(4, 7))
'wall'
>>> word_fraction('parakeet', Fraction(2, 8))
'pa'
>>> word_fraction('perch', Fraction(3, 5))
'per'
>>> word_fraction('ALPACA', Fraction(-1, 3))
'CA'
>>> word_fraction('PARTRIDGE', Fraction(-7, 9))
'RTRIDGE'
>>> word_fraction('manatee', Fraction(1, 2))
Traceback (most recent call last):
AssertionError: invalid fraction
>>> combine(['wallaby', 'parakeet', 'perch'], [Fraction(4, 7), Fraction(1, 4), Fraction(3, 5)])
'wallpaper'
>>> combine(['ALPACA', 'PARTRIDGE'], [Fraction(-1, 3), Fraction(-7, 9)])
'CARTRIDGE'
>>> combine(['Manatee', 'cheetah', 'hamster'], [Fraction(3, 7), Fraction(3, 7), Fraction(-4, 7)])
'Manchester'
>>> decompose('What is 4/7 wallaby, 1/4 parakeet and 3/5 perch?')
(['wallaby', 'parakeet', 'perch'], [Fraction(4, 7), Fraction(1, 4), Fraction(3, 5)])
>>> decompose('What is -1/3 ALPACA and -7/9 PARTRIDGE?')
(['ALPACA', 'PARTRIDGE'], [Fraction(-1, 3), Fraction(-7, 9)])
>>> decompose('What is 3/7 Manatee, 3/7 cheetah and -4/7 hamster?')
(['Manatee', 'cheetah', 'hamster'], [Fraction(3, 7), Fraction(3, 7), Fraction(-4, 7)])
>>> answer('What is 4/7 wallaby, 1/4 parakeet and 3/5 perch?')
'wallpaper'
>>> answer('What is -1/3 ALPACA and -7/9 PARTRIDGE?')
'CARTRIDGE'
>>> answer('What is 3/7 Manatee, 3/7 cheetah and -4/7 hamster?')
'Manchester'
In terms of appearance, a platypus looks as if Mother Nature has played Frankenstein with bits of bird, reptile and mammal. But the platypus genome — which has been fully mapped since January 2021 — also shows a strange mix of genes from different classes of vertebrates.