Daftar perbedaan daftar bilangan bulat adalah perbedaan daftar anggota yang berurutan.
Misalnya daftar perbedaan
1, 3, 2 ,4
aku s
2, -1, 2
Tugas Anda adalah mengambil daftar perbedaan dan menampilkan seperti apa daftar perbedaan jika daftar asli disortir.
Misalnya daftar perbedaan
2, 1, -2, -1
Mungkin mewakili daftar
2 4 5 3 2
Yang saat disortir adalah
2 2 3 4 5
Yang memiliki daftar perbedaan
0 1 1 1
Ini adalah kode-golf sehingga jawaban akan dinilai dalam byte dengan lebih sedikit byte lebih baik.
code-golf
array-manipulation
Wisaya Gandum
sumber
sumber
[-2, 100, -2, -1]
, misalnya.Jawaban:
05AB1E , 4 byte
Cobalah online!
Penjelasan
sumber
Undelta
05AB1E memiliki built-in niche paling banyak. o0Undelta
ಠ ___ ಠPython 3 dengan Numpy ,
565453 byte2 byte off berkat @Artyer (Numpy
sort
bukannya standarsorted
). 1 byte off berkat @ notjagan (pindah0
kecumsum
)Kode mendefinisikan fungsi anonim yang memasukkan daftar atau array Numpy dan menampilkan array Numpy.
Cobalah online!
sumber
numpy
jauh lebih lama. Saya akan kembali besok untuk memperbaiki ini, karena saya melihat Anda sudah capped. Sangat bagus!diff(sort([0 cumsum(x)]))
(di Matlab,[ ]
adalah penggabungan)0
kecumsum
.Mathematica, 40 byte
sumber
Differences@Sort@FoldList[+##&,1,#]&
Jelly , 6 byte
Cobalah online!
sumber
Sekam , 4 byte
Cobalah online!
Penjelasan
sumber
scanl(+)0
di Haskell.Pyth , 9 byte
-1 byte terima kasih kepada @EriktheOutgolfer .
Test Suite.
Pyth , 10 byte
Cobalah online! atau Coba lebih banyak kasus uji .
sumber
+0sM._
alih-alih.u+YNQ0
untuk -1.m=+Z
is a same length variant forsM._
, but sadly it doesn't seem like it can be any shorter.JavaScript (ES6),
5756 bytesSaved 1 byte thanks to @ETHproductions
Demo
Show code snippet
sumber
.sort((a,b)=>a-b)
That's the way to get deltas? By sorting with subtraction? :Pmap()
gives the deltas. This code sorts them. The 2nd map rebuild the new deltas. The JSsort()
method uses lexicographical order by default. So, we need this specialized callback for numbers > 9 (sadly).-p+(p=n)
grinds my gears, but sadly there's no better way... unless...Java 8, 123 bytes
The standard solution: cumulative sum input, sort, then diff. No substantial implementation tricks either.
Cast to
Consumer<int[]>
. Output is mutated input.Try It Online
Ungolfed lambda
Acknowledgments
sumber
l->{int s=l.length,d[]=new int[s+1],i=0;for(;i<s;)d[i+1]=d[i]+l[i++];java.util.Arrays.sort(d);for(i=0;i<s;)l[i]=-d[i]+d[++i];}
(beware SE's invisible characters when copy/pasting)for(;i>0;)l[i-1]=d[i]-d[--i];
(last loop)for(;i-->0;)l[i]=d[i+1]-d[i];
of the same length. Update to come.l->{int s=l.length,d[]=new int[s+1],i=0;while(i<s)d[i+1]=d[i]+l[i++];for(java.util.Arrays.sort(d);i-->0;l[i]=d[i+1]-d[i]);}
.Brachylog, 15 bytes
Try it online!
sumber
R,
3132 bytes-4 bytes thanks to @user2390246 for
diffinv
+5 bytes from Jarko for
cat
Reads from stdin, writes to stdout.
diffinv
is an inverse ofdiff
for a given starting value (0 by default). Since it'sdiff
ed again, it doesn't matter what that value is.As pointed out by Jarko Dubbeldam, I needed to properly output the result, at the cost of five bytes. Alas.
Try it online!
sumber
source
) this doesn't output anything.diffinv
rather thancumsum
you don't need to prepend zero.Python 2, 83 bytes
Try it online!
Horrible solution.
sumber
+=
operator on lists works with any iterable, so you can user+=r[-1]+i,
instead ofr+=[r[-1]+i]
and save one byte.Perl 6, 46 bytes
Try it
Expanded:
sumber
Haskell, 74 bytes
Try it online!
Straightforward.
sumber
=<<
from the function monad comes in handy:(zipWith(-)=<<tail).sort.scanl(+)0
zipWith
.TI-Basic (TI-84 Plus CE), 23 bytes
Prompts for user input. The list must be input with a leading
{
, with numbers separated by,
, and with an optional trailing}
.TI-Basic is a tokenized language;
ΔList(
andcumSum(
are two-byte tokens, all other tokens used are one byte each.Example run (with
NAME
as the program name and{4,-2,7,-4,0}
as the input):Explanation:
sumber
L
's?C++ (gcc), 136 bytes
As unnamed generic lambda, assuming input to be like
std::list
and returning via reference parameter.Try it online!
Ungolfed:
sumber
Pyth, 8 bytes
Demonstration
sumber
TI-Basic, 20 bytes
sumber
Perl 5, 87 + 1 (-a) = 88 bytes
Try it online!
sumber
VB.NET (.NET 4.5), 109 bytes
A function that expects a list as input and modifies it directly. The original parameter can then be used for output
Try it online!
sumber
APL (Dyalog),
1514 bytes-1 byte thanks to ngn.
+\
cumulative sum0,
prepend a zero(
…)
apply the following tacit function on that:⊂
enclose (so we can pick multiple items)⍋⊃¨
let each of the indices that would sort the argument pick from that¯2-/
reversed pairwise differenceTry it online!
Original solution found by the Code Golf Hackathon participants at the Dyalog '17 User Meeting:
Try it online!
⎕
prompt for input0,
prepend a zero+\
cumulative suml←
store asl
⍋
find the indices that will sortl
l[
…]
use that to index intol
¯2-/
reversed pairwise differencesumber
MATL, 6 bytes
Try it online!
sumber
Gaia, 7 bytes
Try it online!
Explanation
sumber
k, 16 bytes
Try it online!
sumber
Japt, 10 bytes
Test it
sumber
Röda, 42 bytes
Try it online!
This is similar to the Perl 6 answer.
.sort
is|sort
,.rotor(2=>-1).flat
is|slide 2
and.map(*R-*)
is|[_2-_1]
.Explanation:
The statement
[i]if i+=_
is equivalent toThe
+=
operator does not push values to the stream, so it is truthy. I could also have used some kind of block (eg.{|j|i+=j;[i]}_
) to tie the addition and pushing statements together, butif
is shorter.sumber
Julia 0.6.0 (34 bytes)
Pretty much a copy of what has been done in R and Python 3
x->diff(sort(cumsum(vcat([0],x))))
sumber
J, 10 bytes
explanation
"sort under scan sum": In J, the Under conjunction
&.
applies the transformation to its right to the input, then applies the verb to its left (in this case sort/:~
) and then does the reverse transformation. That is, J understands how to invert a scan sum, which is exactly what's needed here: the successive differences are the input that, when scan-summed, will produce that scan-sum.Try it online!
sumber