Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: (OT) Interview questions -- your response?

by Anonymous Monk
on Sep 03, 2002 at 21:28 UTC ( [id://194903]=note: print w/replies, xml ) Need Help??


in reply to (OT) Interview questions -- your response?

My full answers would be the following:
1 a) nested linear scans b) use a hash 2) @array[0..$#array] = @array[reverse 0..$#array]; That's *not* a built-in array-reversal method. 3) Modularize into the three primary layers: requests, db queries, output templates.

Questions?

Replies are listed 'Best First'.
•Re: Re: (OT) Interview questions -- your response?
by merlyn (Sage) on Sep 03, 2002 at 22:02 UTC
    @array[0..$#array] = @array[reverse 0..$#array]; That's *not* a built-in array-reversal method.
    Nor it it an "in-place" reversing method, failing the requirements of the question. You've made an entire copy of the array in a different part of memory.

    -- Randal L. Schwartz, Perl hacker

      True enough, and not an unexpected claim. Though one *could* argue that:
      @array[$left,$right] = @array[$right,$left];

      Isn't any more "in-place", as it is the same thing when the array is only two elements. And I didn't see where using a constant amount of additional memory was part of the requirement :-)

Re: Re: (OT) Interview questions -- your response?
by bbfu (Curate) on Sep 04, 2002 at 22:15 UTC

    2) @array[0..$#array] = @array[reverse 0..$#array]; That's *not* a built-in array-reversal method.

    Well, technically, doesn't 0..$#array create an (anonymous) array, which reverse then reverses? :)

    bbfu
    Black flowers blossum
    Fearless on my breath
    Teardrops on the fire
    Fearless on my breath

      Well, technically, doesn't 0..$#array create an (anonymous) array, which reverse then reverses? :)
      Technically no. The 0..$#array just creates a list from zero to the last index, reverse 0..$#array is the list of indices reversed. So we are not reversing the array, just creating a slice with with reversed indices and assigning that slice to the a slice with all the indices in order. I imagine a built-in array reversal would be more like Ruby's array.reverse! method.

        So we are not reversing the array...

        ...just reversing the list. List, array; array, list. While they are technically different (one is a Perl data structure, while the other is an anonymous collection of scalar values), I don't see how that difference really matters in this case. =) You're still using Perl's built-in to reverse a list/array, whether it's the original array, or the list of array indices. It's just semantics.

        I mean, by those standards, reverse technically accepts a LIST, not an ARRAY (and will flatten multiple arrays into a single list, for example), so you should be able to do: @array = reverse @array;, since reverse isn't actually reversing the array but, rather, the list passed to it (after which, the reversed list is passed back and stored into the original array, replacing the original contents). Of course, as Aristotle points out, both of these methods (using Perl's built-in reverse, and your method of reversing the list of indices) fail the "in-place" clause of the original problem, so it's all moot. =)

        bbfu
        Black flowers blossum
        Fearless on my breath
        Teardrops on the fire
        Fearless on my breath

        Perl has @array = reverse @array. That's the one that was not to be used; but since it isn't inplace I guess it fails the excercise's requirements anyway.

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://194903]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found