Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
But that isn't an increment of 0.1. 1.2 is in your list twice, and 1.15 doesn't belong there at all. Are you really talking about floating values, or are you talking about some kind of version number or the like?

Anyway, the algorithm is easy. Given the increment and the array, take the starting value in the array and add the increment to it to form your expected value. Then loop through the array from the second element on: for each element, while it's more than the expected value, put the expected value in your list of missing values and increment it. If the array element is equal to your expected value, increment the expected value and go on to the next array element.

Where I say "more than" or "equal to", allow for floating point imprecision and rounding error.

Alternatively, collect all values equal to the starting value plus some multiple of the increment that are between the starting and ending values (perhaps as hash keys), then remove all those that appear in your array. What's left are the missing values.

If the increment isn't provided, you could initially take it as the difference between the first two elements in the array, then correct it if you find values that don't work with that increment.

In reply to Re: Find out missing floating value in an array by ysth
in thread Find out missing floating value in an array by manimarank

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (4)
    As of 2020-08-04 03:26 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (31 votes). Check out past polls.