21
3
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.
What if two books score exactly the same. i.e i Have Reading Rainbow and Ruby Rails – Kishan Kumar – 2015-09-16T16:22:49.087
@KishanKumar In that specific instance, "randomly arranged at the end of the list" since they're both double-R. In other words, take your pick. In the general case, if two words score the same, they can appear in any order relative to each other. I'll add a bullet to clarify that. – AdmBorkBork – 2015-09-16T16:25:56.500
7You need an A word so your system has an acronym name. I recommend Crazy Librarian's Amazing Sorting System :D – Geobits – 2015-09-16T16:44:06.087
3@Geobits Do you have CLASS? – AdmBorkBork – 2015-09-16T16:47:21.260
Numbers are just decimal numbers? What if there are several ones, are all of them subtracted separately? – Paŭlo Ebermann – 2015-09-16T19:48:36.147
@PaŭloEbermann Strictly speaking, they're Integers, as the
.
is considered punctuation and handled separately. Yes, if there are multiple numbers in the input they are each subtracted, as in the1:8
in the 4th example --(0+-1+0)/3 - (1+8) = -9.333....
– AdmBorkBork – 2015-09-16T19:54:40.157But only integers in decimal notation using the digits
0
to9
, not octal, hexadecimal or strange Chinese characters, right? – Paŭlo Ebermann – 2015-09-16T19:57:06.853@PaŭloEbermann Yes, Arabic numerals (what Unicode would call "European digits") only. Other characters should be handled as the
– AdmBorkBork – 2015-09-16T20:01:19.967ÿ
example. I've edited the numbers portion for clarification.Also,
9 Kings
is(-9 + 0 + 1,5*11 + 9 + 14 + 7 + 19)/7 = ~8,07
,7th = (-7+20+8)/3 = 7
and1:8 = (-1 + 0 - 8)/3 = -3
– Zereges – 2015-09-16T23:18:40.967@Zereges No - integer values are subtracted after you average and before you sort, not as part of the averaging. The digits count as zero. For
7th
that means(0+20+8)/3 - 7 = 2.333.
for example. I expanded out the1:8
example three comments up. Let me know if that's still not clear. – AdmBorkBork – 2015-09-17T13:01:02.783Can you confirm that the symbols (scoring -1) are all the ASCII printable characters (ascii code 32 to 126) except alphanumeric, space (32), backslash (92) and backtick (92) ? – edc65 – 2015-10-05T17:31:49.897
@edc65 You're correct, excepting that backslash (92) is included in the symbols list on this challenge. The backtick (96) is not included in the symbols list on this challenge. However, do note that this challenge is anticipating Unicode input/output. – AdmBorkBork – 2015-10-05T17:56:47.020
Right ... the backslash was eaten when I ported the string in javascript code. Thanks – edc65 – 2015-10-05T18:07:51.150
Can we assume a maximum title lenght? – Fabian Schmengler – 2015-10-05T19:24:44.090
@fschmengler Sure. I'm not picky on that, so choose something reasonable (e.g., 50+) if it impacts your code length. None of the private tests that I have should get anywhere close to overflowing a buffer or suffering from wrapping an int or the like. – AdmBorkBork – 2015-10-05T19:27:24.413
Can I expect no title to have two capital
R
's in one word? – Padarom – 2015-10-06T07:10:42.707@Padarom Like a CamelCase word? I don't have a test case that explicitly tests that one way or the other, but you should ensure that your algorithm doesn't match any capital R, just those that are at the beginning of the words.
– AdmBorkBork – 2015-10-06T12:48:46.580Okay, that's what I was trying to find out, thanks! – Padarom – 2015-10-06T12:53:10.070
It would be so awesome to have an Ook! entry here... – Sanchises – 2015-10-07T10:04:28.223