40
\$\begingroup\$

Challenge:

Take a string of upper or lower case letters as input (optional), and calculate the score that string would get in a game of Scrabble in English.

Rules:

The score of each letter is as follows (use this even if there are other versions of the game):

1 point: E, A, I, O, N, R, T, L, S, U
2 points: D, G
3 points: B, C, M, P
4 points: F, H, V, W, Y
5 points: K
8 points: J, X
10 points: Q, Z

The score of a string is simply the sum of the scores of each of the letters used. You may assume that you have plenty of tiles available, so long words, and words with many of the same letters are valid input.

Test cases:

ABC       ->    7
PPCG      ->   11
STEWIE    ->    9
UGPYKXQ   ->   33
FIZZBUZZ  ->   49
ABCDEFGHIJKLMNOPQRSTUVWXYZ  -> 87

The shortest answer in each language wins! The input and output formats are flexible, so you may take the input as an array of characters (upper or lower case) if you want.

\$\endgroup\$
  • 6
    \$\begingroup\$ I'm hoping to see a MATLAB/Octave solution. All my attempts were horribly long... =/ \$\endgroup\$ – Stewie Griffin Apr 17 '18 at 19:04
  • 4
    \$\begingroup\$ I'm hoping to see a Beatnik solution. Cuz, you know, that would be the right tool for the job. \$\endgroup\$ – Giuseppe Apr 17 '18 at 19:28
  • \$\begingroup\$ @StewieGriffin Does 85 bytes count as horribly long? \$\endgroup\$ – Luis Mendo Apr 17 '18 at 21:11
  • 3
    \$\begingroup\$ Hasn't Mathematica a built-in for it? \$\endgroup\$ – sergiol Apr 18 '18 at 0:30
  • 1
    \$\begingroup\$ @manassehkatz you should definitely give it a go! I highly recommend that you post the challenge in the Sandbox to get some feedback and help with it before posting it on the main site. Complex challenges are notoriously difficult to get right without any feedback. \$\endgroup\$ – Stewie Griffin Apr 18 '18 at 19:37

48 Answers 48

15
\$\begingroup\$

sed 4.2.2, 81

s/[QZ]/JD/g
s/[JX]/KB/g
s/K/FE/g
s/[FHVWY]/BE/g
s/[BCMP]/DE/g
s/[DG]/EE/g
s/./1/g

Output is in unary.

Reduces each letter to a combination of lower-scoring letters until all letters are 1-scorers. Then replaces those with 1s to give a unary count.

Try it online!

\$\endgroup\$
10
\$\begingroup\$

Haskell, 86 84 bytes

f s=length s+sum[n|x<-s,(n,y)<-zip(9:7:[1..])$words"QZ JX DG BCMP FHVWY K",x`elem`y]

Try it online!

Explanation

Most letters give a score of 1 and thus we don't need to keep track of these, instead we just decrement each score (saves 1 byte on 10 as well) and then add the length of the string to the resulting score.

Thanks @nimi for -2 bytes (rearanging the words and using [1..] instead of [4,3..])!

\$\endgroup\$
  • 1
    \$\begingroup\$ zip[1..]$words"DG BCMP FHVWY K . . JX . QZ" gives another alternative with equal length \$\endgroup\$ – Angs Apr 17 '18 at 21:54
10
\$\begingroup\$

Octave, 50 bytes

@(t)'				'/3*sum(65:90==t')'

Try it online!

Challenge accepted. Explanation:

@(t)             % Define anonymous function taking a single argument t.
    ' ... '/3    % Row vector with letter scores. Corresponds to char([1 3 3 2 ...]*3). 
                 % The factor 3 was necessary to avoid a newline.

*                % Dot product (yes, * is dot product, .* is not. Go figure). Equivalent to sum of element-wise products.
     65:90       % Alphabet
          ==t'   % Broadcast equality with input string.
 sum(         )  % Sum this matrix. Gives the count of each letter in the alphabet
               ' % Transpose into column vector for dot product
\$\endgroup\$
  • \$\begingroup\$ Very clever! Using unprintables was a nice touch! :) \$\endgroup\$ – Stewie Griffin Apr 18 '18 at 8:22
  • \$\begingroup\$ @StewieGriffin It's only one byte compared to -47 but that's code-golfing for you! \$\endgroup\$ – Sanchises Apr 18 '18 at 8:31
  • 1
    \$\begingroup\$ Sigh. Well and truly out golfed. I hadn't realised you could use == like that in Octave. Doesn't work in MATLAB. Good to know. \$\endgroup\$ – Tom Carpenter Apr 18 '18 at 8:43
  • 2
    \$\begingroup\$ @TomCarpenter I don't mean to rub any salt in the wound, but the 'old' way of doing this (with bsxfun) is also shorter at 61 bytes: Try it online! \$\endgroup\$ – Sanchises Apr 18 '18 at 8:57
  • 3
    \$\begingroup\$ WAT 50 bytes I don't even \$\endgroup\$ – Luis Mendo Apr 18 '18 at 10:54
8
+500
\$\begingroup\$

Beatnik, 733 bytes

Since it really had to be done, here it is. It was a really nasty to debug and provided a few challenges.

Input must be uppercase letters only. Output is unary (hope that is OK?)

J K ZZZZZZK Z ZD ZB ZZZZZZZZZZZZZZZZZA K A Z ZD ZB ZZZZZZZZZZZZZZZKF K A Z ZD ZB ZZZZZZZZZZZZZZZB K A Z ZD ZB ZZZZZZZZZZZZZZZ K A Z ZD ZB ZZZZZZZZZZZZZZKD K A Z ZD ZB ZZZZZZZZZZZZZD K A Z ZD ZB ZZZZZZZZZZZZZD K A Z ZD ZB ZZZZZZZZZZZZ K A Z ZD ZB ZZZZZZZZZZZZB K A Z ZD ZB ZZZZZZZZZKA K A Z ZD ZB ZZZZZZZZZKF K A Z ZD ZB ZZZZZZZZZZK K A Z ZD ZB ZZZZZZZZZB K A Z ZD ZB ZZZZZZZZZB K A Z ZD ZB ZZZZZZZZKD K A Z ZD ZB ZZZZZZZK K A Z ZD ZB ZZZZKB K A Z ZD ZB ZZZZZZKF K A Z ZD ZB ZZZZZZB K A Z ZD ZB ZZZZZFB K A Z ZD ZB ZZZZZA K A Z ZD ZB ZZZAK K A Z ZD ZB ZZZ K A Z ZD ZB ZD K A Z ZD ZB ZKB K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K A ZKA ZZZZZZZZZZZZZZZZZZY

Try it online!

General process is:

  • get character from input
  • subtract 65
  • check if result is 0
    • if 0 jump specified amount of words.
    • otherwise subtract 1 and repeat check.
  • the jump targets are push print operations followed be a loop back to beginning of program.

Ends with an error.

A more complete explanation:

J K ZZZZZZK Z ZD               # Get input and subtract 65
ZB ZZZZZZZZZZZZZZZZZA K A Z ZD # Character A - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZZZKF K A Z ZD  # Character B - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZZZB K A Z ZD   # Character C - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZZZ K A Z ZD    # Character D - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZZKD K A Z ZD   # Character E - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZD K A Z ZD     # Character F - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZD K A Z ZD     # Character G - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZ K A Z ZD       # Character H - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZB K A Z ZD      # Character I - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZKA K A Z ZD        # Character J - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZKF K A Z ZD        # Character K - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZK K A Z ZD        # Character L - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZB K A Z ZD         # Character M - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZB K A Z ZD         # Character N - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZKD K A Z ZD         # Character O - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZK K A Z ZD           # Character P - if 0 jump to print, otherwise subtract 1
ZB ZZZZKB K A Z ZD             # Character Q - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZKF K A Z ZD           # Character R - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZB K A Z ZD            # Character S - if 0 jump to print, otherwise subtract 1
ZB ZZZZZFB K A Z ZD            # Character T - if 0 jump to print, otherwise subtract 1
ZB ZZZZZA K A Z ZD             # Character U - if 0 jump to print, otherwise subtract 1
ZB ZZZAK K A Z ZD              # Character V - if 0 jump to print, otherwise subtract 1
ZB ZZZ K A Z ZD                # Character W - if 0 jump to print, otherwise subtract 1
ZB ZD K A Z ZD                 # Character X - if 0 jump to print, otherwise subtract 1
ZB ZKB                         # Character Y - if 0 jump to print, otherwise subtract 1
K ZZZZKF KF                    # Jump Point for print 1111111111
K ZZZZKF KF                    #
K ZZZZKF KF                    # Jump Point for print 11111111
K ZZZZKF KF                    #
K ZZZZKF KF                    #
K ZZZZKF KF                    # Jump Point for print 11111
K ZZZZKF KF                    # Jump Point for print 1111
K ZZZZKF KF                    # Jump Point for print 111
K ZZZZKF KF                    # Jump Point for print 11
K ZZZZKF KF                    # Jump Point for print 1
K A ZKA ZZZZZZZZZZZZZZZZZZAAAA # Jump back to start
\$\endgroup\$
8
\$\begingroup\$

Brain-Flak, 210, 204, 198, 184, 170 bytes

({<([{}]<>(({}{}))(([][][][][])<((([]())<([][])>))((((()))))>)[](((()()())<((()))>)((())()()()()))((())()()())((()())()())[]((((())())()))(())){({}<{}>())}>{}{}<{{}}><>})

Try it online!

Thanks to @JoKing for saving 14 bytes!

Readable version:

({              # For each character

                # Push array of letter scores
                # Also adjust character to 1-indexing
        <([{}]<>
        (({}{}))    # Push 2 0s
        (([][][][][])   # 10
        <((([]())   # 4
        <([][])>    # 8
        ))      # 4,4
        ((((()))))> # 1,1,1,1
        )       # 10
        []      # Add 12 to difference
        (((()()())  # 3
        <((()))>    # 1,1
        )       # 3
        ((())()()()())) # 1, 5
        ((())()()())    # 1, 4
        ((()())()())    # 2, 4
        []      # Add 22 to difference
        ((((())())()))  # 1,2,3
        (())        # 1
        )   # Push 65-char

        {({}<{}>())} # Index character into score array
        >
        {}{}         # Add score to running total
        <{{}}><>     # Clear the stack

})               # Implicit print of total score
\$\endgroup\$
  • \$\begingroup\$ 170 bytes \$\endgroup\$ – Jo King Apr 18 '18 at 1:47
  • 2
    \$\begingroup\$ For a given definition of 'readable' :) \$\endgroup\$ – Matt Lacey Apr 19 '18 at 11:59
  • \$\begingroup\$ I made an edit to try and clarify the explanation, feel free to rollback if you find an issue. \$\endgroup\$ – Kamil Drakari Apr 24 '18 at 20:31
7
\$\begingroup\$

Pyth, 40 bytes

sm+2x.e}dbc." zØÍ jÙ¹>;%OG5§"\ 1

Try it here

Explanation

sm+2x.e}dbc." zØÍ jÙ¹>;%OG5§"\ 1
 m                              Q  For each character in the (implicit) input...
    x.e  b                     1   ... find the first index in...
          c." zØÍ jÙ¹>;%OG5§"\     ['dg','bcmp','fhvwy','k','','','jx','','qz']
       }d                          ... containing the character...
  +2                               ... 2-indexed.
s                                  Take the sum.
\$\endgroup\$
7
\$\begingroup\$

Python 2, 78 bytes

lambda x:sum(map(('ABCEIKLMNOPRSTU'+'BCDGMPQZ'*2+'FHJJKQQVWXXYZZ'*4).count,x))

Try it online!

Shorter version, port of DanielIndie's answer, 71 70 bytes

-1 byte thanks to Sunny Patel

lambda x:sum(int('02210313074020029000033739'[ord(c)-65])+1for c in x)

Try it online!

\$\endgroup\$
7
\$\begingroup\$

JavaScript (Node.js), 71 66 63 62 bytes

  • @Arnauld awesome as always reducing 7 bytes
  • thanks to l4m2 for rducing by 1 byte
s=>Buffer(s).map(x=>s="02210313074020029000033739"[x-65]-~s)|s

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Java 8, 75 71 70 bytes

s->s.chars().map(c->"\n\n".charAt(c-65)).sum()

-1 byte by changing "02210313074020029000033739".charAt(c-65)-47 to unprintables (and two \n) so the -47 can be removed. Inspired by @Sanchises' Octave answer.

Try it online.

s->          // Method with String parameter and integer return-type
  s.chars()  //  Loop over the characters as IntStream
   .map(c->"\n\n".charAt(c-65))
             //   Convert the character to its value
   .sum()    //   And sum it all together
\$\endgroup\$
5
\$\begingroup\$

Octave / MATLAB, 85 bytes

@(x)sum([1:4 7 9]*any(reshape(char(strsplit('DG BCMP FHVWY K JX QZ')),6,1,5)==x,3)+1)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Better than my attempts :-) still longer than I would have thought before I tried it though... You had a very different approach to it! \$\endgroup\$ – Stewie Griffin Apr 18 '18 at 6:01
5
\$\begingroup\$

Jelly, 19 bytes

Oị“ÆẠḃbṂƬɠF#ṁ²’ḃ⁵¤S

A monadic link accepting a list of upper-case characters which returns an integer

Try it online! Or see the test-suite.

How?

Oị“ÆẠḃbṂƬɠF#ṁ²’ḃ⁵¤S - Link: list of characters
O                   - ordinals ('A'->65, B->66...)
                 ¤  - nilad followed by link(s) as a nilad:
  “ÆẠḃbṂƬɠF#ṁ²’     -   literal 14011114485013321424185131
                ⁵   -   literal 10
               ḃ    -   bijective-base = [1,3,10,1,1,1,1,4,4,8,4,10,1,3,3,2,1,4,2,4,1,8,5,1,3,1]
 ị                  - index into (1-based & modular) (vectorises)
                    -  i.e. mapping from: O P  Q R S T U V W X Y  Z A B C D E F G H I J K L M N)
                  S - sum
\$\endgroup\$
5
\$\begingroup\$

R, 90 63 bytes

function(W,u=utf8ToInt)sum(u('

')[u(W)-64])

Try it online!

Takes input as an uppercase string. R handles unprintables and multiline strings without issues, so that's nice. Now we're almost twice the external package!

And because CRAN has so many random goodies:

R + ScrabbleScore 31 bytes

ScrabbleScore::sws(scan(,""),F)

Try it online!

Sadly, sws checks for validity by default.

\$\endgroup\$
  • \$\begingroup\$ Had a play with the score list and trimmed a couple \$\endgroup\$ – MickyT Apr 18 '18 at 20:44
  • \$\begingroup\$ @MickyT nice! I played around with unprintables and re-using utf8ToInt instead of match and managed to get a few more down! \$\endgroup\$ – Giuseppe Apr 18 '18 at 22:04
3
\$\begingroup\$

05AB1E, 21 bytes

Takes input as a lowercase list of characters.

•_JÊ¿ùã$Ƶ½œM•11вAIkèO

Try it online! or as a Test suite

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

Octave, 73 bytes

@(x)sum('09977433333222211'(([~,y]=ismember(x,'QZJXKFHVWYBCMPDG'))+1)-47)

Try it online!

Uses ismember to map each character in the input stream x onto it's index in the lookup string 'QZJXKFHVWYBCMPDG'. Any element not found will be mapped to an index of 0 (this will include the 1-point characters).

Next we add 1 to the index to make the 0's become valid 1-index references, and lookup into the string '09977433333222211'. This is one element longer than the first lookup string. The digits represent the point value of each element in the original string, minus 1, with the extra element being a '0' at the beginning .

Finally the resultant string is converted to integers by subtracting 47 ('0'-1), yielding the point value for each letter, and all point values are then summed.

\$\endgroup\$
  • 1
    \$\begingroup\$ Very clever! :) \$\endgroup\$ – Stewie Griffin Apr 18 '18 at 7:44
3
\$\begingroup\$

Emojicode, 358 bytes

🐖🔥➡️🔡🍇🍮s 0🔂l🍡🐕🍇🍮s➕s🍺🐽🍯🔤a🔤1🔤e🔤1🔤i🔤1🔤l🔤1🔤n🔤1🔤o🔤1🔤r🔤1🔤s🔤1🔤t🔤1🔤u🔤1🔤d🔤2🔤g🔤2🔤b🔤3🔤c🔤3🔤m🔤3🔤p🔤3🔤f🔤4🔤h🔤4🔤v🔤4🔤w🔤4🔤y🔤4🔤k🔤5🔤j🔤8🔤x🔤8🔤q🔤10🔤z🔤10🍆🔡l🍉🍎🔡s 10🍉

Try it online!

Explanation:

I changed the variable names from single letters to more meaningful words, and expanded some parts of my code to hopefully make it more readable for people unfamiliar with the language. You can test the expanded program here.

🐋🔡🍇      👴 define a class that takes a string
 🐖🔥➡️🔡🍇    👴 define a method that returns a string
  🍦values🍯    👴 create int dictionary
   🔤a🔤1 🔤e🔤1 🔤i🔤1 🔤l🔤1 🔤n🔤1 🔤o🔤1 🔤r🔤1 🔤s🔤1 🔤t🔤1 🔤u🔤1 🔤d🔤2 🔤g🔤2
   🔤b🔤3 🔤c🔤3 🔤m🔤3 🔤p🔤3 🔤f🔤4 🔤h🔤4 🔤v🔤4 🔤w🔤4 🔤y🔤4 🔤k🔤5 🔤j🔤8 🔤x🔤8
   🔤q🔤10 🔤z🔤10
  🍆        👴 ^ dictionary contains letters(keys) and their numerical values

  🍮score 0                         👴 declare 'score' variable and set to 0
   🍦iterator🍡🐕                     👴 transform input string to iterator
    🔂letter iterator🍇                👴 iterate over each byte in input string
     🍮score➕score 🍺🐽values 🔡letter   👴 add value of each letter to score
   🍉
  🍎🔡score 10    👴 return the score as a string
 🍉
🍉

🏁🍇          👴 begin the program here
 😀🔥🔤abc🔤    👴 call scoring method and print the score
 😀🔥🔤ppcg🔤    👴 repeat with other test cases
 😀🔥🔤stewie🔤
 😀🔥🔤fizzbuzz🔤
 😀🔥🔤abcdefghijklmnopqrstuvwxyz🔤
🍉
\$\endgroup\$
  • 6
    \$\begingroup\$ ouch... my eyes... is there an option on golf.se to hide some specific langages? ^^ \$\endgroup\$ – Olivier Dulac Apr 19 '18 at 12:15
  • \$\begingroup\$ @OlivierDulac There's probably a way to prevent the browser from rendering emoji specially. They do each have standard Unicode black and white characters associated with them. \$\endgroup\$ – mbomb007 Apr 20 '18 at 15:19
3
\$\begingroup\$

Haskell, 66 bytes

sum.map(\c->1+read["02210313074020029000033739"!!(fromEnum c-65)])

Try it online! Same approach as DanielIndie's JavaScript answer.


Haskell, 82 81 bytes

sum.map(\c->1+sum(read.pure<$>lookup c(zip"DGBCMPFHVWYKJXQZ""1122223333347799")))

Try it online!

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

PowerShell, 60 bytes

$args|% t*y|%{$s+='02210313074020029000033739'[$_-65]-47}
$s

Try it online!

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

Japt, 36 bytes

£2+`dg
bcmp
fhvwy
k


jx

qz`·bøX
x

Takes input as a lowercase string, returns a number.
Short explanation:

£2+`dg
¬       // Split the input into chars,
 £      // then map over each char, returning
  2+`dg // 2 plus

qz`·bøX
    bøX // the char's index in
qz`·    // the hardcoded string split by newlines.
x       // And finally sum the whole thing.

Try it online!

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

Ruby, 60 bytes

->s{s.sum{|c|"BDDCBECEBIFBDBBDKBBBBEEIEK"[c.ord-65].ord-65}}

Try it online!

A lambda, accepting input as an array of (uppercase) characters and returning an integer.

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

Perl 5 -pF, 50 bytes

_1DG2BCMP3FHVWY4K7JX9QZ=~/\d\D*$_/,$\+=1+$&for@F}{

Try it online!

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

Gforth, 109 Bytes

: V s" 1332142418513113:11114484:" ; : C 0 NAME 0 DO DUP C@ 65 - V DROP + C@ 48 - ROT + SWAP 1+ LOOP DROP . ;

Input must be uppercase:
C PPCG 11 OK

Readable

\ String used as table with values for each letter in the alphabet
\ : follows 9 in the ASCII-table
: V
   s" 1332142418513113:11114484:"
;

: C
   0                   \ Initialize sum        ( sum               )
   NAME                \ Get the string        ( sum  c-addr count )
   0 DO                \ Start of loop         ( sum  c-addr       )
      DUP C@           \ Get letter            ( sum  c-addr char  )
      65 -             \ Calculate table index ( sum  c-addr index )
      V DROP + C@      \ Get table entry       ( sum  c-addr entry )
      48 -             \ Calculate entry value ( sum  c-addr value )
      ROT + SWAP       \ Update sum            ( sum' c-addr       )
      1+               \ Next character        ( sum' c-addr'      )
   LOOP
   DROP .              \ Drop c-addr and print result
;

Try it online!

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

Perl 6, 52 bytes

{TR/A..Z/02210313074020029000033739/.comb.sum+.ords}

Try it online!

Maps every character to a digit, and sums them. And adds 1 for each character because there isn't a digit 10 without incurring unicode bytes.

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

Retina 0.8.2, 41 bytes

T`BCDGJKMPQXZF\HVWY`221174229793
.
$*..
.

Try it online! Link includes test cases. Explanation: Like the Haskell answer, nontrivial letters are translated to 1 less than their score, and 1 is added later when the characters are converted to unary. Putting FHVWY last allows them all to map to a score of 3 + 1.

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

C++, 95 bytes

char*m="02210313074020029000033739";
int f(char*p){int n=0;while(*p)n+=m[*p++-65]-47;return n;}

Try it online (not a TIO link sorry)

Explanation:

  • Declares m, an array of the values of each letter in order, minus 1. The minus 1 is because of Q and Z: I couldn't have a two digit number in there
  • Iterates through through the string p until we get to null character, and adds the score of the number (*p gives us the letter, and -65 so we can properly index the array). Since m is a char* it converts to a char so we minus 48 so bring it back to 0, but add 1 since m is declared as one score less for each character.

I'm not an avid poster here so I hope I've done this correctly. I believe they returning n counts as printing the value, and that declaring a function is fine.

\$\endgroup\$
  • \$\begingroup\$ Very nice! The only byte you can save, is the newline: Try it online! \$\endgroup\$ – movatica Apr 30 at 21:39
2
\$\begingroup\$

C (gcc), 78 72 bytes

i;f(char*s){for(i=0;*s;)i+="\n\n"[*s++-65];s=i;}

There are actually 26 characters in that string. See the code rendered properly and run it here.

Thanks to gastropner for golfing 6 bytes.

Ungolfed version:

i; // declare a variable to store the score; it is implicitly of type int
f(char* s) { // function taking a string as argument and implicitly returning an int
    for(i = 0; // initialize the score to 0
        *s; ) // iterate over the string until we hit terminating NUL byte
        i += "\n\n"[*s++ - 65]; // this is a 26-char string containing the ASCII equivalent of each numeric scrabble value; 65 is ASCII code for 'A', mapping the alphabet onto the string
    s = i; // implicitly return the score
}
\$\endgroup\$
2
\$\begingroup\$

Excel, 91 bytes

{=LEN(A1)+SUM(0+("0"&MID("02210313074020029000033739",CODE(MID(A1,ROW(A:A),1)&"z")-64,1)))}

Explanation:

  • Input is in cell A1
  • The formula must be entered as an array formula with Ctrl+Shift+Enter, which adds the curly brackets { } to both ends.
  • MID(A1,ROW(A:A),1) pulls out each character in turn (and a lot of empty values, too, since it's going to return as many values as there are rows in the sheet)
  • CODE(MID(~)&"z") pulls out the ASCII value for the each character. The &"z" appends a z to the end of the MID() result because CODE() doesn't like empty inputs. The ASCII value for z is higher than every capital letter, though, so it's effectively ignored later.
  • MID("02210313074020029000033739",CODE(~)-64,1) pulls out a letter from the score string based on its ASCII value adjusted down by 64 so the letters run 1-26 instead of 65-90.
  • "0"&MID(~) prepends a zero to the MID() result because Excel won't let you do math with empty strings, of which there will be several.
  • 0+("0"&MID(~)) turns all those strings into numbers.
  • SUM(0+("0"&MID(~))) adds up all those strings that are now numbers.
  • LEN(A1)+SUM(~) adds the length of the input to the sum because all the values in the score string (02210313074020029000033739) were adjusted down by one so they would all be one digit long.

There's a very similar solution in Google Sheets but it comes in at 97 bytes because ArrayFromula() is longer than {} (but at least it can handle 0 + "" = 0).

=Len(A1)+ArrayFormula(Sum(0+Mid("02210313074020029000033739",Code(Mid(A1,Row(A:A),1)&"z")-64,1)))
\$\endgroup\$
  • 1
    \$\begingroup\$ Well done. I have a Excel solution using 26 SUBSTITUTE(), coming in at a hefty 527 bytes. \$\endgroup\$ – Wernisch Apr 20 '18 at 12:35
2
\$\begingroup\$

Wolfram Language (Mathematica), 74 bytes

Of course Wolfram|Alpha supports Scrabble scoring! This is an anonymous function.

Plus@@(First[WolframAlpha["Scrabble "<>##,"NumberData"]]&/@Characters[#])&

This doesn't work on TIO.

To run, go here, scroll down and click "Create a New Notebook »". The code to use in the notebook is in this TIO program so you can copy it. Paste each function call in its own code block. If you run too many in a single block, the execution won't complete.

Note that WolframAlpha sends a request using the Internet. Though there are other answers on PPCG that use it, I thought you should know.

This program uses the below shorter function, but calls it on each individual character of the input (sending a separate call to Wolfram|Alpha each time!)


This only works for input up to length 15, the width of a Scrabble board. (49 bytes)

First[WolframAlpha["Scrabble "<>#,"NumberData"]]&

Same as above, but will display the result in a box, along with whether the input is a valid Scrabble word. (45 bytes)

First[WolframAlpha["Scrabble "<>#,"Result"]]&
\$\endgroup\$
2
\$\begingroup\$

C# (.NET Core), 50 + 18 = 68 bytes

s=>s.Sum(c=>"02210313074020029000033739"[c-65]-47)

+18 bytes for using System.Linq;

Try it online!

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

K (oK), 60 38 bytes

Solution:

+/1+.:'"02210313074020029000033739"65!

Try it online!

Explanation:

Index into the scores, sum up result.

+/1+.:'"02210313074020029000033739"65! / the solution
                                   65! / input modulo 65 to get position in A-Z
       "02210313074020029000033739"    / index into the scores (1 point lower)
    .:'                                / value (.:) each (') to convert to ints
  1+                                   / increase by 1
+/                                     / sum up
\$\endgroup\$
2
\$\begingroup\$

Japt -x, 43 39 35 30 bytes

£2+`dglbcÛghvwylk¥ljx¥qz`qÊaøX

Try it online!

35 byte solution:

¬£Ò('0+#Ý03#740#È29e4+33739 g65nXc

Run it online

\$\endgroup\$

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.