Our Solar System comprises the Sun and objects that orbit it, whether they orbit it directly or by orbiting other objects that orbit it directly. Of those objects that orbit the Sun directly, the largest eight are the planets that form the planetary system around it. The remainder are significantly smaller objects, such as dwarf planets and small Solar System Bodies (SSSBs) such as comets and asteroids.
The eight planets of the Solar System can be aligned as follows according to increasing (average) distance to the Sun: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus en Neptune. When discovered in 1930, Pluto was initially considered to be the ninth planet of the Sun. This changed when it was downgraded to a dwarf planet in 2006 with the adoption of a formal definition of a planet1. Eris, discovered in 2005, is the most massive known dwarf planet and and is at its most distant position roughly more than three times as far from the Sun as Pluto. Now, if we assign the values -8 up to and including 8 to the successive letters of the string JHMLCNVTURISEYAPO, we get:
S+U+N | = | 3+0-3 | = | 0 |
M+E+R+C+U+R+Y | = | -6+4+1-4+0+1+5 | = | 1 |
V+E+N+U+S | = | -2+4-3+0+3 | = | 2 |
E+A+R+T+H | = | 4+6+1-1-7 | = | 3 |
M+A+R+S | = | -6+6+1+3 | = | 4 |
J+U+P+I+T+E+R | = | -8+0+7+2-1+4+1 | = | 5 |
S+A+T+U+R+N | = | 3+6-1+0+1-3 | = | 6 |
U+R+A+N+U+S | = | 0+1+6-3+0+3 | = | 7 |
N+E+P+T+U+N+E | = | -3+4+7-1+0-3+4 | = | 8 |
P+L+U+T+O | = | 7-5+0-1+8 | = | 9 |
E+R+I+S | = | 4+1+2+3 | = | 10 |
A letterstring is a string (str) that is composed of an odd number of different letters. All functions in this assignment take a letterstring as an argument, and must check that this string only contains letters, has an odd length and contains no duplicate letters (without making a distinction between uppercase and lowercase letters). If this is not the case, the functions must raise an AssertionError with the message invalid letterstring. Raising this AssertionError always takes precedence over any other exceptions that are explicitly raised by the functions.
In addition, one or more words can be passed to some functions. You may assume that these words only contain letters, without the need to check this explicitly. The word value of a word is an integer that is computed as the sum of the values of the individual letters. The letter value is determined using a lettterstring having length $$2n + 1$$ ($$n \in \mathbb{N}$$), whose successive letters are assigned the values $$-n$$ up to and including $$n$$. No distinction should be made between uppercase and lowercase letters in determining the word value.
Write a function lettervalue that takes a letterstring (str). For a given letterstring of length $$2n + 1$$ ($$n \in \mathbb{N}$$), the function must return a dictionary (dict) that maps the successive letters (str) of the letterstring into the values $$-n$$ up to and including $$n$$ (int). All keys of this dictionary should be uppercase letters.
Write a function wordvalue that takes two arguments: a word (str) and a letterstring (str). The function must return the word value (int) of the given word, with the value of the individual letters based on the given letterstring. In case the given word contains letters that do not occur in the given letterstring (without making distinction between uppercase and lowercase letters), the function must raise an AssertionError with the message missing letters.
Write a function alignment that takes two arguments: a sequence (list or tuple) of $$m \in \mathbb{N}_0$$ words (str) and a letterstring (str). The function must return a Boolean value (bool) that indicates whether the words have successive word values $$0, 1, \ldots, m - 1$$. In determining the word values, the value of the individual letters should be based on the given letterstring.
Write a function arrange1 that takes two arguments: a list (list) of words (str) and a letterstring (str). The function must sort the words in the given list according to increasing word value. In doing so, word values should be determined using the given letterstring. Words having the same word value must be sorted alphabetically, without making distinction between uppercase and lowercase letters.
Write a function arrange2 that takes two arguments: a sequence (list or tuple) of words (str) and a letterstring (str). The function must return a tuple containing the words (str) from the given sequence sorted according to increasing word value. In doing so, word values should be determined using the given letterstring. Words having the same word value must be sorted alphabetically, without making distinction between uppercase and lowercase letters.
Make sure that the implementations of the function make optimal reuse of functionality that has already been implemented.
>>> lettervalue('EARTH')
{'A': -1, 'H': 2, 'R': 0, 'E': -2, 'T': 1}
>>> lettervalue('Venus')
{'U': 1, 'S': 2, 'N': 0, 'E': -1, 'V': -2}
>>> lettervalue('Churyumov-Gerasimenk')
Traceback (most recent call last):
AssertionError: invalid letterstring
>>> wordvalue('SUN', 'JHMLCNVTURISEYAPO')
0
>>> wordvalue('mercury', 'JHMLCNVTURISEYAPO')
1
>>> wordvalue('Venus', 'JHMLCNVTURISEYAPO')
2
>>> wordvalue('EARTH', 'ABCDEFGHIJKLM')
Traceback (most recent call last):
AssertionError: missing letters
>>> alignment(['SUN', 'mercury', 'Venus'], 'JHMLCNVTURISEYAPO')
True
>>> alignment(['SUN', 'mercury', 'EARTH'], 'JHMLCNVTURISEYAPO')
False
>>> alignment(['Sun', 'mercury', 'Venus', 'EARTH', 'MARS', 'Jupiter', 'saturn', 'URANUS', 'Neptune', 'pluto'], 'JHMLCNVTURISEYAPO')
True
>>> planets = ['MARS', 'saturn', 'Jupiter', 'URANUS', 'Venus', 'mercury', 'EARTH', 'Sun', 'Neptune', 'pluto']
>>> arrange1(planeten, 'JHMLCNVTURISEYAPO')
>>> planets
['Sun', 'mercury', 'Venus', 'EARTH', 'MARS', 'Jupiter', 'saturn', 'URANUS', 'Neptune', 'pluto']
>>> planets = ('MARS', 'saturn', 'Jupiter', 'URANUS', 'Venus', 'mercury', 'EARTH', 'Sun', 'Neptune', 'pluto')
>>> arrange2(planeten, 'JHMLCNVTURISEYAPO')
('Sun', 'mercury', 'Venus', 'EARTH', 'MARS', 'Jupiter', 'saturn', 'URANUS', 'Neptune', 'pluto')
Earth is the only planet not named after a god. If that's not mundane!