51
5
Context
At Hogwarts School of Witchcraft and Wizardry, students are sorted into 4 houses, Gryffindor
, Ravenclaw
, Slytherin
and Hufflepuff
. This sorting is done by a magical hat, called the Sorting Hat.
Task
Your task is to code a sorting hat. In other words, you should create some deterministic rule that, given the name of a person, outputs one of the four available houses. (See the output section for a restriction on the rule)
Input
Input will be a string (representing the student name) in the ascii range [32, 126]
, the printable characters.
This is not necessarily an English name. The input may be "@_??" and your code should still assign a house.
Output
For a fixed string (any string of printable ASCII characters), your code should always output the same house, thus your code should be deterministic. The output has to uniquely identify the houses, in any of the following ways:
- returning the full house name, e.g.
"Gryffindor"
(capitalization doesn't matter) - returning the house initial, e.g.
"G"
(capitalization doesn't matter) - returning the house index, e.g.
0
(you may use any house ordering as long as the indexing is done with0, 1, 2, 3
or1, 2, 3, 4
and as long as you specify it in your answer.)
What is more, there are 12 cases that should be as follows:
- the inputs
harry
,hermione
andron
should returngryffindor
; - the inputs
luna
,cho
andpenelope
should returnravenclaw
; - the inputs
draco
,crab
andgoyle
should returnslytherin
; - the inputs
cedric
,ernie
andhannah
should returnhufflepuff
;
You can either get these 12 inputs as stated, or the 12 inputs where the first letter is upper case (e.g. Harry
).
You should also prove that there are other English names in this list that get mapped into each house.
Be sure to include the house that your code assigns to your own codegolf.SE nickname, plus the house rgs
or RGS
gets assigned to. Bonus imaginary internet points if I get assigned to Ravenclaw
!!!
As of now, these answers qualify for the bonus imaginary internet points: G B Ruby answer, ovs Python answer, Kevin Cruijssen Whitespace answer, Jonathan Allan Jelly answer, Nick Kennedy Jelly answer, Kaddath PHP answer, Noodle9 C answer, 640KB x86-16 answer, Guillermo Phillips PHP answer, Jonathan Alan Python 2 answer, Bob Jarvis - Reinstate Monica Clojure answer, Mitchell Spector Bash answer.
Test cases
"Harry" -> "Gryffindor"
"Hermione" -> "Gryffindor"
"Ron" -> "Gryffindor"
"Luna" -> "Ravenclaw"
"Cho" -> "Ravenclaw"
"Penelope" -> "Ravenclaw"
"Draco" -> "Slytherin"
"Crab" -> "Slytherin"
"Goyle" -> "Slytherin"
"Cedric" -> "Hufflepuff"
"Hannah" -> "Hufflepuff"
"Ernie" -> "Hufflepuff"
This is code-golf so shortest submission in bytes, wins! If you liked this challenge, consider upvoting it... And happy golfing!
5
Can we use different indices for the houses? (would this be allowed: https://tio.run/##HY6xDsIwDER3vsJLlFbqAiqgVmJmh5XFTV0SKXUiU4Z@fcCZ3tPdDZf3zSc@leX2KhHXaUbg0ePHN9yaoTeXwfRnc7yavixJgHElCAzWo8huu7@QrCExqUtiRfwyKp1PikxMMeW6mAVdDZ3gpHynPdbG0SzBqZFwqJFHZvRqj/vTjgeALIG3eqKDpVG25Qc)
– ovs – 2020-02-18T09:34:28.250@ovs I will allow it and edit the challenge accordingly. – RGS – 2020-02-18T13:52:22.693
17Crab should really be Crabbe, although potentially a bit late for the answers already written – Nick – 2020-02-18T16:21:20.227
2What's beyond 127 in the ASCII table? – S.S. Anne – 2020-02-18T16:23:41.490
@NickA geez, you are absolutely right! ahaha sorry for that one! And I call myself a Harry Potter fan :/ Well, I don't want to edit the question now because it might ruin some of the answers... – RGS – 2020-02-18T17:44:56.603
6if this is purely deterministic should it have the
random
tag? – 640KB – 2020-02-18T17:53:14.937The idea was that you could pick a random function from strings to the set
{0,1,2,3}
. Do you think it is not appropriate? What would you suggest? – RGS – 2020-02-18T17:55:04.4506@NickA Technically it should be
vincent
. Alsogoyle
should begregory
. – Magma – 2020-02-18T19:18:49.330@Magma I think calling him "crab" is worse than calling him "crabbe" :) – RGS – 2020-02-18T19:19:40.167
@Noodle9 nice catch! Fixed it – RGS – 2020-02-19T07:29:53.140
"as long as the indexing is done with
0, 1, 2, 3
" - can we please use 1-indexing? Also how about allowing any four consistent, distinct values? Lastly if all the listed names provide satisfactory outputs and we show four names from the linked name list map to the houses, can other names be assigned produce different output (not one of the four houses)? – Jonathan Allan – 2020-02-19T14:57:20.4571@JonathanAllan 1 indexing is fine but it might be a bit late for me to allow any four distinct values. Also, it is not admissible that the sorting hat doesn't work for some strings in the ascii range of printable characters, even if it works for many strings. – RGS – 2020-02-19T17:23:58.673
Thanks, could you clarify indexing and "You should also prove that there are other English names" bits in the question (the latter implies we may have other strings which map elsewhere IMO) – Jonathan Allan – 2020-02-19T17:27:15.063
1@JonathanAllan My interpretation of that rule is that there needs to be some (not necessarily uniform) distribution across the houses and not just return Gryffindor for everything that isn't a special input. The statement is just to prove that your program actually does that – Cruncher – 2020-02-19T17:47:05.377
@JonathanAllan thanks for your feedback! I tried making the challenge more clear, but my intention was exactly as Cruncher said :) – RGS – 2020-02-19T19:36:46.583
@S.S.Anne - 128 :-) – Bob Jarvis - Reinstate Monica – 2020-02-20T22:03:44.647
@Bob ASCII is a 7-bit character set. There is no 128. – S.S. Anne – 2020-02-20T22:08:17.583