tags:

views:

351

answers:

4

I got this question in a previous interview and couldnt do it , any idea?

  1. What does this do:

    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=( 
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++; 
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++ 
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=` 
    
A: 

I am not a Ruby expert by any means by the first step should be make it into a format you can read. I broke it down by line.

`$=`;

$_=\%!;

($_)=/(.)/;

$==++$|;

($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=($!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++; 

$_++;

$_++;

($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);

$,++;

$,++;

$^|=$";

`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`

I cheated and tried to run it, and it doesn't work. I get an unexpected null error.

Don't feel bad if you can't do this. This seems pointless. Programming questions should try to test your skills not test you on something that if somebody is really using it would mean there application would be really bad.

David Basarab
I'm still just as confused as before.
Jesse J
+1  A: 

Here, I changed all the special Ruby globals into single-letter variables and inserted some whitespace:

`a`
n = \%!
(n) = /(.)/
a = ++o
(b, c, d, f, e, g, h, i, j, k, l, @%) = (m =~ /(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/, e), a++
b++
b++
n++
n++
(n, f, d) = (j . e . "gcp[q]nfdlp[q]", e & j, i,)
d++
d++
h |= e
`nfdclgjkp[q]bjk${#}p[q]gfehjk.>&a`

Whoever wrote this doesn't understand Ruby. There's no increment operator in Ruby. Tokens like \%! and @% mean nothing in Ruby. You can't interpolate variables, even global variables, in strings or backquoted commands, as in "$=". The dot . is not a concatenation operator in Ruby. I don't think this is Ruby. It's like a hybrid of languages.

yjerem
"It's like a hybrid of languages" sort of like Perl. :)
Schwern
A: 

This looks closer to Perl, to be honest, but in any case pretty nonsensical.

Karl
+15  A: 

This is Perl code that prints out "Just another Perl hacker."

While most of the $_, $=, etc. variables are available in Ruby as well, the presence of statements such as $,++ indicate Perl, which actually has pre- and post-increment operators, unlike Ruby.


I went in with Vim and replaced all the symbols with their English equivalent. I munged something up since the output is now "Just another Per hacker" (missing the L on Perl), but here's what I came up with:

use English;
`$FORMAT_LINES_PER_PAGE`;
$ARG=\%!;($ARG)=/(.)/;$FORMAT_LINES_PER_PAGE=++$OUTPUT_AUTOFLUSH;
($INPUT_LINE_NUMBER,$/,$OUTPUT_FIELD_SEPARATOR,$OUTPUT_RECORD_SEPARATOR,$LIST_SEPARATOR,$SUBSCRIPT_SEPARATOR,$FORMAT_TOP_NAME,$OFMT,$FORMAT_NAME,$MULTILINE_MATCHING,$FORMAT_LINE_BREAK_CHARACTERS,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$LIST_SEPARATOR),$FORMAT_LINES_PER_PAGE++;
$INPUT_LINE_NUMBER++;
$INPUT_LINE_NUMBER++; $ARG++;$ARG++;
($ARG,$OUTPUT_RECORD_SEPARATOR,$OUTPUT_FIELD_SEPARATOR)=($FORMAT_NAME.$LIST_SEPARATOR."$SUBSCRIPT_SEPARATOR$/$FORMAT_PAGE_NUMBER[$CHILD_ERROR]$ARG$OUTPUT_RECORD_SEPARATOR$OUTPUT_FIELD_SEPARATOR$FORMAT_LINE_BREAK_CHARACTERS$FORMAT_PAGE_NUMBER[$CHILD_ERROR]",$LIST_SEPARATOR&$FORMAT_NAME,$OFMT,);
$OUTPUT_FIELD_SEPARATOR++ ;
$OUTPUT_FIELD_SEPARATOR++;
$FORMAT_TOP_NAME|=$LIST_SEPARATOR;
`$ARG$OUTPUT_RECORD_SEPARATOR$OUTPUT_FIELD_SEPARATOR$/$FORMAT_LINE_BREAK_CHARACTERS$SUBSCRIPT_SEPARATOR$FORMAT_NAME$MULTILINE_MATCHING$FORMAT_PAGE_NUMBER[$CHILD_ERROR]$INPUT_LINE_NUMBER$FORMAT_NAME$MULTILINE_MATCHING${#}$FORMAT_PAGE_NUMBER[$CHILD_ERROR]$SUBSCRIPT_SEPARATOR$OUTPUT_RECORD_SEPARATOR$LIST_SEPARATOR$FORMAT_TOP_NAME$FORMAT_NAME$MULTILINE_MATCHING.>&$FORMAT_LINES_PER_PAGE`
Mark Rushakoff
Good to know! I fail to understand why they would make this an interview question, however. If you write Perl like that you're doing something horribly wrong..
Karl
> If you write Perl like that you're doing something horribly wrong..FTFY: If you write Perl you're doing something horribly wrong. ;)
tpdi
@Karl: Having just conducted a few technical interviews myself, I can't even imagine what the interviewer was thinking. Even if the interviewer was planning on saying "I was just joking around, it's a JAPH program", that's completely inappropriate in my opinion. Knowledge or understanding of what this code does demonstrates nothing, as even when you replace the symbols with meaningful names it is still unintelligible.
Mark Rushakoff
To try to understand that, take a look at this:http://www.kichwa.com/quik_ref/spec_variables.htmlIt does explains the special variables used.
Alexandre
This japh was created by Enoch Roode: see http://en.wikipedia.org/wiki/Just_another_Perl_hacker
snoopy
@tpdi: I flagged your comment as offensive. I'm sure you meant well, but unjustified slagging of specific technologies is not welcome here.
Ether
Ether: how exactly did they mean well?
MkV
@MkV, the smiley is intended to indicate that the comment is meant in jest. Compare `Your choice of technology is stupid and and so are you.` with `Your choice of technology is stupid and and so are you. :)` See how the smiley makes the comment so much less offensive?
daotoad