39
\$\begingroup\$

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/

\$\endgroup\$
  • 4
    \$\begingroup\$ I can't help but think of this \$\endgroup\$ – JohnE Jul 23 '15 at 0:58
  • 7
    \$\begingroup\$ Wow, so much creativity guys xD... cough comment abusers cough \$\endgroup\$ – user40734 Jul 23 '15 at 1:30
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – user40734 Jul 23 '15 at 1:35
  • 1
    \$\begingroup\$ @NicoA definitely! Maybe the only problem is that this task was too easy. \$\endgroup\$ – Maltysen Jul 23 '15 at 1:40
  • 2
    \$\begingroup\$ @NicoA If you do, be very careful in defining what's not allowed. Many languages allow raw strings to float around, for instance. \$\endgroup\$ – xnor Jul 23 '15 at 1:48

29 Answers 29

58
\$\begingroup\$

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 #
\$\endgroup\$
  • 3
    \$\begingroup\$ Another useful feature of the wonderful English-like syntax \$\endgroup\$ – qwr Jul 23 '15 at 1:40
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – Beta Decay Jul 24 '15 at 5:20
  • 1
    \$\begingroup\$ @BetaDecay This works because of the rules that functions are allowed by default and that function literals are allowed for functions. \$\endgroup\$ – xnor Jul 24 '15 at 7:20
54
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ No comments is definitely more within the spirit of the challenge. Good job. \$\endgroup\$ – Christopher Wirt Aug 3 '15 at 16:19
  • \$\begingroup\$ @ChristopherWirt Thanks! :) \$\endgroup\$ – Alex A. Aug 3 '15 at 16:33
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – Glen O Aug 12 '15 at 8:48
22
\$\begingroup\$

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

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

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.

\$\endgroup\$
9
\$\begingroup\$

R, Distance 37 36 34

Without using comments :)

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

Note there is a space at the beginning.

\$\endgroup\$
8
\$\begingroup\$

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.

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

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.

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

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!

\$\endgroup\$
4
\$\begingroup\$

CJam, 7 6 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.

\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ Yay the link works! \$\endgroup\$ – Maltysen Jul 23 '15 at 1:45
  • \$\begingroup\$ Nice demo! Isn't calling it an IDE kind of overkill, though? \$\endgroup\$ – kirbyfan64sos Jul 23 '15 at 1:58
  • \$\begingroup\$ @kirbyfan64sos Totally, but I want to add more features like syntax highlighting and whatnot. For now, it's just an interpreter. \$\endgroup\$ – phase Jul 23 '15 at 2:00
  • \$\begingroup\$ I think a link to the O esolangs page would be more useful. \$\endgroup\$ – mbomb007 Jul 23 '15 at 20:11
  • \$\begingroup\$ @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. \$\endgroup\$ – phase Jul 23 '15 at 20:31
3
\$\begingroup\$

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.

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

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

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

Try it online here.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ No comment trolling yay! \$\endgroup\$ – Anubian Noob Jul 23 '15 at 19:37
1
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

05AB1E, distance 3

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyke, Score 3

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

Try it here!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

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.

(([]){[{}]{}([])}{})
\$\endgroup\$
  • 1
    \$\begingroup\$ Just wanted to congratulate you on your 2^8th ppcg answer \$\endgroup\$ – Taylor Scott Sep 17 '17 at 20:40
0
\$\begingroup\$

Ahead, 17

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Sriotchilism O'Zaic Mar 16 at 14:44
  • \$\begingroup\$ 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 \$\endgroup\$ – Silas Reel Mar 16 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy