Ваша программа должна принимать массив в качестве входных данных.
Массив:
- Всегда будет одномерным
- Будет содержать только целые числа
- Может быть пустым
Программа должна обратить массив, а затем добавить элементы к оригиналу, например:
Входные данные: [1, 2, 3]
Оригинал: [1, 2, 3]
Перевернутый: [3, 2, 1]
[1, 2, 3]
+ + +
[3, 2, 1]
[1+3, 2+2, 3+1]
Выход: [4, 4, 4]
Тестовые случаи:
#In #Out
[8, 92], [100, 100]
[1, 2, 3], [4, 4, 4]
[5, 24, 85, 6], [11, 109, 109, 11]
[], []
[999], [1998]
Это код-гольф , выигрывает самый короткий код (в байтах)!
code-golf
array-manipulation
Ноа Кристино
источник
источник
Ответы:
Haskell , 20 байтов
5 байтов сэкономить, изменив на свободную точку, как предложено nimi
Попробуйте онлайн!
источник
zipWith(+)=<<reverse
.main
для компиляции.=<<
из функции монады , которая определяется как:(=<<) f g x = f (g x) x
. Здесь написано в инфиксах:(zipWith(+) =<< reverse) x
->zipWith(+) (reverse x) x
.Желе , 2 байта
Попробуйте онлайн!
или
Попробуйте онлайн! (спасибо @Mr. Xcoder за вторую программу)
объяснение, хотя это довольно очевидно
For empty array
[]
, this outputs nothing. That is correct. Jelly's representation of an empty list is just simply nothing. Note that Jelly's representation of a list with a single element is just the element itself. AppendŒṘ
to the code to see the Python internal representation of the output.источник
[9]
it outputs 18 instead of[18]
, and 2) when tested on[]
it doesn't output anything.+Ṛ
works too.JavaScript (ES6), 27 bytes
Show code snippet
источник
05AB1E, 2 bytes
Try it online!
источник
R+
also works for 2 bytes.Python 2, 32 bytes
Alternative solution without
zip
(35 bytes):Try it online!
источник
Python 2, 40 bytes
Try it online!
The other, shorter Python answer replaces a list comprehension with
map
. Wish I'd thought to do that faster. ;-;источник
Japt, 7 bytes
Try it online! with the
-Q
flag to format the output array.Explanation
Implicit:
U
= input arrayMap the input by the following function...
The value, plus the value in the input array at index...
-(index+1)
, which gets elements from the end of the array.источник
Ruby, 25 bytes
Try it online!
источник
Mathematica, 12 bytes
Try it online!
источник
Python 2,
3332 bytes-1 byte thanks to @xnor
Try it online!
источник
l*1
saves a byte.l*1
, any elaborationl*1
makes a copy of the listl
. If we would not make a copy,pop()
would delete elements from the list before they were accessed in the for loop.Brachylog, 6 bytes
Try it online!
источник
C# (.NET Core),
6160 bytes-1 byte thanks to TheLethalCoder
Try it online!
Byte count also includes:
For explanation - Zip function in LINQ takes two collections and executes given function for all corresponding elements, ie. both first elements together, both second elements etc.
источник
Zip
call to so no need for theToArray()
. Nice job!CJam, 7 bytes
Try it online!
источник
""
for[]
, that's weird. Not your fault just the language.[]
.[]
, and[4]
, so it's fine.APL (Dyalog), 3 bytes
Try it online!
Explanation
источник
⌽+⊢
with no input⍬
, the empty vector. With no argument, it prints the train by itselfJ, 3 bytes
Reverse, sum.
Try it online!
источник
R,
1716 bytes-1 byte thanks to djhurio
Reads from stdin; returns the vector;
numeric(0)
is the zero-length numeric vector for the empty list.Try it online!
источник
numeric(0)
numeric(0)
in R.rev(l<-scan())+l
, 16 bytes?pryr::f(rev(x)+x)
Clojure,
2017 bytes3 bytes saved thanks to @MattPutnam
Seems to be quite competitive with non-golfing languages.
See it online
источник
rseq
instead ofreverse
.Pyth, 3 bytes
Try it here.
источник
sV_
PowerShell, 26 bytes
Try it online!
Takes input as command-line arguments.
источник
C, 49 bytes
источник
a,n,b
bea,b,n
or something?b
isn't a parameter for the function, just an extra definition stuffed in there for golfing reasons.a
must be a pointer to integers, andn
must be how many integers there are in the array.PowerShell,
4032 bytesTry it online!
Takes input as individual command-line arguments, which is allowed as one of the native list format for PowerShell. Then loops through each element (i.e., a shorter way of looping through the indices), adding the element counting from the back (
-1
indexed) to the current element (0
indexed, hence the decrement-1
). Those are left on the pipeline and output is implicit.Saved 8 bytes thanks to @briantist
источник
Write-Output
happens, it puts things on stdout one item per line. For example, you can see here that, when captured, the object is indeed anarray
type.param
and then replace1..$n
with1..($n=$args)
?Java 8,
61575653 bytes-1 byte and bug-fixed thanks to @Nevay.
-3 bytes thanks to @OliverGrégoire.
(It was a port of (and golfed by
48 bytes) of @jkelm's C# answer, but now it's a different shorter solution thanks to @OliverGrégoire.)Explanation:
Try it here.
The method modifies the input-array to save bytes, so no need for a return-type.
источник
a->{for(int i=0,l=a.length;i<l/2;a[i]=a[l+~i]+=a[i++]);}
.1,2,3
(returns4,2,4
instead of4,4,4
), the loop has to run as long as2*i<l
, noti<l/2
.l-i-1
, just couldn't come up with it..a->{for(int l=0,r=a.length;l<r;a[l]=a[--r]+=a[l++]);}
.l
andr
makes sense for your implementation, so I've used those as well (and added an explanation).Ohm, 3 bytes
Try it online!
источник
[]
it doesn't output anything but that's just the language.Actually, 4 bytes
Try it online!
источник
anyfix, 3 bytes
The version on TryItOnline! is an outdated version of anyfix, which contains a few fatal errors such as not being able to add lists because of typos in the source code. Use the code on GitHub instead.
источник
Neim, 2 bytes
This is a function that takes input on the top of the stack and outputs on the top of the stack.
Try it online!
источник
Röda, 22 bytes
Try it online!
This is an anonymous function that takes in an array and returns a stream of values, which the TIO link outputs separated over newlines.
Explanation
источник
JavaScript (ES6),
3433 bytesSaved a byte thanks to @ETHproductions.
Show code snippet
источник
-i-1
to+~i
.MATL, 3 bytes
Try it online!
Extremely straightforward.
t
duplicates the input.P
flips (reverses) it, and+
adds the two arrays element wise.источник
PHP, 59 bytes
takes input from command line arguments; empty output for empty input
Yields a warning in PHP>7.0. This version does not (60 bytes):
источник