13
\$\begingroup\$

Introduction

In the land of [Insert cool name here], people don't buy things with money, because everyone has a severe allergy to paper. They pay eachother with words! But how is that? Well, they give each letter number values:

a=1,b=2,c=3,etc. 

(With some other special rules that will be described later)

In this challenge, your task will be to calculate the value of sentences.

Challenge

You will take an input which will be a sentence. You may assume the input has no newlines or trailing spaces. The challenge will be to calculate the value of the sentence, using these rules:

a=1,b=2,c=3,etc.  
  • A capital letter is worth 1.5 times it's corresponding lowercase letter

H=h*1.5

So, the word

cab

Would be worth c+a+b = 3+1+2 = 6

But the word Cab with a capital c would be worth (c*1.5)+a+b = 4.5+1+2 = 7.5 So if your program input was "Cab" your program would output 7.5

  • All non alphabetic characters are worth 1.

This is code golf, so shortest answer in bytes wins. Good luck!

\$\endgroup\$
  • 4
    \$\begingroup\$ Wait, money is paper?? I always thought it was either shiny metal discs or some sort of magic invoked by swiping the sacred card. \$\endgroup\$ – Geobits Aug 21 '15 at 14:08
  • 2
    \$\begingroup\$ Even U.S. banknotes are actually made of cotton and linen..but I guess the people of [Insert cool name here] hadn't thought of that yet. \$\endgroup\$ – Arcinde Aug 21 '15 at 14:31
  • \$\begingroup\$ Are trailing zeros allowed? E.g., printing 7.0 instead of 7? \$\endgroup\$ – kirbyfan64sos Aug 21 '15 at 16:37
  • \$\begingroup\$ @kirbyfan64sos Trailing 0s are allowed. \$\endgroup\$ – TreFox Aug 21 '15 at 21:32
  • \$\begingroup\$ What about spaces? \$\endgroup\$ – juniorRubyist Jun 3 '18 at 4:56

21 Answers 21

13
\$\begingroup\$

Python 3, 71 65 61 bytes

lambda z:sum((ord(s)*1.5**(s<'_')-96)**s.isalpha()for s in z)

By an extraordinary coincidence, (ord(s)-64)*1.5 is equal to ord(s)*1.5-96, so we only have to write -96 once. The rest is pretty straight forward.

Edit: Shaved off some bytes using exponentiation shenanigans.

\$\endgroup\$
5
\$\begingroup\$

Python 2, 120 102 bytes

Edit:

e=raw_input()
print sum([ord(l)-96for l in e if not l.isupper()]+[1.5*ord(l)-96for l in e if l.isupper()])

First submission, not so golfy but one has to start somewhere.

def s2(p):
 c=0
 for l in p:
  if l.isupper():
   c+=(ord(l.lower())-96)*1.5
  else:
   c+=ord(l)-96
 return c
print s(raw_input())
\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! This post contains some tips for code golfing in Python that may help you better your score. You could start by decreasing the amount of whitespace. \$\endgroup\$ – Alex A. Aug 20 '15 at 22:47
  • \$\begingroup\$ In your second list comprehension why not replace (ord(l.lower())-96)*1.5 with 1.5*ord(l)-96. You know that l is upper so just work with that and multiply out to remove the parens(64*1.5=96). \$\endgroup\$ – ruler501 Aug 21 '15 at 5:51
  • \$\begingroup\$ You can also remove the space between a closing paren and for in the comprehensions. \$\endgroup\$ – Alex A. Aug 21 '15 at 6:15
  • \$\begingroup\$ If I'm not mistaken, you could make this even shorter by simply making it a lambda with e as a parameter that returns the result. \$\endgroup\$ – Alex A. Aug 21 '15 at 23:28
  • \$\begingroup\$ In the "comprehension" one ? \$\endgroup\$ – Baart Aug 21 '15 at 23:30
5
\$\begingroup\$

Pyth, 23 20 bytes

sm|*hxGrdZ|}dG1.5 1z

Live demo and test cases.

Explanation

 m                 z    For each input character
    hxGrdZ              Get the value of it's lowercase form, or 0 for non-alphabetic characters
   *      |}dG1.5       Multiply it by 1 if it's lowercase, 1.5 if uppercase
  |               1     If it's still zero, it's a non-alphabetic character, so use 1 as its value
s                       Sum of all the values

Quite a few creative uses of booleans values as integers here.

23-byte version:

sm+*hxGJrdZ|}dG1.5!}JGz

Live demo and test cases.

\$\endgroup\$
  • \$\begingroup\$ This outputs the wrong thing for . (all non-alphabetic characters should be worth 1.) \$\endgroup\$ – Lynn Aug 21 '15 at 1:20
  • 1
    \$\begingroup\$ @Mauris Fixed!! \$\endgroup\$ – kirbyfan64sos Aug 21 '15 at 1:34
4
\$\begingroup\$

Julia, 63 bytes

s->sum(c->isalpha(c)?(64<c<91?1.5:1)*(c-(64<c<91?'@':'`')):1,s)

This simply sums an array constructed via a comprehension that loops over the characters in the input string and performs arithmetic on their codepoints.

Ungolfed:

function char_score(c::Char)
    (64 < c < 91 ? 1.5 : 1) * (c - (64 < c < 91 ? '@' : '`')) : 1
end

function sentence_value(s::String)
    sum(char_score, s)
end

Thanks to Glen O for fixing the approach.

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

Stuck, 85 43 Bytes

Yeah yeah, I know, Python is shorter.. :P I'm using the same logic as Tryth now, for the most part.

s_"str.isalpha"fgl;l-|0Gc"_91<1.5;^*96-":++

Explanation:

s_                                            # Take input & duplicate
  "str.isalpha"fg                             # Filter for only alpha chars, save
                 l;l-|                        # Determine number of symbols in start string
                      0Gc                     # Get saved string, convert to char array
                         "_91<1.5;^*96-":     # Logic to find score for each letter
                                         ++   # Sum the list of nums, add to # of symbols
\$\endgroup\$
2
\$\begingroup\$

Python 2, 101 bytes

v=0
for x in raw_input():v+=(ord(x.lower())-96)*(1.5 if ord(x)<96 else 1)if x.isalpha()else 1
print v
\$\endgroup\$
1
\$\begingroup\$

CJam, 30 bytes

q:i91,64fm1.5f*32,5f-+1fe>f=:+

How this works (wow, I've never made one of these!):

   91,64fm1.5f*32,5f-+1fe>      Construct an array so that a[i] == score for chr(i)
q:i                             Read STDIN and convert to ASCII codes
                          f=    Index each from the array
                            :+  Sum the result
\$\endgroup\$
1
\$\begingroup\$

F#, 168 bytes

Not really golfed yet, but a start:

fun(w:string)->w|>Seq.map(fun c->if Char.IsLetter c then (if Char.IsUpper(c) then (float)(Math.Abs(64-(int)c))*1.5 else (float)(Math.Abs(96-(int)c))) else 1.0)|>Seq.sum

Here a more readable version:

let calc (w : string) =
    w
    |> Seq.map (fun c -> if Char.IsLetter c then (if Char.IsUpper(c) then (float)(Math.Abs(64 - (int)c)) * 1.5 else (float)(Math.Abs (96 - (int)c))) else 1.0)
    |> Seq.sum
\$\endgroup\$
1
\$\begingroup\$

K, 30

+/1^(,/1 1.5*(.Q`a`A)!\:1+!26)

.

k)+/1^(,/1 1.5*(.Q`a`A)!\:1+!26)"Programming Puzzles & Code Golf"
349f

How it works:

.Q`a`A generates two lists of lowercase and uppercase letters

k).Q`a`A
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

!:1+til 26maps each letter in each list from 1 to 26

k)(.Q`a`A)!\:1+!26
"abcdefghijklmnopqrstuvwxyz"!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Multiply the first list by 1, the last by 1.5

k)1 1.5*(.Q`a`A)!\:1+!26
"abcdefghijklmnopqrstuvwxyz"!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26f
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"!1.5 3 4.5 6 7.5 9 10.5 12 13.5 15 16.5 18 19.5 21 22.5 24 25.5 27 28.5 30 31.5 33 34.5 36 37.5 39

Raze into a single dictionary using ,/

k)(,/1 1.5*(.Q`a`A)!\:1+!26)
a| 1
b| 2
c| 3
d| 4
..

Map the characters in the input string to the relevant scores

k)(,/1 1.5*(.Q`a`A)!\:1+!26)"Programming Puzzles & Code Golf"
24 18 15 7 18 1 13 13 9 14 7 0n 24 21 26 26 12 5 19 0n 0n 0n 4.5 15 4 5 0n 10.5 15 12 6

Fill any null values with 1

k)1^(,/1 1.5*(.Q`a`A)!\:1+!26)"Programming Puzzles & Code Golf"
24 18 15 7 18 1 13 13 9 14 7 1 24 21 26 26 12 5 19 1 1 1 4.5 15 4 5 1 10.5 15 12 6

Sum

k)+/1^(,/1 1.5*(.Q`a`A)!\:1+!26)"Programming Puzzles & Code Golf"
349f
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 121 bytes

l=process.argv[2].split(""),r=0;for(k in l)c=l[k],o=c.toLowerCase(),r+=(o.charCodeAt(0)-96)*(o===c?1:1.5);console.log(r);

call js file with node (node index.js "Cab")

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

MATLAB, 68 bytes

This takes advantage of the fact that characters are automatically casted to integers, and that boolean values can be summed as integers.

sum([t(t>96&t<132)-96,(t(t>64&t<91)-64)*1.5,t<65|(t>90&t<97)|t>122])
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 77 bytes

@_=split//,$ARGV[0];$i+=(ord)*(/[a-z]/||/[A-Z]/*1.5||96/ord)-96for@_;print$i

Tested on v5.20.2.

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

Javascript (ES6), 85 82 80 67 bytes

I love quick & easy challenges like this. :)

t=>[...t].map(c=>u+=(v=parseInt(c,36)-9)>0?v*(c>'Z'||1.5):1,u=0)&&u

This works by interpreting each char as a base-36 number, multiplying it by 1 or 1.5 if it's greater than 9 (a-z or A-Z), and giving 1 instead if not. As always, suggestions welcome!

\$\endgroup\$
  • 1
    \$\begingroup\$ The 0 in charCodeAt is bot necessary \$\endgroup\$ – Downgoat Aug 21 '15 at 5:38
  • \$\begingroup\$ @vihan Didn't know that; thanks for the tip! \$\endgroup\$ – ETHproductions Aug 21 '15 at 15:48
  • \$\begingroup\$ why not use toString(36) \$\endgroup\$ – l4m2 May 31 '18 at 15:46
  • \$\begingroup\$ @l4m2 I'm not sure how .toString(36) applies here. Do you mean something like parseInt(c,36)? Actually, that might be shorter... \$\endgroup\$ – ETHproductions Jun 1 '18 at 3:13
  • \$\begingroup\$ You can save some bytes by going recursive and using 2/3 when parseInt returns NaN: ([c,...t])=>c?(parseInt(c,36)-9||2/3)*(c>'Z'||1.5)+f(t):0 \$\endgroup\$ – Rick Hitchcock Jun 2 '18 at 11:49
0
\$\begingroup\$

Python 3: 86 85 Bytes

t=0
for c in input():k=ord(c)-64;t+=k*1.5if 0<k<27else k-32if 32<k<59else 1
print(t)
\$\endgroup\$
0
\$\begingroup\$

C# 81 Bytes

decimal a(string i){return i.Sum(c=>c>64&&c<91?(c-64)*1.5m:c>96&&c<123?c-96:1m);}

Call with (LinqPad):

a("Hello World").Dump();
\$\endgroup\$
0
\$\begingroup\$

PHP, 102 bytes

foreach(str_split($argv[1])as$c){$v=ord($c)-64;$s+=A<=$c&&$c<=Z?1.5*$v:(a<=$c&&$c<=z?$v-32:1);}echo$s;

Usage example:

$ php -d error_reporting=0 value.php cab
6
$ php -d error_reporting=0 value.php Cab
7.5
$ php -d error_reporting=0 value.php 'Programming Puzzles & Code Golf'
349

Nothing special in the algorithm. Each character from the first program's argument ($argv[1]) is checked against A and Z then a and z and counted accordingly.

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 108 Bytes

Decently competitive, I'm kinda surprised. Not too shabby for not having a compact Ternary operator.

Code:

$a=[char[]]$args[0];$a|%{$b=$_-64;If($b-in(1..26)){$c+=$b*1.5}ElseIf($b-in(33..58)){$c+=$b-32}Else{$c++}};$c

Explained:

$a=[char[]]$args[0]                # Take command-line input, cast as char array
$a|%{                              # For each letter in the array
  $b=$_-64                         # Set $b as the int value of the letter (implicit casting), minus offset
  If($b-in(1..26)){$c+=$b*1.5}     # If it's a capital, multiply by 1.5.
                         # Note that $c implicitly starts at 0 the first time through
  ElseIf($b-in(33..58)){$c+=$b-32} # Not a capital
  Else{$c++}                       # Not a letter
  }
$c                                 # Print out the sum
\$\endgroup\$
0
\$\begingroup\$

C, 85 bytes

float f(char*s){return(*s-96)*!!islower(*s)+1.5*(*s-64)*!!isupper(*s)+(*++s?f(s):0);}

The !! before islower and isupper are necessary, because the boolean values returned by these functions are not guaranteed to be 0 and 1 , true value was 1024 on my system indeed !

\$\endgroup\$
0
\$\begingroup\$

Candy, 26 22 bytes

(~"a"<{A#64-2/3*|A#96-}h)Z

Thanks to @Tryth for the factorization trick!

(~"a"<{A2/3*|A}#96-h)Z

Invokation is with the -I flag, as in candy -I "Cab" -e $prg

The code in it's long form is:

while     # loop while able to consume characters from stack
  peekA   # A gets stack to
  "a"
  less    # is pop() < "a"
  if
    pushA   # capitalized
    digit2
    div
    digit3
    mult
  else
    pushA   # lower case
  endif
  number
  digit9
  digit6
  sub
  popAddZ   # add pop() to counter register Z
endwhile
pushZ       # push Z onto stack as answer
\$\endgroup\$
0
\$\begingroup\$

Prolog (SWI), 101 bytes

Code:

X*Y:-X>64,X<91,Y is X*1.5-96;X>96,X<123,Y is X-96.
_*1.
p(L):-maplist(*,L,A),sumlist(A,B),write(B).

Explained:

X*Y:-X>64,X<91,       % When X is upper case
     Y is X*1.5-96    %      Y is 1.5 times charvalue starting at 1
     ;X>96,X<123,     % OR when X is lower case
     Y is X-96.       %      Y is charvalue starting at 1
_*1.                  % ELSE Y is 1
p(L):-maplist(*,L,A), % Get list of charvalues for all chars in string
      sumlist(A,B),   % Take sum of list
      write(B).       % Print

Example:

p(`Cab`).
7.5
\$\endgroup\$
0
\$\begingroup\$

PHP, 75 bytes

while(~$c=$argn[$i++])$r+=ctype_alpha($c)?ord($c)%32*(1+($c<a)/2):1;echo$r;

Run as pipe with -nr or try it online.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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