寻找文字的价值!


13

介绍

在[在这里插入很酷的名字]的土地上,人们不花钱买东西,因为每个人都对纸张过敏。他们用言语付钱给彼此!那怎么了 好吧,他们给每个字母数字值:

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

(以及一些其他特殊规则,将在以后进行描述)

在这个挑战中,您的任务是计算句子的值。

挑战

您将输入一个句子。您可能会假设输入内容没有换行符或尾随空格。挑战将是使用以下规则来计算句子的值:

a=1,b=2,c=3,etc.  
  • 大写字母的价值是对应的小写字母的1.5倍

H=h*1.5

所以,这个词

cab

值得 c+a+b = 3+1+2 = 6

但是Cab带有大写字母c 的单词值得。(c*1.5)+a+b = 4.5+1+2 = 7.5 因此,如果您的程序输入为“ Cab”,则程序将输出7.5

  • 所有非字母字符均值1。

这是代码高尔夫球,因此最短答案以字节为单位。祝好运!


4
等等,钱是纸吗?我一直以为它要么是闪亮的金属光盘,要么是刷卡刷出的某种魔法。
Geobits,2015年

2
甚至连美国的钞票实际上都是用棉麻制成的。.但是我想[在这里插入好名字的人]还没有想到这一点。
jcai 2015年

是否允许尾随零?例如,打印7.0而不是7
kirbyfan64sos

@ kirbyfan64sos允许尾随0。
Nico A

那空间呢?
juniorRubyist

Answers:


13

Python 3,71 65 61字节

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

碰巧的(ord(s)-64)*1.5是,它等于ord(s)*1.5-96,所以我们只需要写-96一次。其余的很简单。

编辑:使用幂运算的技巧削减一些字节。


5

Python 2中,120个 102字节

编辑:

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

第一次提交,不是那么打高尔夫球,但必须从某个地方开始。

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

欢迎来到编程难题和Code Golf!这篇文章包含一些使用Python打高尔夫球的技巧,这些技巧可能会帮助您提高得分。您可以从减少空白量开始。
Alex A.

在第二个列表中,为什么不将(ord(l.lower())-96)* 1.5替换为1.5 * ord(l)-96。您知道l较高,因此只需对其进行处理并相乘即可删除括号(64 * 1.5 = 96)。
Ruler501

您也可以删除右括号之间的空格和for理解中的空格。
Alex A.

如果我没记错的话,您可以通过简单地将其e设为lambda(作为返回结果的参数)来使其更短。
Alex A.

在“领悟”中?
2015年

5

Pyth,23 20字节

sm|*hxGrdZ|}dG1.5 1z

现场演示和测试案例。

说明

 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

在这里,布尔值作为整数有很多创造性的用法。

23字节版本:

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

现场演示和测试案例。


这会输出错误的信息.(所有非字母字符都应该值1.)
Lynn

1
@毛里斯固定!!
kirbyfan64sos

4

朱莉娅63字节

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

这只是对通过理解构造的数组求和,该理解遍历输入字符串中的字符并对其代码点执行算术运算。

取消高尔夫:

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

感谢Glen O解决这个问题。


2

卡住85 43字节

是的,我知道,Python较短

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

说明:

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


1

CJam,30个字节

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

工作原理(哇,我从来没有做过其中之一!):

   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

1

F#,168个字节

还不是真的打高尔夫球,而是一个开始:

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

这里是一个更具可读性的版本:

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

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

怎么运行的:

.Q`a`A 生成两个大小写字母的列表

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

!:1+til 26将每个列表中的每个字母从1映射到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

第一个列表乘以1,最后一个乘以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

使用以下语言将其放到单个字典中 ,/

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

将输入字符串中的字符映射到相关分数

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

用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

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

1

JavaScript,121字节

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

调用带有节点的js文件(节点index.js“ Cab”)


1

MATLAB,68字节

这利用了以下事实:字符会自动转换为整数,并且布尔值可以求和为整数。

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

1

Perl 5,77个字节

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

经过测试v5.20.2


1

Javascript(ES6),85 82 80 67字节

我喜欢这样的快捷挑战。:)

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

通过将每个字符解释为以36为底的数字,如果大于9(a-zA-Z),则将其乘以1或1.5,如果不大于9,则将其乘以1 。一如既往,欢迎提出建议!


1
在charCodeAt 0是BOT必要
Downgoat

@vihan不知道那件事;谢谢你的提示!
ETHproductions'Aug

为什么不使用toString(36)
l4m2 '18年

@ l4m2我不确定如何.toString(36)在这里应用。你是说类似的东西parseInt(c,36)吗?实际上,那可能会更短...
ETHproductions '18

您可以通过递归并在parseInt返回NaN时使用2/3来节省一些字节: ([c,...t])=>c?(parseInt(c,36)-9||2/3)*(c>'Z'||1.5)+f(t):0
Rick Hitchcock


0

C#81字节

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

致电(LinqPad):

a("Hello World").Dump();

0

PHP,102字节

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;

用法示例:

$ 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

该算法没有什么特别的。从第一程序的参数的每个字符($argv[1])抵靠检查AZ然后az,并相应地计数。


0

PowerShell,108字节

体面的竞争,我有点惊讶。没有紧凑的三元运算符也不太破旧。

码:

$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

解释:

$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

0

C,85个字节

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

!!之前islowerisupper有必要的,因为这些函数返回布尔值不保证01,真正的价值是1024我的系统上的确!


0

糖果26 22字节

(〜“ a” <{A#64-2 / 3 * | A#96-} h)Z

感谢@Tryth的分解技巧!

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

调用带有-I标志,如 candy -I "Cab" -e $prg

长格式的代码是:

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

0

Prolog(SWI),101个字节

码:

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

解释:

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

例:

p(`Cab`).
7.5

0

PHP,75字节

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

与管道一起运行-nr在线尝试

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.