Hi pr33,

Wow, your question certainly got the Monks working on lots of solutions!

I recommend understanding every single program in this thread... Understand exactly how they work.

I am curious if this was some sort of homework assignment or not?
I am also curious about what other languages you know?

I did not assume that this was homework for the simple reason that the beginner classes that I work with would not have even covered array indicies before an assignment like this would be given. You wouldn't be thinking in that direction because, the class wouldn't have even talked about it! Of course it could be that you are in a "bad class". In any case all the better to show some better ways!

None of the code presented (except yours) uses splice() and array index. I hope that sends you a clear message! Normally use the Perl array iterators which do not depend upon some [$i] index value. Of course indicies can and should be used in some situations, but they should not be your first thought.

My code perhaps appears at first glance to be "complicated" as compared to some of the more brief solutions. I will try to explain the thought process. Maybe other Monks can do that for their code also?

I rejected at first thought any idea of splice() and index as being too complicated. Also your code did not require me to modify the input array "in place" - that means to me that I can use extra memory for the "solution".

I started with a foreach loop over all of the input numbers. There are actually 4 conditions:

I worked on those parts (ran the code) and made decisions about not needing a $flag and that @stack could serve that function.

Then I coded:

There was some short back and forth to fine tune the singleton 7 idea, but there was a place for it within the 4 basic cases.

I recommend looking at Laurent_R's solution here. It is shorter but still has the basic 4 different situations.

Of course, "shorter, fewer lines" does not always mean "better" or even "faster". I tried to present some code for you that you could understand and modify if necessary to meet your needs.


In reply to Re^3: Ignore a range of numbers ina List by Marshall
in thread Ignore a range of numbers ina List by pr33

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.