Finding the Value of Words!

13

1

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!

Nico A

Posted 2015-08-20T21:57:32.113

Reputation: 2 390

4Wait, money is paper?? I always thought it was either shiny metal discs or some sort of magic invoked by swiping the sacred card. – Geobits – 2015-08-21T14:08:54.987

2Even 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. – jcai – 2015-08-21T14:31:15.400

Are trailing zeros allowed? E.g., printing 7.0 instead of 7? – kirbyfan64sos – 2015-08-21T16:37:55.667

@kirbyfan64sos Trailing 0s are allowed. – Nico A – 2015-08-21T21:32:51.350

What about spaces? – juniorRubyist – 2018-06-03T04:56:27.087

@juniorRubyist All non-alphabetic characters are worth 1. – Nico A – 2018-06-03T13:12:41.947

Answers

13

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.

Tryth

Posted 2015-08-20T21:57:32.113

Reputation: 750

5

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())

Baart

Posted 2015-08-20T21:57:32.113

Reputation: 151

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.

– Alex A. – 2015-08-20T22:47:29.157

In your second list comprehension why not replace (ord(l.lower())-96)1.5 with 1.5ord(l)-96. You know that l is upper so just work with that and multiply out to remove the parens(64*1.5=96). – ruler501 – 2015-08-21T05:51:24.627

You can also remove the space between a closing paren and for in the comprehensions. – Alex A. – 2015-08-21T06:15:28.143

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. – Alex A. – 2015-08-21T23:28:24.777

In the "comprehension" one ? – Baart – 2015-08-21T23:30:12.223

5

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.

kirbyfan64sos

Posted 2015-08-20T21:57:32.113

Reputation: 8 730

This outputs the wrong thing for . (all non-alphabetic characters should be worth 1.) – Lynn – 2015-08-21T01:20:06.597

1@Mauris Fixed!! – kirbyfan64sos – 2015-08-21T01:34:57.697

4

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.

Alex A.

Posted 2015-08-20T21:57:32.113

Reputation: 23 761

2

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

Kade

Posted 2015-08-20T21:57:32.113

Reputation: 7 463

2

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

Alex Blundell

Posted 2015-08-20T21:57:32.113

Reputation: 121

1

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

Lynn

Posted 2015-08-20T21:57:32.113

Reputation: 55 648

1

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!

ETHproductions

Posted 2015-08-20T21:57:32.113

Reputation: 47 880

1The 0 in charCodeAt is bot necessary – Downgoat – 2015-08-21T05:38:59.413

@vihan Didn't know that; thanks for the tip! – ETHproductions – 2015-08-21T15:48:20.023

why not use toString(36) – l4m2 – 2018-05-31T15:46:45.050

@l4m2 I'm not sure how .toString(36) applies here. Do you mean something like parseInt(c,36)? Actually, that might be shorter... – ETHproductions – 2018-06-01T03:13:51.857

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 – Rick Hitchcock – 2018-06-02T11:49:48.083

1

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

oopbase

Posted 2015-08-20T21:57:32.113

Reputation: 542

1

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])

Stewie Griffin

Posted 2015-08-20T21:57:32.113

Reputation: 43 471

1

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

tmartin

Posted 2015-08-20T21:57:32.113

Reputation: 3 917

1

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")

Marcel

Posted 2015-08-20T21:57:32.113

Reputation: 21

1

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.

msh210

Posted 2015-08-20T21:57:32.113

Reputation: 3 094

0

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.

Titus

Posted 2015-08-20T21:57:32.113

Reputation: 13 814

0

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)

Daniel Wakefield

Posted 2015-08-20T21:57:32.113

Reputation: 484

0

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();

Stephan Schinkel

Posted 2015-08-20T21:57:32.113

Reputation: 596

0

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.

axiac

Posted 2015-08-20T21:57:32.113

Reputation: 749

0

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

AdmBorkBork

Posted 2015-08-20T21:57:32.113

Reputation: 41 581

0

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 !

pawel.boczarski

Posted 2015-08-20T21:57:32.113

Reputation: 1 243

0

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

Dale Johnson

Posted 2015-08-20T21:57:32.113

Reputation: 509

0

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

Emigna

Posted 2015-08-20T21:57:32.113

Reputation: 50 798