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.

Assignment

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:

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.

Example

>>> 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'

Epilogue

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.

Het vogelbekdier brengt de helft van de tijd in het water door. Bij het zwemmen worden de voorpoten afwisselend bewogen.

Resources