-6
\$\begingroup\$

This is a little codegolf challenge from codingame.com. I was wondering how much Python3 code is necessary (but of course answers are not limited to Python3).

Input
The first line contains one positive integer n. The second line contains n positive integers. Each consecutive integers on the second line are separated by one space. There is no newline at the end of the second line.

Output
One line containing the sum of the squares of the numbers on the second input line, and no white space except an optional newline at the end.

Each entry must be a complete program with input and output, not only a function. Shortest code wins.

For example, input:

5
1 2 5 7 8

and output:

143
\$\endgroup\$
12
  • 6
    \$\begingroup\$ Can we take only one input? The first one is unnecessary \$\endgroup\$
    – Luis Mendo
    Mar 29, 2016 at 16:10
  • 6
    \$\begingroup\$ You do know you're changing IO allowed by default? meta.codegolf.stackexchange.com/questions/2447/… \$\endgroup\$
    – Blue
    Mar 29, 2016 at 16:11
  • 1
    \$\begingroup\$ Can we take input as a list of integers? (i.e [1,2,5,7,8]) \$\endgroup\$ Mar 29, 2016 at 16:19
  • 6
    \$\begingroup\$ I'm confused why you've posted both here and in the Sandbox ... meta.codegolf.stackexchange.com/a/8827/42963 \$\endgroup\$ Mar 29, 2016 at 16:20
  • 8
    \$\begingroup\$ We've discussed the situation among the moderators and figured that the copyright is indeed probably not worth worrying about. However, the downvotes on your challenge are likely due to the fact that you've copied the input format verbatim from the source, and cumbersome I/O formats are one of the things to avoid when writing challenges. If you relaxed the rules a little, this challenge would probably blend in more nicely and be more well-received. Sorry about the premature deletion, and welcome to Programming Puzzles & Code Golf! \$\endgroup\$
    – Dennis
    Mar 29, 2016 at 20:37

23 Answers 23

2
\$\begingroup\$

05AB1E, 5 bytes

Code:

²ð¡nO

Explanation:

²      # Take the second input, the first input is ignored
 ð¡    # Split on spaces
   n   # Square each element
    O  # Take the sum and implicitly output it

Uses CP-1252 encoding.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 52

i=input;print(sum(int(d)**2for d in i(i()).split()))
\$\endgroup\$
2
  • \$\begingroup\$ It is giving an unnecessary output. But that sound fair since there is an unnecessary input! \$\endgroup\$
    – aliqandil
    Mar 29, 2016 at 17:08
  • \$\begingroup\$ Yup, going by the rules... \$\endgroup\$
    – shooqie
    Mar 29, 2016 at 17:14
2
\$\begingroup\$

Jelly, 5 bytes

ṣ⁶V²S

Try it online.

Input format is rather annoying....

Explanation

ṣ⁶V²S
ṣ⁶       split by space
  V      map eval over the resulting list -- the newline in the first element causes the first line to be treated as a separate link and ignored
   ²     map square over the list
    S    sum
\$\endgroup\$
3
  • \$\begingroup\$ You can save three bytes by eliminating the first three bytes. \$\endgroup\$
    – Dennis
    Mar 29, 2016 at 20:20
  • \$\begingroup\$ @Dennis Oh, that's clever. Thanks. \$\endgroup\$ Mar 29, 2016 at 21:17
  • \$\begingroup\$ In case it isn't clear, V executes the string just as if it were a complete program. For the first string, the second line is the main link, and the first one isn't referenced, so it is ignored. \$\endgroup\$
    – Dennis
    Mar 29, 2016 at 23:04
1
\$\begingroup\$

Minkolang 0.15, 9 bytes

n[n2;+]N.

Try it here!

Explanation

n            Take number from input
 [    ]      Pop top of stack and repeat that many times
  n          Take number from input
   2;        Square it
     +       Add to top of stack
       N.    Output as number and stop.
\$\endgroup\$
1
\$\begingroup\$

CJam, 10 bytes

l;l~]2f#1b

Test it here.

Explanation

l;   e# Read first line and discard.
l~   e# Read second line and evaluate, dumping all integers on the stack.
]    e# Wrap them in an array.
2f#  e# Square each.
1b   e# Sum.
\$\endgroup\$
2
  • \$\begingroup\$ 1b is a nice trick \$\endgroup\$
    – Luis Mendo
    Mar 29, 2016 at 16:30
  • 1
    \$\begingroup\$ @DonMuesli I could probably use :+ here, but 1b became a habit because it also works as expected for empty arrays. \$\endgroup\$ Mar 29, 2016 at 16:31
1
\$\begingroup\$

Python 3, 54 bytes

-[] thanks to shooqie

print(sum(int(i)**2for i in input(input()).split()))

Takes input as specified in the first version of the question.

So to answer your question: Not a lot.

\$\endgroup\$
4
  • \$\begingroup\$ first version :-D \$\endgroup\$
    – Luis Mendo
    Mar 29, 2016 at 16:16
  • \$\begingroup\$ Stupid me, testing in a REPL... \$\endgroup\$ Mar 29, 2016 at 16:28
  • \$\begingroup\$ You don't need the square brackets: print(sum(int(i)**2for i in input(input()).split())) \$\endgroup\$
    – shooqie
    Mar 29, 2016 at 16:32
  • \$\begingroup\$ @CatsAreFluffy and shooqie - Thank you, I was doing it in 56 bytes and was wondering what parts of the code are unnecessary. As opposed to my code, input(input()) saves one byte, 2for another and the square brackets another two. \$\endgroup\$
    – mIllIbyte
    Mar 29, 2016 at 16:43
1
\$\begingroup\$

MATL, 6 bytes

xjU2^s

Try it online!

x    % take first input and throw it away
j    % take second input as a string
U    % convert to array with those numbers
2^   % element-wise square
s    % sum of array
\$\endgroup\$
1
\$\begingroup\$

R, 22 bytes

Nothing very flash, handles the input as specified

cat(sum(scan()[-1]^2))

Test run

> cat(sum(scan()[-1]^2))
1: 5
2: 1 2 5 7 8
7: 
Read 6 items
143
> 
\$\endgroup\$
1
\$\begingroup\$

Pyke, 10 bytes

;\ cFE2^)s

Takes input as requested

Try it here (Yay setup a try-it page!)

Or 5 bytes if allowed a list of ints:

2Rm^s

Or noncompeting (add squared node), 3 bytes

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

Lua, 74 62 Bytes

Edit: saved 8 bytes due to @KennyLau

It's quite straigthforward, skip the first line and iterate over the group of digits in the second.

i,x=io.read,0i()i():gsub("%d+",function(d)x=x+d*d end)print(x)

Ungolfed

x=0
i=io.read 
i()
i():gsub("%d+",function(d)
   x=x+d*d
end)
print(x)
\$\endgroup\$
2
  • \$\begingroup\$ Actually, lua can convert from string to number automatically, so you don't have to do it yourself :) \$\endgroup\$
    – Leaky Nun
    Mar 30, 2016 at 10:20
  • \$\begingroup\$ Also, this actually takes up less bytes than this: x=0i=1print(x..i) x,i=0,1print(x..i) \$\endgroup\$
    – Leaky Nun
    Mar 30, 2016 at 10:25
1
\$\begingroup\$

Octave, 45 bytes

Octave is not exactly designed for accepting random input formats. For instance, input over multiple lines is impossible. Also, numbers can't be entered as a list unless you have brackets. For that reason, the input must be taken as a string.

input('');disp((x=str2num(input('','s')))*x')

input('')                                     % Take the first input line and discard
                         input('','s')        % Take the second input as a string 
               x=str2num(input('','s'))       % Convert the string to a list of numbers, x
               (x=str2num(input('','s')))*x'  % Multiply x by x transposed
                                              % This is equivalent to sum(x.^2) but shorter
          disp((x=str2num(input('','s')))*x') % Display the result

Call it like this:

input('');disp((x=str2num(input('','s')))*x')
5
1 2 5 7 8
143
\$\endgroup\$
0
\$\begingroup\$

C,65 bytes

j,k;main(i){for(;scanf("%d",&i)+1;j++&&(k+=i*i));printf("%d",k);}
\$\endgroup\$
0
\$\begingroup\$

Pyth, 10 bytes

sm^sd2ce.z

Try it here!

Takes input in the exact same format as specified in the challenge.

Explanation

sm^sd2ce.z  # .z = input lines

       e.z  # only take the last line
      c     # split on spaces
 m          # map each number d
   sd       # convert to integer
  ^  2      # square
s           # sum all squares

Alterntive solutions with more generous input formats

8 bytes if I may omit the number count:

sm^sd2cz

Try it here!

4 bytes if I may take the input as list of integers:

s^R2

Try it here!

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

Pyth, 9 bytes

s^R2re.z7

Try it here

Or 7 bytes if allowed to omit line 1

s^R2rz7
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You're missing the first line of input. \$\endgroup\$
    – isaacg
    Mar 29, 2016 at 16:32
0
\$\begingroup\$

Haskell, 48 bytes

main=interact$show.sum.map((^2).read).tail.words

How it works:

interact                 read whole input, pass it to a function and
                         print it's return value. The function is:

               words     split at whitespace into words
           tail          drop first element (the number on line #1)
    map(       )         convert each word 
           read             to integers
       (^2)                 and square
  sum                    sum all values
show                     convert back to string
\$\endgroup\$
0
\$\begingroup\$

GS2, 5 bytes

W",Φd

Try it online!

How it works

W      Find all numbers in the input; push a list.
 "     Discard the first number.
  ,    Square.
   Φ   Map the last instruction over the list of numbers.
    d  Compute the sum of all squares.
\$\endgroup\$
0
\$\begingroup\$

J, 4 bytes + 100

+/*:

Try it.

   +/*:1 2 5 7 8
143

A +100 punishment in the byte-count to myself for not having standard input.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Add proper input commands please. For some reason this question is very specific about I/O formats, and even normally you can't assume input is in a variable. (And it might be shorter anyway (if you have a 1 or 2 byte input command)) \$\endgroup\$ Mar 29, 2016 at 20:04
  • \$\begingroup\$ Thanks for your comment. Does J really have an input command? \$\endgroup\$
    – Leaky Nun
    Mar 30, 2016 at 5:15
  • \$\begingroup\$ Yes, J has I/O. \$\endgroup\$
    – Dennis
    Mar 30, 2016 at 6:09
0
\$\begingroup\$

Japt, 8 bytes

Ns1 mp x

Try it in the Japt Interpreter.

How it works

The code is parsed as

N.s(1).m(p).x()
  • N is a variable; it contains the parsed input.
  • s(1) discards the first array element.
  • m(p) maps p (square) over the remaining array.
  • x() computes the sum of the array of squares.
\$\endgroup\$
0
0
\$\begingroup\$

Pylongolf2, 8 bytes

c| n²+~

 

c  - read the input
|  - split by space (note the 1 space after |)
n  - convert all of that in the list to numbers
²  - Square everything in the list
+  - Sum everything in the list
~  - Print it
\$\endgroup\$
0
\$\begingroup\$

Batch, 76 bytes

@set/px=
@set/px=
@set t=0
@for %%n in (%x%)do set/at+=%%n*%%n
@echo %t%
\$\endgroup\$
0
\$\begingroup\$

GolfScript, 13 bytes

Input is extremely easy for GolfScript.

~](;{.*}%{+}*

Try it online!

Explanation

~             # Take & Evaluate the input
 ]            # Wrap the whole input into a list
  (;          # Remove the first item
    {.*}%     # Map with squaring
         {+}* # Reduce by summation
\$\endgroup\$
0
\$\begingroup\$

JavaScript (V8), 38 bytes

x=>Math.hypot(...x.match(/\s\d+/g))**2

Try it online!

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

Thunno 2 S, 4 bytes

sON²

Attempt This Online!

With normal I/O it would be 1 byte (with S flag):

²

Attempt This Online!

Explanation

sON²  # Implicit input
s     # Discard the first input
 O    # Split the second input on spaces
  N   # Convert each string to a number
   ²  # Square each number
      # Implicit output of the sum
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.