# Thorough though Thoreau threw, Troll throws through tough troughs

To each of these nine confusingly similar words, assign a number 1-9 in any way you like:

though
through
thorough
Thoreau
throw
threw
trough
tough
troll


Write a program that takes in a string. If the input is one of these nine words, output the number you assigned to it. If the input is not one of the words above, the program may do anything (including error or loop forever).

The words are case sensitive, e.g. Thoreau, should produce a number from 1-9 but thoreau will not necessarily do the same.

## Example

Suppose you assign the numbers as follows:

though   9
through  2
thorough 7
Thoreau  6
throw    3
threw    5
trough   4
tough    1
troll    8


Then when tough is input, 1 should be output.
When through is input, 2 should be output.
When throw is input, 3 should be output.
. . .
When though is input, 9 should be output.

All other inputs may do anything.

## Details

• Take the input string via stdin or the command line and output to stdout.
• The output may contain a single trailing newline.
• Instead of a program, you may write a function that takes in a string and prints the result normally or returns it.
• The shortest submission in bytes wins.
• Darn it! I had a clever solution to output zero when not found by using the Python string find method. Then the rules changed. Clever idea not so clever now. May 17 '15 at 8:15
• @CarpetPython My bad really. Don't hesitate to downvote if you feel unsatisfied with the change. (Though I promise to everyone there will be no more changes.) May 17 '15 at 8:21
• That's ok. I think my answer is still valid (though I little verbose). May 17 '15 at 8:29
• Can I make it work regardless of capitalization? May 17 '15 at 14:25
• @ASCIIThenANSI as long as it works for the 9 cases May 17 '15 at 18:21

# CJam, 11 9 7 bytes

q1b2+B%


How it works:

We are making use of the fact that the sum of the ASCII codes + 2 moded with 11 gives very nice order of 1 through 9 and then 10 for the nine concerned words. Here is the ordering:

through -> 1
thorough -> 2
tough -> 3
Thoreau -> 4
throw -> 5
threw -> 6
trough -> 7
though -> 8
troll -> 9


Code explanation:

q               e# Read the input
1b             e# Sum the ASCII code values of all characters in this word
2+           e# Increment the sum by 2
B%         e# Mod by 11 and automatically print the mod result at the end


4 bytes saved thanks to user23013

Try it online here

• How are we supposed to try it? (not everyone speaks CJam, some of us speak lolcode) May 17 '15 at 11:27
• @Behrooz There is a link. Click on it, put your input in the input section, Run. Not sure how it can be easier :) May 17 '15 at 11:43
• Holly shit, I was thinking how am I supposed to give it the list of words. nice one May 17 '15 at 11:54
• @user23013 Damn! Every single time! May 17 '15 at 14:11
• Or q1b2+B%. May 18 '15 at 0:33

# Pyth, 8 chars

e%Cz8109


Try it online: Demonstration or Test Suite

I'm using the assignment:

though   5
through  9
thorough 4
Thoreau  7
throw    3
threw    2
trough   8
tough    6
troll    1


### Explanation:

   z       input()
C        convert to int (convert each char to their ASCII value
and interprete the result as number in base 256)
%  8109   modulo 8109
e          modulo 10


Btw, I found the magic number 8109 by using this script: fqr1 10Sme%CdT.z1.

• Will this not have false-positives? May 17 '15 at 8:41
• @alexander-brett What exactly do you mean? The output of all other inputs is not specified in the OP. We can output anything we want. May 17 '15 at 8:42
• Sorry, I missed that edit to the OP. That's a shame :P May 17 '15 at 8:43

# Python 2, 92 54 bytes

print'K{7j)yE<}'.find(chr(hash(raw_input())%95+32))+1


The index string is created with for word in words: print chr(hash(word)%95+32),. As pointed out in Jakube's answer, the hash function will give different results depending on Python version. This index string is computed on 64 bit Python 2.7.6.

Longer (92 bytes) but less cryptic answer:

print'though through thorough Thoreau throw threw trough tough troll'.find(raw_input())/7+1


The programs returns 1-9 for though through thorough Thoreau throw threw trough tough troll in that order. When the input is not found, find will return a -1 which conveniently turns into a zero after the +1.

• Note that the 0 stuff is no longer required. Sorry for changing it on you. May 17 '15 at 8:11
• Thanks for noticing the effort. It was a good solution for a little while... May 17 '15 at 8:18
• @CarpetPython Nice use of floor-division -- it works out surprisingly neatly.
– xnor
May 17 '15 at 8:22

# Python 2.7.9 32 bit version, 22 bytes

lambda x:hash(x)%78%10


Notice, the version is really important here. You will get different results if your using a 64 bit version of Python. Since the hash method will compute 64 bit hash values instead of 32 bit.

The assignment is:

though  => 5   through => 6   thorough => 8
Thoreau => 7   throw   => 3   threw    => 1
trough  => 9   tough   => 4   troll    => 2


Try it online: http://ideone.com/Rqp9J8

• Wow, so all this time, you were iterating through language versions and operating system bits ? :P May 17 '15 at 15:25
• Very nice answer. Did you find the constant 78 through mathematics, automated iteration, or some guesses? May 17 '15 at 16:06
• @CarpetPython Just a simple brute-force loop which goes through all possible modules. Once sorted(...)==range(1,10), I stopped. May 17 '15 at 16:09

# Pyth, 7 bytes

et%Cz31


I am using the following asignment:

though   8
through  3
thorough 1
Thoreau  5
throw    4
threw    7
trough   6
tough    2
troll    9


Cz interprets the input as a base 256 number. Then, we take this mod 31, subtract 1, and take the result mod 10. Equivalent pseudocode:

((base_256(input()) % 31) - 1) % 10


# Vyxal, 6 bytes

C∑⇧11%


Try it Online!

Cjam port.

# Japt, 6 bytes

nH %BÉ


## Explanation

Takes advantage of the fact that, when parsing a base-n string to an integer, JavaScript will stop parsing if it encounters a digit greater than n and return the result up to that point. By using base-32 here (digits 0-v) the ws in "threw" and "throw" are, essentially, ignored.

nH      :Convert from base-32
%B   :Modulo 11
É  :Subtract 1


Here's a breakdown of the formula applied to each word:

Input nH %B É
through 31738067473 2 1
tough 31226385 3 2
troll 31318709 4 3
trough 1002207761 5 4
though 991722001 6 5
throw 968568 7 6
threw 968558 8 7
thorough 1015520459281 9 8
Thoreau 31735003486 10 9

## JavaScript, 22 bytes

A direct translation - doesn't seem worth posting it separately.

f=
U=>parseInt(U,32)%11-1
o.innerText=["through","tough","troll","trough","though","throw","threw","thorough","Thoreau"].map(s=>f(s)+": "+s).join\n
<pre id=o><pre>

# Python 2, 27 bytes

f=lambda w:int(w,34)%444/46


With this assignment:

>>> for w in "though through thorough Thoreau throw threw trough tough troll".split(): print f(w),w
...
9 though
7 through
3 thorough
8 Thoreau
2 throw
5 threw
6 trough
1 tough
4 troll


Several variations are possible, e.g.

f=lambda w:int(w,35)/159%10


# C (gcc), 66 bytes

h,k;f(char*s){for(h=33;*s;)h^=*s++;h=strchr(k="(Z5qW]2@H",h)-k+1;}


Try it online!

• May require the -O compiler flag. h;f(char*s){for(h=33;*s;)h^=*s++;h=index("(Z5qW]2@H",h)-"H"+9;} Aug 6 '18 at 7:26

# Java 8, 53 25 bytes

s->(s.chars().sum()+2)%11


or

s->-~-~s.chars().sum()%11


Port of @Optimizer's CJam answer, because it (most likely) cannot be done any shorter in Java..

Try it online.

• Java has parseInt, doesn't it? Would a port of my solution not be shorter? Jan 31 '18 at 12:25
• @Shaggy Java indeed has parseInt with given base, but unfortunately it's quite byte-excessive due to static class requirement: Long.parseLong(...,32) as shortest variant. In addition, it seems to fail for "throw" (and "threw" as well) in Java for some reason. w is outside the base-32 range it seems (and using 33 gives incorrect results). Jan 31 '18 at 13:24

# Jelly, 7 bytes

OS+2%11


Try it online!

Boring Jelly port of the fantastic CJam answer.