# 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 Commented Jul 23, 2015 at 0:58
• Wow, so much creativity guys xD... cough comment abusers cough
– user40734
Commented Jul 23, 2015 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
Commented Jul 23, 2015 at 1:35
• @NicoA definitely! Maybe the only problem is that this task was too easy. Commented Jul 23, 2015 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
Commented Jul 23, 2015 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 #
• Replace the space after sum with #
• Another useful feature of the wonderful English-like syntax
– qwr
Commented Jul 23, 2015 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 Commented Jul 24, 2015 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
Commented Jul 24, 2015 at 7:20

# Julia, distance 27 26

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. Commented Aug 3, 2015 at 16:19
• @ChristopherWirt Thanks! :) Commented Aug 3, 2015 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. Commented Aug 12, 2015 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. Commented Jul 23, 2015 at 4:39
• @Dennis Awesome, thanks so much for the suggestion! Commented Jul 23, 2015 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

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


Note there is a space at the beginning.

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

## 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 Commented Jul 22, 2016 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. Commented Dec 26, 2015 at 12:24
• @Element118 Nice catch. Also changed the return type while I was at it to have the method name line up better. Commented Dec 26, 2015 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_ Commented Nov 15, 2016 at 16:52

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


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


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

• Wait, do people actually do this?
– Wezl
Commented Jan 5, 2021 at 23:36

# 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

# Vyxal 3, distance 1

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


Try it Online!

Prints a singleton number.

## Explanation

Given a set of integersṠ, find the sum of all integers in it.­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁢⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁡⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁢⁣⁪‏‏​⁡⁠⁡‌⁢⁢​‎‎⁪⁡⁪⁠⁪⁣⁡⁪‏‏​⁡⁠⁡‌⁢⁣​‎‎⁪⁡⁪⁠⁪⁣⁢⁪‏‏​⁡⁠⁡‌⁢⁤​‎‎⁪⁡⁪⁠⁪⁣⁣⁪‏‏​⁡⁠⁡‌⁣⁡​‎‎⁪⁡⁪⁠⁪⁤⁡⁪‏‏​⁡⁠⁡‌⁣⁢​‎‎⁪⁡⁪⁠⁪⁤⁢⁪‏‏​⁡⁠⁡‌⁣⁣​‎‎⁪⁡⁪⁠⁪⁤⁤⁪‏‏​⁡⁠⁡‌⁣⁤​‎‎⁪⁡⁪⁠⁪⁢⁡⁡⁪‏‏​⁡⁠⁡‌⁤⁡​‎‎⁪⁡⁪⁠⁪⁢⁡⁢⁪‏‏​⁡⁠⁡‌⁤⁢​‎‎⁪⁡⁪⁠⁪⁢⁡⁣⁪‏‏​⁡⁠⁡‌⁤⁣​‎‎⁪⁡⁪⁠⁪⁢⁡⁤⁪‏‏​⁡⁠⁡‌⁤⁤​‎‎⁪⁡⁪⁠⁪⁢⁢⁡⁪‏‏​⁡⁠⁡‌⁢⁡⁡​‎⁪⁪⁠‎⁪⁡⁪⁠⁪⁢⁢⁢⁪‏‏​⁡⁠⁡‌⁢⁡⁢​‎‎⁪⁡⁪⁠⁪⁢⁢⁣⁪‏‏​⁡⁠⁡‌⁢⁡⁣​‎‎⁪⁡⁪⁠⁪⁢⁢⁤⁪‏‏​⁡⁠⁡‌⁢⁡⁤​‎‎⁪⁡⁪⁠⁪⁢⁣⁡⁪‏‏​⁡⁠⁡‌⁢⁢⁡​‎‎⁪⁡⁪⁠⁪⁢⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁤⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁤⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁤⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁤⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁡⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁡⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁡⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁡⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁣⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁣⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁣⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁤⁡⁪‏‏​⁡⁠⁡‌­
G                                                              ## ‎⁡Get the max element
i                                                             ## ‎⁢index
ve                                                           ## ‎⁣Is odd? (decrement, Is even?)
n                                                          ## ‎⁤Push "abcdefghijklmnopqrstuvwxyz" anyway
a                                                        ## ‎⁢⁡Is the alphabet truthy? (Always)
s                                                      ## ‎⁢⁢Split! I don't know how can you split a bool with 1...
e                                                     ## ‎⁢⁣Is this even?
t                                                    ## ‎⁢⁤Get the last element (How can you do it with a bool?)
o                                                  ## ‎⁣⁡Overlap (How??)
i                                               ## ‎⁣⁣Index with input again
n                                              ## ‎⁣⁤Also, push "abcdefghijklmnopqrstuvwxyz" anyway
t                                             ## ‎⁤⁡Get the last element ("z")
e                                            ## ‎⁤⁢Split on newlines ("z" has no newlines, so pop and push ["z"])
g                                           ## ‎⁤⁣Never gonna <g>ive you up. (I don't understand what this does anyway)
e                                          ## ‎⁤⁤Split on newlines (Again? This time, it doesn't wrap)
r                                         ## ‎⁢⁡⁡replace this with input?
s                                        ## ‎⁢⁡⁢split with itself?
Ṡ                                       ## ‎⁢⁡⁣SUM!
,                                      ## ‎⁢⁡⁤PRINT!
find the sum of all integers in it.  ## ‎⁢⁢⁡I give up. This doesn't do anything.
💎


Created with the help of Luminespire.

# 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! Commented Jul 23, 2015 at 1:45
• Nice demo! Isn't calling it an IDE kind of overkill, though? Commented Jul 23, 2015 at 1:58
• @kirbyfan64sos Totally, but I want to add more features like syntax highlighting and whatnot. For now, it's just an interpreter.
Commented Jul 23, 2015 at 2:00
• I think a link to the O esolangs page would be more useful. Commented Jul 23, 2015 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.
Commented Jul 23, 2015 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. Commented Jul 23, 2015 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
Commented Jul 23, 2015 at 1:34
• You know you can just scroll up to get the correct spelling of "Levenshtein," right? ;) Commented Jul 23, 2015 at 4:53
• @AlexA. It's funnier this way. :) Commented Jul 23, 2015 at 12:54
• I don't blame him. I had to check the spelling about 10 times before I just copy-pasted it.
– user40734
Commented Jul 23, 2015 at 14:41

# Pip, distance 3

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.

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  # 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! Commented Jul 23, 2015 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

[](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.


?                   # 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 Commented Sep 17, 2017 at 20:40

>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. Commented Mar 16, 2019 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 Commented Mar 16, 2019 at 16:01