Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Tuesday Evening Code Quiz

by davorg (Chancellor)
on Oct 31, 2001 at 01:51 UTC ( [id://122211]=perlmeditation: print w/replies, xml ) Need Help??

Earlier this evening I was given a technical Perl test for a potential new job. Most of the questions were pretty straightforward, but I thought you might be interested to see this one:

What does the following code print out

@a = ("a", 1, "b", 2, "c", 3); $i = 0; grep($foo{$_} = $a[$#a - $i++], @a); print $foo{"c"}, "\n";

Secondly, what is an easier way to populate %foo?

I think I came up with pretty good answers for both parts, but I'd be interested to hear what you think.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you don't talk about Perl club."

Replies are listed 'Best First'.
(tye)Re: Tuesday Evening Code Quiz
by tye (Sage) on Oct 31, 2001 at 02:34 UTC

    Well the previous answers of @foo{@a}= reverse @a; are simpler code, they still don't make a lot of sense. To make sense of it, you would write:

    my %foo= ( a=>3, b=>2, c=>1 ); @foo{values %foo}= keys %foo;
    and then the reader of the code would realize which items "go together" and that %foo is a two-way lookup table.

            - tye (but my friends call me "Tye")
      However is this answer safe? If %foo (which appears to be global) already had something in it, you have completely messed that up. Plus you changed the scope horribly with your my you couldn't resist.

      The reverse answers may hide intent, but they are less likely to break other hidden behaviour assumptions which something, somewhere, might rely on.

      So while I like your answer better, I would suggest that you note the assumptions you made...

Re: Tuesday Evening Code Quiz
by blakem (Monsignor) on Oct 31, 2001 at 02:06 UTC
    It took a minute to make sure the code above didn't have an off-by-one error, but I think it does the same thing as:
    @foo{@a} = reverse @a;

    -Blake

      Yeah. That's what I xame up with. No idea when that would ever be useful tho' :)

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

        Perhaps a good answer for the first question would have been....
        Not enough information. The output is dependent on the unspecified values of $[ $, and $#

        -Blake

Re: Tuesday Evening Code Quiz
by japhy (Canon) on Oct 31, 2001 at 02:37 UTC
    I made sure to reply without looking at other responses.
    @a = ("a", 1, "b", 2, "c", 3); $i = 0; grep($foo{$_} = $a[$#a - $i++], @a); # for each element $_ in @a # set $foo{$_} to the element OPPOSITE $_ in the array # %foo = ( # a => 3, 1 => 'c', b => 2, # 2 => 'b', c => 1, 3 => 'a', # ) print $foo{"c"}, "\n"; # 1
    As far as an easier way to populate the hash, I'd do:
    @foo{@a} = reverse @a;

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Tuesday Evening Code Quiz
by dws (Chancellor) on Oct 31, 2001 at 02:19 UTC
    At first glance, $foo will end up holding a map and an inverted map drawn from @a. Looks like a long-winded way of saying   %foo = (@a, reverse @a); In which case $foo{"c"} would be 3.

    On second glance, first glance was wrong, wrong, wrong. It's   %foo{@a} = reverse @a;

    Partial credit?

      How about partial partial credit?
      %foo{@a} = reverse @a; #should be @foo{@a}

      A hash slice needs '@', otherwise this yields a syntax error (under version 5.6.1 at least).

      @foo{@a} = reverse @a;

      MM

Re: Tuesday Evening Code Quiz
by pixel (Scribe) on Oct 31, 2001 at 02:25 UTC

    Ew! grep in a void context! Bad code! Nasty code!

    I hope you won't accept a job in a place that produces such monstrosities :-)

    Blessed Be
    The Pixel

(jeffa) Re: Tuesday Evening Code Quiz
by jeffa (Bishop) on Oct 31, 2001 at 02:41 UTC
    Similar to tye's, but much more modifiable ;)
    @a = ('a'..'c'); @foo{@a} = reverse (1..@a); @foo{values %foo} = keys %foo;

    jeffa

Re: Tuesday Evening Code Quiz
by maverick (Curate) on Oct 31, 2001 at 02:08 UTC
    So, jeffa wanted me to look at this...so, I say the answer to part one is $foo{'c'} == 1. jeffa is my witness that I didn't run the code to figure it out.

    How about

    @foo{@a} = reverse @a;
    for the second part.

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Log In?
Username:
Password:

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

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

    No recent polls found