Visitors to the Eötvös Loránd1 University of Sciences in Budapest are greeted by a perpetual book with leaves of water.
A reference to a number of consecutive pages from a book is usually noted as m–n, with $$m \in \mathbb{N}_0$$ the number of the first page and $$n \in \mathbb{N}_0$$ the number of the last page (with $$m < n$$). A reference to a single page with number $$m$$ is simply noted as m (so we never write m–m).
If the numbers $$m$$ and $$n$$ have the same number of digits, the notation is often abbreviated by removing the longest common prefix in the digits of $$m$$ and $$n$$ from $$n$$. For example, the notation 1234–1247 can be abbreviated to 1234–47 by removing the longest common prefix 12 from 1247. We can therefore easily recognize that m–n is an abbreviation, given that in this case $$m > n$$.
Your task is to write the following functions, that each take a reference to a number of consecutive pages from a book (str). This may either be a reference to a single page or to multiple pages, and in the latter case both the full notation or the abbreviated notation may be used.
A function pages that returns a tuple containing the number (int) of the first and the last page of the given reference. If a reference to a single page is given, that page is both the first and the last page. If an abbreviated reference is given, the number of the last page is the one as given in the reference (which has a smaller value than that of the first page).
A function short that, if possible, returns the abbreviated notation (str) of the given reference. If the given reference is not yet abbreviated but can be abbreviated, its abbreviated notation must be returned. Otherwise, the given reference itself must be returned.
A function long that returns the full notation (str) of the given reference.
A function page_count that returns the total number of pages in the given reference. When counting pages, both the first and the last page of the reference are taken into account.
>>> pages('1234')
(1234, 1234)
>>> pages('1234-1247')
(1234, 1247)
>>> pages('1234-47')
(1234, 47)
>>> pages('99-103')
(99, 103)
>>> short('1234')
'1234'
>>> short('1234-1247')
'1234-47'
>>> short('1234-47')
'1234-47'
>>> short('99-103')
'99-103'
>>> long('1234')
'1234'
>>> long('1234-1247')
'1234-1247'
>>> long('1234-47')
'1234-1247'
>>> long('99-103')
'99-103'
>>> page_count('1234')
1
>>> page_count('1234-1247')
14
>>> page_count('1234-47')
14
>>> page_count('99-103')
5
This staircase near the library at Lebanon's University of Balamand2 is painted to resemble a stack of classic texts:
Republic4 (Greek: Πολιτεία, Politeia; Latin: Res Publica) by Plato5
The Epistle of Forgiveness9 (Arabic: رسالة الغفران, Risālat al-ghufrān) by 10Abī al-Alā al-Ma’arrī11
Divine Comedy12 (Italian: La Divina Commedia) by 13Dante Alighieri14
Muqaddimah15 by 16Ibn Khaldūn17
The Prince18 (Italian: Il principe) by 19Niccolò Machiavelli20
Discourse on the Method21 (French: Discours de la Méthode) by 22René Descartes23
Critique of Pure Reason24 (German: Kritik der reinen Vernunft) by 25Immanuel Kant26
On the Origin of Species30 by 31Charles Darwin32
33The Brothers Karamazov (Russian: Братья Карамазовы, Brat'ja Karamazovy) by 34Fyodor Dostoevsky35
Thus Spoke Zarathustra36 (German: Also sprach Zarathustra) by 37Friedrich Nietzsche38
Relativity: The Special and the General Theory39 by 40Albert Einstein41
The Prophet42 by 43Khalil Gibran44
The Days (Arabic: الايام, al-Ayyām) by 45Tāhā Husayn46
A Study of History47 by 48Arnold Toynbee49
Cosmos: A Personal Voyage50 by 51Carl Sagan52
Les Desorientés55 by Amin Maalouf56
The Road Ahead57 by 58Bill Gates59
This puts them (almost) in chronological order.