# Code close to the challenge: Sum of integers

This is a simple challenge: Given a sequence of integers, find the sum of all integers in it.

But with a twist. Your score is the Levenshtein distance between your code and the following phrase (The challenge):

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

You may assume there are no newlines or trailing spaces in the input.

Example input/output:

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


An online calculator for Levenshtein distance can be found here: http://planetcalc.com/1721/

• I can't help but think of this – JohnE Jul 23 '15 at 0:58
• Wow, so much creativity guys xD... cough comment abusers cough – user40734 Jul 23 '15 at 1:30
• Do you think this should have a sequel with another challenge but same Levenshtein distance principal? Not sure if this is considered sequel-worthy or not. – user40734 Jul 23 '15 at 1:35
• @NicoA definitely! Maybe the only problem is that this task was too easy. – Maltysen Jul 23 '15 at 1:40
• @NicoA If you do, be very careful in defining what's not allowed. Many languages allow raw strings to float around, for instance. – xnor Jul 23 '15 at 1:48

## Python, distance 3

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


This gives the built-in function sum, which can sum a set like sum({3,5,7})==17. The remaining parts are commented. This has distance 3, with 3 edits:

• Add the initial #
• Add a newline
• Replace the space after sum with #
• Another useful feature of the wonderful English-like syntax – qwr Jul 23 '15 at 1:40
• My only question is how to use it since this isn't a full program, and (in theory) you don't even need in your code because it is a built in – Beta Decay Jul 24 '15 at 5:20
• @BetaDecay This works because of the rules that functions are allowed by default and that function literals are allowed for functions. – xnor Jul 24 '15 at 7:20

# Julia, distance 27 26

No comments!

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


This creates a function called Given that accepts an array and returns the sum of its elements. Since a lot of Julia builtins have relevant names (but are irrelevant to the calculation here), we can just list a few delimited with semicolons. As long as they aren't the last thing listed, they won't be returned. The last part actually makes an array containing the sum and three functions and selects the first element, the sum.

• No comments is definitely more within the spirit of the challenge. Good job. – Christopher Wirt Aug 3 '15 at 16:19
• @ChristopherWirt Thanks! :) – Alex A. Aug 3 '15 at 16:33
• Might I suggest an improvement? Given=(a;set;of=integer; find;th;[sum,of,all,integer, in][]) - "Given" then still works the same, but this has a distance of 18. – Glen O Aug 12 '15 at 8:48

# APL, distance 6 3

Saved 3 distances...? thanks to Dennis!

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


This sums a given array (+/). The remainder of the sentence is added to the end using a comment (⍝).

• You don't need { and ⍵}. +/ is already a valid function. – Dennis Jul 23 '15 at 4:39
• @Dennis Awesome, thanks so much for the suggestion! – Alex A. Jul 23 '15 at 4:44

# GolfScript, 5

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


This a full program that uses no comments (but a lot of noops).

Try it online in Web GolfScript.

# R, Distance 3736 34

Without using comments :)

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


Note there is a space at the beginning.

## Mathematica, distance 17

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

It doesn't use any comments or no-ops, but instead declares all of the words as variables, and then gets rid of them by multiplying by zero.

It also has the benefit of being the only answer that actually takes a set of integers as its input.

The input {1,2,3} provides the output 6 as expected.

Unfortunately, the Mathematica Sum function doesn't do the task in the question, therefore necessitating a larger number of bytes.

• Given a set of integers find the sum of all *0+Total@Input[] distance 14 – Leaky Nun Jul 22 '16 at 19:06

# Java - 43 41

I tried.

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.

• I think you can change your argument in the function from a to r to save one edit distance. – Element118 Dec 26 '15 at 12:24
• @Element118 Nice catch. Also changed the return type while I was at it to have the method name line up better. – Anubian Noob Dec 26 '15 at 18:34
• couldnt you use " int a_" as the beginning of the snippet? (one space before the int and 2 after it before the a_ – masterX244 Nov 15 '16 at 16:52

# RProgN, Distance 2.

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


In RProgN, a, set, find and sum are all commands. Everything else is per default ignored in syntax. a pushes the alphabet to the stack, which will cause sum to fail. Set never has enough arguments, so always fails, erroring. Find either has the wrong number of arguments, or tries to compare the alphabet with the input stack, which doesn't work.

a and set can both be 'fixed' by removing the space between then, aset is not a function, so it's ignored. Find just has an extra character inserted at the start, causing it also to not be recognized, and ignored. Only sum is left, which conveniently sums the contents of the input stack.

Finally, RProgN might win something!

Try it Online!

# CJam, 76 5

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


This is an anonymous function that pops an array from the stack and leaves an integer in return.

Thanks to @AboveFire for shortening the distance by 1.

Try it online.

## Matlab, distance 29 28

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


Without using any comments :-)

The code is in the form of an anonymous function. I'm assuming the input is a vector (1D-array) of numbers.

Example:

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


# O, 5

M]+o"Given a set of integers, find the sum of all integers in it.

Numbers must be in hexadecimal and in reverse negative notation:

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

Try it online

• Yay the link works! – Maltysen Jul 23 '15 at 1:45
• Nice demo! Isn't calling it an IDE kind of overkill, though? – kirbyfan64sos Jul 23 '15 at 1:58
• @kirbyfan64sos Totally, but I want to add more features like syntax highlighting and whatnot. For now, it's just an interpreter. – phase Jul 23 '15 at 2:00
• I think a link to the O esolangs page would be more useful. – mbomb007 Jul 23 '15 at 20:11
• @mbomb007 But that's horribly out of date; half the things on there don't work, and that's only a little bit of the language documented. – phase Jul 23 '15 at 20:31

# K, 60 5

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


I'm guessing symbols do NOT go nicely with the Leve-whatever distance...

Hahaha. Originally, I had no what the LeveXXX distance was, so I got 60. Then, thanks to helpful comments, it dropped to 5.

• Its not that you're using symbols, Levenshtein distance is #of additions, deleteions, substitutions. so you have 60 deletetion from the original string. – Maltysen Jul 23 '15 at 1:33
• Its not symbols, its just that your code is so short that the length of the phrase - the length of your code = 60. Ouch. – user40734 Jul 23 '15 at 1:34
• You know you can just scroll up to get the correct spelling of "Levenshtein," right? ;) – Alex A. Jul 23 '15 at 4:53
• @AlexA. It's funnier this way. :) – kirbyfan64sos Jul 23 '15 at 12:54
• I don't blame him. I had to check the spelling about 10 times before I just copy-pasted it. – user40734 Jul 23 '15 at 14:41

## F#, distance 21

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


Gotta love the ability to use double ticks to give a function a name with spaces in it.

Usage:

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


6

# Pip, distance 3

Joining the club of trivial no-comments-but-lots-of-no-ops golflang answers...

Given a set of integers, find the sum of all integers in $+g  GitHub repository for Pip The code practically documents itself; maybe s/in/using/ for a more accurate description. Integers given as command-line arguments are read into the list g, which is here folded on addition and the result auto-printed. Most everything else is just variables, which are no-ops. I was a bit surprised at first that s, f worked without complaining, since f refers to the main function and taking the range of a code block doesn't make sense. But then I realized: the , range operator, when given a function argument, just constructs another function (as do many operators in Pip). So I think s, f evaluates to {Given a set of integers, find the sum of all integers in " ",$+g}. (Which is then discarded anyway.)

One final point: this code works with the current version of Pip, in which I haven't assigned G to anything yet. If in some future version I make G a binary or ternary operator, then a distance-4 version would be necessary. Using given instead of Given would work nicely.

# Haskell, distance 11

No comments!

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


Usage:

> const sum"Given a set of integers, find the sum of all integers in it." $[1..10] 55  # Cubix, Distance 9 @ivOn a ;et I+ i?tegers, fu;d <he sum of all integers in it.  Try it online! This wraps onto the cube  @ 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 . . . . . . .  The operative code is • I+i Input a integer,add to TOS then input a character • ? Test character value. Redirect left for -1 (end of input) or right for anything else (0 can't be input) • ;O@ pop TOS, output sum and exit • <;u redirect, pop TOS and u-turn onto the start # Pyth - 4 Just puts the actual code, sQ in front of the string no-oped by a space. sQ "Given a set of integers, find the sum of all integers in it.  # PHP4.1, distance 25 This one is a pretty long one and really late in the run. But anyway, here it is: <?=$n_a_set_of_integers_fi=array_sum(\$f_all_integers_in_i);


For this to work, you just need to pass it an array over POST/GET/COOKIE/session, using the key f_all_integers_in_i.

• No comment trolling yay! – Anubian Noob Jul 23 '15 at 19:37

# Pyt, distance 1

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


All alphanumeric characters are no-ops in Pyt, and the sum of a list only takes one character: Ʃ

Try it online!

# C++17, distance 44 29

Variadic Generic Lambda FTW

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


Previous solution

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


# 05AB1E, distance 3

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


Try it online!

# Pyke, Score 3

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


Try it here!

# Ly, score 4

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



Note the trailing newline.

Try it online!

The code is pretty self-explanatory. &+ is Ly's summing operator, while # is a comment. It's unfortunate that I have to include a trailing newline due to the fact that ending a program with a comment line will "comment out" Ly's implicit output, which is actually a bug in the interpreter that I'm calling a feature.

# dc, 14

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


comments:

?                   # 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


TIO

it complains about the stack being empty if you enter 1 number, but still works, and removal of 0 saves 2 diff.

# Excel VBA, Distance: 11

Anonymous VBE immediate window function that takes input from the range [a:a] on the ActiveSheet object and outputs to the VBE immediate window

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


# Brain-Flak, 20

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


Try it online!

# Explanation

Since there are no parentheses in the original text this boils down to a problem in Brain-Flak. But that still doesn't make this answer trivial, as answers in Brain-Flak rarely are.

One's first intuition would probably be the following code

({{}})


Which works ... unless there is a zero on the stack in which case it just sums until the zero. To get around this problem we have to use a stack height to check that the stack is not empty. This can be set up like this

([])    #{ 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 }


This one works, but there is something wrong with it. We keep silencing the pops and the pushes in the loops but they are almost equal so there should be a way to cancel them out. If we try

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


We end up off by n each time. So here's the trick, we already have an n siting around, we just move it into the push to balance things out.

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

• Just wanted to congratulate you on your 2^8th ppcg answer – Taylor Scott Sep 17 '17 at 20:40

# Ahead, 17

>jLIve>   K+O @f integers, find the sum of all integers in it.
^ >j<l


Try it online!

# Jelly, Distance: 2

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


Try it online!

Jelly only evaluates the main link (the last line) and explicit commands will run other links (other lines).

The last line has S, which sums the input.

The first line does not get executed because there is no reference to it in the main link.

# Snap! 4.2.2.9 (+ Tools), scratchblocks3 syntax, distance 35

This is a function. integers, find the sum of all inte is the input.

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

• How does this take input? Is input supposed to be pasted into the code? Usually we do not allow that sort of input but rather require that input is either taken from STDIN or if your submission is a function, that it is passed as an argument. – Post Rock Garf Hunter Mar 16 '19 at 14:44
• No, it is a function. integers, find the sum of all inte is an argument, it just isn't distinguished from regular variables in scratchblocks3 syntax, which is why I clarified – Silas Reel Mar 16 '19 at 16:01