Thorough though Thoreau threw, Troll throws through tough troughs

19

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.

Calvin's Hobbies

Posted 2015-05-17T06:56:47.700

Reputation: 84 000

1Darn 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. – Logic Knight – 2015-05-17T08:15:16.780

@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.) – Calvin's Hobbies – 2015-05-17T08:21:01.210

That's ok. I think my answer is still valid (though I little verbose). – Logic Knight – 2015-05-17T08:29:33.213

Can I make it work regardless of capitalization? – ASCIIThenANSI – 2015-05-17T14:25:03.117

2@ASCIIThenANSI as long as it works for the 9 cases – Calvin's Hobbies – 2015-05-17T18:21:21.240

Answers

19

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

Optimizer

Posted 2015-05-17T06:56:47.700

Reputation: 25 836

How are we supposed to try it? (not everyone speaks CJam, some of us speak lolcode) – Behrooz – 2015-05-17T11:27:35.220

@Behrooz There is a link. Click on it, put your input in the input section, Run. Not sure how it can be easier :) – Optimizer – 2015-05-17T11:43:04.240

Holly shit, I was thinking how am I supposed to give it the list of words. nice one – Behrooz – 2015-05-17T11:54:32.793

:i:+ is the same as 1b. – jimmy23013 – 2015-05-17T14:10:13.393

1@user23013 Damn! Every single time! – Optimizer – 2015-05-17T14:11:54.200

In fact it's probably because I'm currently not posting so many answers. And if my answer is longer (sometimes much longer) than existing answers, I choose not to post in most cases. – jimmy23013 – 2015-05-17T14:18:31.997

Looking at the link: it should work, but it does not. Just a CJAM empty box, no code, no input. In Firefox. – edc65 – 2015-05-17T15:16:03.307

@edc65 some known issues with firefox. Try opening the link in chrome – Optimizer – 2015-05-17T15:21:38.937

2Or q1b2+B%. – jimmy23013 – 2015-05-18T00:33:50.560

ÇOÌ11% - 05AB1E, your method, 6 bytes. – Magic Octopus Urn – 2018-01-31T16:56:39.907

18

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.

Jakube

Posted 2015-05-17T06:56:47.700

Reputation: 21 462

Will this not have false-positives? – alexander-brett – 2015-05-17T08:41:20.943

5@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. – Jakube – 2015-05-17T08:42:36.953

Sorry, I missed that edit to the OP. That's a shame :P – alexander-brett – 2015-05-17T08:43:35.170

11

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.

Logic Knight

Posted 2015-05-17T06:56:47.700

Reputation: 6 622

Note that the 0 stuff is no longer required. Sorry for changing it on you. – Calvin's Hobbies – 2015-05-17T08:11:39.590

Thanks for noticing the effort. It was a good solution for a little while... – Logic Knight – 2015-05-17T08:18:57.200

3@CarpetPython Nice use of floor-division -- it works out surprisingly neatly. – xnor – 2015-05-17T08:22:02.177

7

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

Jakube

Posted 2015-05-17T06:56:47.700

Reputation: 21 462

2Wow, so all this time, you were iterating through language versions and operating system bits ? :P – Optimizer – 2015-05-17T15:25:57.243

1Very nice answer. Did you find the constant 78 through mathematics, automated iteration, or some guesses? – Logic Knight – 2015-05-17T16:06:38.717

3@CarpetPython Just a simple brute-force loop which goes through all possible modules. Once sorted(...)==range(1,10), I stopped. – Jakube – 2015-05-17T16:09:22.130

5

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.

isaacg

Posted 2015-05-17T06:56:47.700

Reputation: 39 268

1

Japt, 6 bytes

nH %BÉ

Try it | Check 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

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>

Shaggy

Posted 2015-05-17T06:56:47.700

Reputation: 24 623

1

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

ygramul

Posted 2015-05-17T06:56:47.700

Reputation: 161

0

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!

gastropner

Posted 2015-05-17T06:56:47.700

Reputation: 3 264

May require the -O compiler flag. h;f(char*s){for(h=33;*s;)h^=*s++;h=index("(Z5qW]2@H",h)-"H"+9;} – ceilingcat – 2018-08-06T07:26:03.647

0

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.

Kevin Cruijssen

Posted 2015-05-17T06:56:47.700

Reputation: 67 575

Java has parseInt, doesn't it? Would a port of my solution not be shorter?

– Shaggy – 2018-01-31T12:25:06.067

@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).

– Kevin Cruijssen – 2018-01-31T13:24:55.307

0

Jelly, 7 bytes

OS+2%11

Try it online!

Boring Jelly port of the fantastic CJam answer.

Comrade SparklePony

Posted 2015-05-17T06:56:47.700

Reputation: 5 784