17
\$\begingroup\$

My dad is a retired teacher, and he used to give combined spelling and math quizzes, where the student would spell a word, and then 'score' the word by adding up the letters, where a=1, b=2, etc. (e.g. cat = 3+1+20=24). This made grading the quizzes easier, as he would just have to check for incorrect 'scores' rather than incorrectly spelled words, and had the added benefit of testing 2 skills at once.

He hired a friend of mine to write a program that would score words for him, so he could generate lengthy answer keys without error. This problem is inspired by that program.

Requirements:

  1. Accept any word with uppercase and lowercase letters
  2. Return an error for any special characters, i.e. spaces, hyphens, @^%# etc.
  3. a=1, b=2,... and A=1, B=2,...
  4. Print the score of the word
  5. (Optional) check that the word is in a dictionary after scoring, and print a warning if it is not.
  6. No importing an external letter->number dictionary. You must generate it yourself.

Any language is acceptable. This is similar to the 'digital root battle,' but much simpler.

\$\endgroup\$
  • 2
    \$\begingroup\$ Is this supposed to be a code golf? \$\endgroup\$ – Peter Taylor Apr 23 '11 at 18:08
  • 2
    \$\begingroup\$ @Zach Using the code-golf tag. \$\endgroup\$ – Lowjacker Apr 23 '11 at 18:24
  • 2
    \$\begingroup\$ Did your dad even bother teaching the "I before E except after C" rule? \$\endgroup\$ – Nathan Merrill Dec 28 '15 at 12:28
  • 2
    \$\begingroup\$ Yeah, only checking scores? I'd spell cat as aaaaaaaaaaaaaaaaaaaaaaaa. Dad: Score is 24? That's right! \$\endgroup\$ – ericw31415 May 8 '16 at 14:13
  • 3
    \$\begingroup\$ @ericw31415 Every hashing function has collisions ;-). So far none of his students have tried that attack vector \$\endgroup\$ – Zach May 9 '16 at 15:39

33 Answers 33

1
2
0
\$\begingroup\$

K, 44

{$[=/x in\:a:.Q.a,.Q.A;+/(a!,/2#,1+!26)x;`]}

Returns ` for any non alphabetic input

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

VBA 120

Sub s(t)
For i=1 To Len(t)
a=Asc(Mid(t,i,1))-64
a=IIf(a>32,a-32,a)
If a<1 Or a>26 Then Print
b=b+a
Next
MsgBox b
End Sub

Print does not cause a compile-time error, though the syntax is invalid, which causes an exception to be thrown and the program to exit if, and only if, the character is not an upper- or lower-case letter.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C, 97 Chars

int r(c*z){int x=0;while(*z){char w=tolower(*(z++));if(w<96|w>122)return-1;x+=w-96;}return x;}

It returns -1 if there's an invalid character.

| improve this answer | |
\$\endgroup\$
1
2

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.