It's back to school season! So for a part-time job, you're helping out at the school's library. The problem is, the head librarian has never even heard the words "Dewey Decimal," let alone implemented that system. Instead, the sorting system in use has grown "organically" as the library has expanded...
In an effort to keep your sanity, you've elected to write a program to help you sort books as they're returned, because woe unto you if you sort the books wrong. (The head librarian is VERY strict.)
Input / Output
- The input will be a list of (hypothetical) book titles, one per line, from STDIN / language equivalent.
- You can assume no more than 100 books input at a time (you can only carry so many around the library at once).
- Books can have multiple words in their titles, and these words may be separated by spaces or other punctuation (e.g., a colon
:
, a dash-
, etc.). - For ease of calculation, assume all titles are UTF-8.
Output is the same titles, sorted according to the below rules, again one per line, to STDOUT / language equivalent.
The Sorting Rules
Books are sorted numerically based on their average character value (i.e., cumulative character value divided the number of characters in the book title), counted by the following rules:
- All characters count for determining the number of characters in a title.
- Lowercase letters are counted by their position in the alphabet. (a=1,b=2,...z=26)
- If the title contains capital letters, those count for 1.5 their lowercase value (A=1.5,B=3,...Z=39). ("Capital letters are important!" says the librarian.)
- Each punctuation mark / symbol in this list
!@#$%^&*()-=_+[]\{}|;':",./<>?~
counts -1 from the cumulative value before averaging. ("Grandiose titles are not!") - If the title contains a number, written in Arabic numerals, that number is subtracted from the average value before sorting. Multiple consecutive digits are treated as one number (e.g.,
42
would subtract 42, not subtract 4 and then subtract 2). Individual digits don't count for the cumulative value (i.e., each digit contributes 0), but DO count for number of characters. Note that this may result in a negative value and should be treated appropriately. (Rumor has it, the librarian has had a crush on a math instructor for several years, now.) - If the title contains two separate words that start with an
R
, the book gets a score of "infinity" and is dumped into a pile in the corner (i.e., randomly arranged at the end of the list). (The librarian was once dumped by a person with those initials, or so you've heard.) - Spaces don't count for the cumulative character value (i.e., they contribute 0), but DO contribute to the number of characters in a title.
- Characters that don't fit the above rules (e.g, a
ÿ
) don't count for the cumulative character value (i.e., they contribute 0), but DO contribute to the number of characters in a title. - For example, a hypothetical book
ÿÿÿÿÿ
would have a "score" of(0+0+0+0+0) / 5 = 0
, but a hypothetical bookÿÿyÿÿ
would have a "score" of(0+0+25+0+0) / 5 = 5
. - Two books that happen to "score" the same can be Output in your choice of order. (They're on the same shelf, anyway)
Example Input 1
War and Peace
Reading Rainbow: The Best Unicorn Ever
Maus
Home for a Bunny
Example Output 1 (with "scores" in parentheses to show reasoning - you don't need to print them)
War and Peace (8.5)
Home for a Bunny (10.125)
Maus (15.125)
Reading Rainbow: The Best Unicorn Ever (infinity)
Example Input 2
Matthew
Mark
Luke
John
Revelations
Example Output 2 (with "scores" in parentheses to show reasoning - you don't need to print them)
Mark (12.375)
John (13)
Revelations (13.545454...)
Luke (13.75)
Matthew (~13.786)
Example Input 3
42
9 Kings
1:8
7th
Example Output 3 (with "scores" in parentheses to show reasoning - you don't need to print them)
42 (-42)
1:8 (-9.3333...)
9 Kings (~0.36)
7th (2.3333...)
Other restrictions
- This is Code-Golf, because you need to keep the program secret from the ever-watching eyes of the librarian, and the smaller the program, the easier it is to hide.
- Standard loophole restrictions apply
- Don't let the librarian catch you slacking off by spending all your time on PPCG.