Hmm, cool, but it doesn't check. I put in (8636 460 17372 242 ...) and got out (28501.5412844037 20325.5412844037 37237.5412844037 20107.5412844037 ...)

Here is my complete program. Since it failed on a longer test vector, it might be an overflow or something?

use strict; use warnings; use Benchmark 'cmpthese'; sub decode { my $total= shift; my $delta= 0; my $diff= 0; foreach (@_) { $delta += $_; $diff += $delta; } my $original= ($total-$diff)/(1+scalar @_); my @results; my $value= $original; foreach (@_) { $value += $_; push (@results, $value); } return ($original, @results); } sub decode2 { my $total= shift; my $delta= 0; my $diff= 0; foreach (@_) { $delta += $_; $diff += $delta; } my $original= ($total-$diff)/(1+scalar @_); my $value= $original; my @results= map { $value += $_; } @_; return ($original, @results); } sub decode3 { my ($dc, $next); $_[0] = -$_[0]; $dc -= $_ for @_; # subtract the sum of all elements from the firs +t $_[0] = $dc/@_ - $_[1]; return map { $next += $_ } @_; } sub encode { my $prev= shift; my $total= $prev; my @results; while (my $value= shift) { my $delta= $value-$prev; push (@results, $delta); $prev= $value; $total += $value; } return ($total, @results); } sub test { print ("original: @_\n"); my @x= encode (@_); print ("encoded: @x\n"); my @y= decode3 (@x); print ("decoded: @y\n"); die "error- mismatch" unless scalar(@y)==scalar(@_); foreach (@_) { die "error- mismatch" unless $_ == shift(@y); } } my @d1= (1000, 1001, 980, 1024, 1023); test (@d1); my @d2= ( 8636, 460, 17372, 242, 305, 347, 212, 102, 738, 43797, 1769, 832, 16127, 481, 233, 441, 188, 2812, 430, 463, 24576, 829, 5078, 50620, 89922, 1536, 339, 396, 66560, 5629, 106, 4404, 3172, 28672, 1496, 2370, 495, 1264, 793, 5148, 95, 8192, 408, 4096, 368, 456, 776, 36080, 219104, 980912, 11607, 43496, 219, 570, 563, 53248, 11361, 1916, 718, 20480, 3465, 32, 88, 43, 100, 226, 10042, 769, 262, 246, 2837, 530, 2507, 1529, 2560, 1146, 457, 517, 43520, 1448, 260, 75, 247, 775, 927, 517, 16606, 126, 832, 5, 457, 94208, 52110, 2873, 1228, 94208, 56704, 480, 48, 173, 28, 24576, 391, 145, 1615, 239, 106, 508, 704 ); test (@d2); my @x= encode (@d2); cmpthese ( 0, { 'for' => sub { decode (@x); }, 'map' => sub { decode2 (@x); }, 'Aristotle' => sub { decode3 (@x); }, });

In reply to Re: Re: Perl Drag Racing - sum/delta list by John M. Dlugosz
in thread Perl Drag Racing - sum/delta list by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.