输入值
一个字符串。
输出量
该行中所有整数的总和。
约束条件
1≤线长≤500
样本测试案例
输入值
the 5is 108 seCONd4 a
输出量
117
说明
总和为:5 + 108 + 4 = 117
string x='-12hello3';,您要计算负整数(即-12 + 3 === -9)吗?
一个字符串。
该行中所有整数的总和。
1≤线长≤500
输入值
the 5is 108 seCONd4 a
输出量
117
说明
总和为:5 + 108 + 4 = 117
string x='-12hello3';,您要计算负整数(即-12 + 3 === -9)吗?
Answers:
s=>eval(s.match(/\d+/g).join`+`)
匹配所有数字,+并将其变成5 + 108 + 4,将其加入,评估结果。
仅适用于正整数。
感谢Arnauld,节省了2个字节
f=
s=>eval(s.match(/\d+/g).join`+`)
g=()=>b.innerHTML = f(a.value)
g()
<input id=a value="the 5is 108 seCONd4 a" onkeyup="g()">
<pre id=b>
console.log(f.toString().length),但也不是 100%可靠。
þмS¡þO
说明:
þм # Only leave the non-digits of the (implicit) input-string
# i.e. "the 5is 108 seCONd4 a" → "the is seCONd a"
S # Split it into a list of characters
# → ["t","h","e"," ","i","s"," "," ","s","e","C","O","N","d"," ","a"]
¡ # Split the (implicit) input-string by each of these characters
# → ["","","","","5","","","108","","","","","","","4","",""]
þ # Remove the empty strings by only leaving the digits
# → ["5","108","4"]
O # And sum these numbers (which is output implicitly)
# → 117
¡!
匿名默认功能
+/#⍎¨∊∘⎕D⊆⊢
⊢ 论点
⊆ 分区(True的运行变为片段,False的运行为分隔符)
∊ 会员
∘ 的
⎕D 的一组数字
#⍎¨ 在根名称空间中评估每个
+/ 和
for n in ${1//[!0-9]/ };{((s+=n));};echo $s
用空格替换每个非数字,然后将它们加在一起。
-5字节归功于GammaFunction
为什么不。强制性正则表达式答案。可以使用Python 2停靠6。由于我使用的是评估方法而不是地图,因此不再适用。
import re;x=lambda y:eval('+'.join(re.findall('\d+',y)))
说明:
import re; # Import regex module
x=lambda y: eval( ) # Run as Python code
'+'.join( ) # Joined by '+'
re.findall('\d+',y) # A list of all matches of regex \d+ in string y
z == l[1]而不是z is l[1]。如果数字足够高,当前代码可能会给出假阴性。
?|i.I!/s+q;;>p.O@
? |
i .
I ! / s + q ; ;
> p . O @ . . .
. .
. .
一个相当简单的。 I在cubix中,它将输入中的第一个整数并将其推入堆栈。这具有跳过所有字符的作用。其余部分将处理附加内容并检测输入的结束。
I! 输入一个整数并将其测试为0s+q;;如果不为零,则交换TOS(力和初始0)并添加。将结果推入堆栈底部并清除顶部。返回开始。/i? 如果为零,请重定向并输入字符以进行检查|?;/如果正数(字符)右移为反射,则将其推回检查器?并从堆栈中右移到弹出式菜单上,TOS上保留0。然后将IP重定向回主循环。I>p.O@ 如果负数(输入的末尾)向左转,则进行整数输入,将堆栈的底部移至顶部,然后输出并停止。Sum##N=>MatchAll&"\\d+"
一个更有趣但间接的答案(37个字节): {Sum!Reap[ReplaceF[_,/"\\d+",Sow@N]]}
Sum##N=>MatchAll&"\\d+"
其形式为:
f##g=>h&x
展开并加上括号后,将变为:
f ## (g => (h&x))
##将两个函数组合在一起,=>创建一个将左函数映射到右函数的结果的函数,并将&参数绑定到函数的一侧。对于input _,这等效于:
{ f[Map[g, h[_, x]]] }
首先,然后MatchAll运行数字字符(\\d+)。之后,我们使用N函数将每次运行转换为实际整数。最后,我们使用取这些数字的总和Sum。
105个字节+ 25个字节用于正则表达式导入
s->{long c=0;for(Matcher m=Pattern.compile("\\d+").matcher(s);m.find();c+=new Long(m.group()));return c;}
在线尝试!
说明
s->{ // Lambda function
long c=0; // Sum is zero
for(Matcher m=Pattern.compile("\\d+").matcher(s); // Prepare regex matcher
m.find(); // While the string contains unused matches...
c+=new Long(m.group())); // Add those matches to the output
return c; // Return the output
}
S =INPUT
D S SPAN('0123456789') . N REM . S :F(O)
O =O + N :(D)
O OUTPUT =O
END
s->java.util.Arrays.stream(s.split("\\D")).filter(t->!t.isEmpty()).mapToLong(Long::new).sum()
-4 bytes通过使用Long::new代替Long::valueOf。
-1 byte通过缩短正则表达式-如果以后已经删除空字符串,则在拆分时可以做一些额外的工作。
s-> // Lambda (target type is ToLongFunction<String>)
java.util.Arrays.stream( // Stream the result of
s.split("\\D") // splitting on non-digits
)
.filter(t->!t.isEmpty()) // Discard any empty strings
.mapToLong(Long::new) // Convert to long
.sum() // Add up the stream's values.
"the 5is 108 seCONd4 a",117由于导致5+108+4=117)。此外,此处的每个“问题”都应具有获胜条件标签。在这种情况下,我认为它是[code-golf](是最短的解决方案)?