in reply to Re: Spliting array into nested sequential arrays
in thread Spliting array into nested sequential arrays

Not that I'm suggesting I'd have produced that in an interview mind you!
They gave me two questions and this was the 2nd one. I am not sure what they expected to see in 45 minutes.

Thanks for the code. I see what I could have done differently to have given them not "naive" solution. I suppose being succinct is important as well.

  • Comment on Re^2: Spliting array into nested sequential arrays

Replies are listed 'Best First'.
Re^3: Spliting array into nested sequential arrays
by GrandFather (Saint) on Oct 29, 2014 at 03:41 UTC

    It's not so much being succinct, although clean code often is. The key bits are:

    • Handle the initial empty list case outside the loop to make the loop cleaner.
    • Use Perl's "last element index" (-1) so you don't need to count stuff.
    • Handle "time for a new segment" and "add a value" as independent operations.
    • Work with the index of the next interesting value, not the value.
    • Clean up as you go.

    although upon reflection I'd change @lists = grep{scalar @$_} @lists; to splice @lists, $smallest, 1 if !@{$lists[$smallest]};.

    Perl is the programming world's equivalent of English

      And to add just a little to what GrandFather said, keep in mind that readability = maintainability in most cases. If hired, you will be writing code 5 years from now that someone just like yourself now will have to maintain. So the goal is to come up with reasonable solutions that will show knowledge of the tools while at the same time showing consideration of the work environment.

      You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.