接近挑战的代码:整数和


39

这是一个简单的挑战:给定整数序列,找到其中的所有整数之和。

但是有一个转折。您的分数是代码与以下短语(挑战)之间的Levenshtein距离:

给定一组整数,找到其中所有整数的总和。

您可以假设输入中没有换行符或尾随空格。

输入/输出示例:

Input: 1 5 -6 2 4 5
Output: 11
Input: 1 -2 10
Output: 9

可在此处找到Levenshtein距离的在线计算器:http : //planetcalc.com/1721/


4
我忍不住想起这件事
— JohnE

7
哇,这么多创造力的人xD ... 咳嗽评论滥用者咳嗽

1
您是否认为这应该有另一个挑战的续集,但Levenshtein距离原理相同?不知道这是否值得续集。

1
@NicoA绝对!也许唯一的问题是此任务太容易了。
— Maltysen

2
@NicoA如果这样做,请务必谨慎定义不允许的内容。例如,许多语言都允许原始字符串浮动。
— xnor

Answers:


58

Python,距离3

#Given a set of integers, find the 
sum#of all integers in it.

这提供了内置函数sum,该函数可以对进行求和sum({3,5,7})==17。其余部分被评论。距离为3,具有3个修改:

  • 添加首字母 #
  • 添加换行符
  • 之后sum用#

3
出色的类似于英语的语法的另一个有用功能
— qwr

1
我唯一的问题是如何使用它,因为它不是一个完整的程序,并且(从理论上来说)您甚至不需要在代码中,因为它是内置的
— Beta Decay 2015年

1
@BetaDecay之所以起作用,是因为存在以下规则:默认情况下允许使用函数,并且函数应使用函数文字。
— xnor

54

朱莉娅,距离27 26

没意见!

Given(a)=(Set;of;integer; find; [sum(a),all,integer,in][1])

这将创建一个名为的函数Given,该函数接受一个数组并返回其元素的总和。由于许多Julia内建函数都有相关的名称(但与此处的计算无关),我们只列出一些用分号分隔的名称。只要它们不是列出的最后一项,它们就不会退还。最后一部分实际上是一个包含求和和三个函数的数组,然后选择第一个元素和。


毫无疑问,更多的评论绝对是挑战精神。做得好。
— Christopher Wirt

@ChristopherWirt谢谢!:)
— Alex A.

4
我可以建议改善吗?Given=(a;set;of=integer; find;th;[sum,of,all,integer, in][])- “考虑”,那么仍然有效相同,但这18的距离
— 格伦Ø

22

APL,距离6 3

保存了3个距离...?感谢丹尼斯!

+/⍝en a set of integers, find the sum of all integers in it.

这将给定数组(+/)相加。使用注释(⍝)将句子的其余部分添加到末尾。


1
您不需要{和⍵}。+/已经是有效的函数。
— 丹尼斯

@Dennis很棒,非常感谢您的建议!
— Alex A.

10

GolfScript,5岁

~{Given a set of integers+ find the sum of all integers in it}*

这是一个完整的程序,不使用任何注释(但要注意很多)。

在Web GolfScript中在线尝试。


9

R,距离37 36 34

不使用评论:)

 as.integer ( sum (scan(,integer( ) )))

请注意,开头有一个空格。


8

Mathematica,距离17

Given a set of integers find the sum of all integers in it*0+Total@Input[]

它不使用任何注释或无操作,而是将所有单词声明为变量,然后通过乘以零来消除它们。

它还有一个好处,它是唯一一个实际上将一组整数作为输入的答案。

输入{1,2,3}提供6预期的输出。

不幸的是,Mathematica Sum函数不能执行问题中的任务,因此需要更多的字节数。


Given a set of integers find the sum of all *0+Total@Input[]距离14
— Leaky Nun

6

爪哇-43 41

我试过了。

float a_set_of(int[] r){return IntStream.of(r).sum()}//n it.
Given a set of integers, find the sum of all integers in it.

Java:P。


我认为您可以将函数中的参数从a更改为r以节省一个编辑距离。
— Element118

@ Element118好抓住。在我使用它的同时,还更改了返回类型,以使方法名称更好地排列起来。
— Anubian Noob 2015年

2
您不能使用“ int a_”作为代码段的开头吗?(在int之前一个空格,在a_之前第二个空格
— masterX244 '16

6

RProgN,距离2。

Given aset of integers, ;find the sum of all integers in it.

在RProgN中,a,set,find和sum都是命令。默认情况下,语法中会忽略其他所有内容。a将字母推入堆栈,这将导致求和失败。Set永远不会有足够的参数,因此总是会失败并出错。查找或者参数数量错误,或者尝试将字母与输入堆栈进行比较,这是行不通的。

a和set都可以通过删除两者之间的空格来“固定”,因为aset不是函数,因此将被忽略。查找只是在开始处插入了一个额外的字符,导致它也无法被识别和忽略。只剩下sum,可以方便地对输入堆栈的内容求和。

最后,RProgN可能会赢得一些大奖!

在线尝试!


4

果酱,7 6 5

{:+}e# a set of integers, find the sum of all integers in it.

这是一个匿名函数,该函数从堆栈中弹出一个数组,并返回一个整数。

感谢@AboveFire将距离缩短了1。

在线尝试。


4

Matlab,距离29 28

Given_a_set_of_integers=@(findthe)sum(all(1)*findthe)

不使用任何评论:-)

该代码采用匿名函数的形式。我假设输入是数字的向量(一维数组)。

例:

>> Given_a_set_of_integers=@(findthe)sum(all(1)*findthe)
Given_a_set_of_integers = 
    @(findthe)sum(all(1)*findthe)

>> Given_a_set_of_integers([1 5 -6 2 4 5])
ans =
    11

3

Ø,5

M] + o“给出一组整数,找到其中的所有整数之和。

数字必须为十六进制且为负负数:

  • -6 => 6_
  • -4 => 4_
  • -10 => A_

在线尝试


1
是的,链接有效!
— Maltysen

不错的演示!但是,这不是说它有点过分吗?
— kirbyfan64sos 2015年

完全是@ kirbyfan64sos,但我想添加更多功能,例如语法突出显示和诸如此类。现在,它只是一个解释器。
— 阶段

我认为指向O esolangs页面的链接会更有用。
— mbomb007

@ mbomb007但这太过时了;那里的一半内容不起作用,而这只是所记录语言的一小部分。
— 阶段

3

K,60 5

+/ / Given a set of integers, find the sum of all integers in it.

我猜想符号与Leve的距离不太合适...

哈哈哈 最初,我没有 LeveXXX距离,所以我得到了60。然后,由于有帮助的注释,该距离下降到了5。


不是您正在使用符号,Levenshtein距离就是添加,删除和替换的#个。因此您要从原始字符串中删除60条。
— Maltysen

它不是符号,只是您的代码太短以至于短语的长度-您的代码的长度= 60。

2
您知道您可以向上滚动以获得“ Levenshtein”的正确拼写,对吗?;)
— Alex A.

@AlexA。这样比较有趣。:)
— kirbyfan64sos 2015年

2
我不怪他 在复制粘贴之前,我不得不检查了大约10次拼写。

3

F#,距离21

let ``Given a set of integers, find the sum of all integers in it`` x = Seq.sum x

一定喜欢使用双勾号为函数命名的功能,其中带有空格。

用法:

[1;2;3] |> ``Given a set of integers, find the sum of all integers in it`` |> printfn "%i"

6


3

点距离3

加入琐碎的无注释俱乐部,但是很多无操作性golflang答案...

Given a set of integers, find the sum of all integers in $+g

Pip的GitHub存储库

该代码实际上记录了自己。也许s/in/using/更准确的描述。作为命令行参数给出的整数将被读入列表中g,列表在加法时会被折叠并自动打印结果。大多数其他所有内容只是变量,没有操作。

一开始我很惊讶,而且s, f没有抱怨,因为f提到了主要功能,而采用代码块的范围是没有道理的。但是后来我意识到:,范围运算符在给定函数参数时,只会构造另一个函数(就像Pip中的许多运算符一样)。所以我认为s, f评估为{Given a set of integers, find the sum of all integers in " ",$+g}。(然后将其丢弃。)

最后一点:这段代码适用于当前版本的Pip,在该版本中我尚未分配G任何内容。如果在将来的版本中G使用二进制或三元运算符,则距离4版本是必要的。使用given而不是Given将很好地工作。


3

Haskell,距离11

没意见!

const sum"Given a set of integers, find the sum of all integers in it."

用法:

> const sum"Given a set of integers, find the sum of all integers in it." $ [1..10]
55

3

库比克斯,距离9

@ivOn a ;et I+ i?tegers, fu;d <he sum of all integers in it.

在线尝试!

包裹在立方体上

      @ i v
      O n a
      ; e t
I + i ? t e g e r s , f
u ; d < h e s u m o f a
l l i n t e g e r s i n
      i t .
      . . .
      . . .

执行代码是

  • I+i 输入一个整数,添加到TOS然后输入一个字符
  • ?测试字符值。向左重定向-1(输入结束)或向右重定向其他任何内容(无法输入0)
    • ;O@ 弹出TOS,输出总和并退出
    • <;u 重定向,弹出TOS并掉头进入起点


1

PHP4.1,距离25

这是一个相当长的版本,实际上是后期。

但是无论如何,这里是:

<?=$n_a_set_of_integers_fi=array_sum($f_all_integers_in_i);

为此,您只需使用密钥通过POST / GET / COOKIE / session将其传递给数组f_all_integers_in_i。


2
没有评论拖曳呀!
— Anubian Noob 2015年

1

PYT,距离1

Given a set of integers, find the Ʃum of all integers in it.

在Pyt中,所有字母数字字符均为无操作符,列表的总和仅包含一个字符:Ʃ

在线尝试!


0

C ++ 17,距离44 29

可变参数通用Lambda FTW

[](auto...t){return(t+...);}//the sum of all integers in it.

先前的解决方案

template<class...t>int s(t...l){return(...+l);}//gers in it.



0

丽,得分4

&+#Given a set of integers, find the sum of all integers in it.

注意尾随换行符。

在线尝试!

该代码是不言自明的。&+是Ly的求和运算符,#而是评论。不幸的是,由于必须用注释行结束程序,才能“注释掉” Ly的隐式输出,这实际上是我正在调用功能的解释器中的一个错误,因此我必须在结尾加上换行符。


0

直流电,14

?[+z1 <f]d  sf xp#egers, find the sum of all integers in it.

评论:

?                   # read numbers
 [                  # start macro
  +                 # add up last two things on stack
   z1 <f            # if stack size is more than 1, execute macro at register 'f'
        ]           # end macro
         d          # dupe it
            sf      # saving one copy to register 'f'
               x    # and executing another
                p   # printing result

蒂奥

如果输入1,它会抱怨堆栈为空,但仍然有效,删除0将节省2个差异。


0

Excel VBA,距离:11

匿名VBE立即窗口函数,该函数从[a:a]ActiveSheet对象上的范围获取输入并输出到VBE立即窗口

?[Sum(a:a)] 'f integers, find the sum of all integers in it.

0

脑筋急转弯,20

(([]){[{}]{}([])}{})ers, find the sum of all integers in it.

在线尝试!

说明

由于原始文本中没有括号,因此可以归结为Brain-Flak中的问题。但这仍然不能使这个答案变得微不足道,因为Brain-Flak中的答案很少。

一个人的直觉可能是以下代码

({{}})

哪个有效... 除非堆栈上没有零,在这种情况下,它一直累加到零。要解决此问题,我们必须使用堆栈高度检查堆栈是否为空。可以这样设置

([])    #{ Push stack height }
(       #{ Start Push }
{       #{ Loop until zero }
 <{}>   #{ Silently Pop the last height }
 {}     #{ Grab a value from the stack }
 <([])> #{ Silently push the stack height again }
}       #{ End loop }
{}      #{ Remove last stack height }
)       #{ Push the result }

这是可行的,但是有问题。我们一直使循环中的爆音和推动静音,但它们几乎相等,因此应该有一种消除它们的方法。如果我们尝试

([])({[{}]{}([])}{})

我们n每次都结束。因此,这就是窍门,我们已经有了一个n选址,我们只是将其移至推动平衡的过程中。

(([]){[{}]{}([])}{})

1
只是想恭喜您,您的2^8回答是多少?
— Taylor Scott


0

果冻,距离:2

Given a set of integers, find the sum of all integers in it.
S

在线尝试!

Jelly仅评估主链接(最后一行),显式命令将运行其他链接(其他行)。

最后一行有S,它对输入求和。

第一行不会执行,因为在主链接中没有对它的引用。


0

快点!4.2.2.9(+工具),scratchblocks3语法,距离35

这是一个功能。integers, find the sum of all inte是输入。

for each(et)of(integers, find the sum of all inte
change[s v]by(et
end
report(s

这如何接受输入?输入是否应该粘贴到代码中?通常,我们不允许这种输入,而是要求输入是从STDIN中获取的,或者如果您的提交是函数,则将其作为参数传递。
— 小麦巫师

不,这是一个功能。integers, find the sum of all inte是一个参数,只是没有与scratchblocks3语法中的常规变量区分开,这就是我澄清的原因
— Silas Reel
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.