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


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.


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.


  • 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.
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Logic Knight May 17 '15 at 8:15
  • \$\begingroup\$ @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.) \$\endgroup\$ – Calvin's Hobbies May 17 '15 at 8:21
  • \$\begingroup\$ That's ok. I think my answer is still valid (though I little verbose). \$\endgroup\$ – Logic Knight May 17 '15 at 8:29
  • \$\begingroup\$ Can I make it work regardless of capitalization? \$\endgroup\$ – ASCIIThenANSI May 17 '15 at 14:25
  • 2
    \$\begingroup\$ @ASCIIThenANSI as long as it works for the 9 cases \$\endgroup\$ – Calvin's Hobbies May 17 '15 at 18:21

10 Answers 10


CJam, 11 9 7 bytes


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

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

Pyth, 8 chars


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


   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.

  • \$\begingroup\$ Will this not have false-positives? \$\endgroup\$ – alexander-brett May 17 '15 at 8:41
  • 5
    \$\begingroup\$ @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. \$\endgroup\$ – Jakube May 17 '15 at 8:42
  • \$\begingroup\$ Sorry, I missed that edit to the OP. That's a shame :P \$\endgroup\$ – alexander-brett May 17 '15 at 8:43

Python 2, 92 54 bytes


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.

  • \$\begingroup\$ Note that the 0 stuff is no longer required. Sorry for changing it on you. \$\endgroup\$ – Calvin's Hobbies May 17 '15 at 8:11
  • \$\begingroup\$ Thanks for noticing the effort. It was a good solution for a little while... \$\endgroup\$ – Logic Knight May 17 '15 at 8:18
  • 3
    \$\begingroup\$ @CarpetPython Nice use of floor-division -- it works out surprisingly neatly. \$\endgroup\$ – 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

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

Pyth, 7 bytes


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

Demonstration, test harness.


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

Japt, 6 bytes

nH %BÉ

Try it | Check all words


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

JavaScript, 22 bytes

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

o.innerText=["through","tough","troll","trough","though","throw","threw","thorough","Thoreau"].map(s=>f(s)+": "+s).join`\n`
<pre id=o><pre>


C (gcc), 66 bytes


Try it online!

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

Java 8, 53 25 bytes




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

Try it online.

  • \$\begingroup\$ Java has parseInt, doesn't it? Would a port of my solution not be shorter? \$\endgroup\$ – Shaggy Jan 31 '18 at 12:25
  • \$\begingroup\$ @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). \$\endgroup\$ – Kevin Cruijssen Jan 31 '18 at 13:24

Jelly, 7 bytes


Try it online!

Boring Jelly port of the fantastic CJam answer.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.