21
\$\begingroup\$

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

11 Answers 11

19
\$\begingroup\$

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

\$\endgroup\$
10
  • \$\begingroup\$ How are we supposed to try it? (not everyone speaks CJam, some of us speak lolcode) \$\endgroup\$
    – Behrooz
    Commented May 17, 2015 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
    Commented May 17, 2015 at 11:43
  • \$\begingroup\$ Holly shit, I was thinking how am I supposed to give it the list of words. nice one \$\endgroup\$
    – Behrooz
    Commented May 17, 2015 at 11:54
  • 1
    \$\begingroup\$ @user23013 Damn! Every single time! \$\endgroup\$
    – Optimizer
    Commented May 17, 2015 at 14:11
  • 2
    \$\begingroup\$ Or q1b2+B%. \$\endgroup\$
    – jimmy23013
    Commented May 18, 2015 at 0:33
18
\$\begingroup\$

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.

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

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.

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

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

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Wow, so all this time, you were iterating through language versions and operating system bits ? :P \$\endgroup\$
    – Optimizer
    Commented May 17, 2015 at 15:25
  • 1
    \$\begingroup\$ Very nice answer. Did you find the constant 78 through mathematics, automated iteration, or some guesses? \$\endgroup\$ Commented May 17, 2015 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
    Commented May 17, 2015 at 16:09
5
\$\begingroup\$

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

Demonstration, test harness.

\$\endgroup\$
3
\$\begingroup\$

Japt, 6 bytes

nH %BÉ

Try it | Test all words


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>

\$\endgroup\$
2
\$\begingroup\$

Vyxal, 6 bytes

C∑⇧11%

Try it Online!

Cjam port.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\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
    Commented Aug 6, 2018 at 7:26
0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Java has parseInt, doesn't it? Would a port of my solution not be shorter? \$\endgroup\$
    – Shaggy
    Commented Jan 31, 2018 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\$ Commented Jan 31, 2018 at 13:24
0
\$\begingroup\$

Jelly, 7 bytes

OS+2%11

Try it online!

Boring Jelly port of the fantastic CJam answer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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