13

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

  1. What does this do:

    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=( 
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++; 
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++ 
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=` 
    
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
fenec
  • 5,637
  • 10
  • 56
  • 82
  • Where you allowed to convert it to a more readable format? – David Basarab Jul 16 '10 at 21:17
  • 24
    What does it do? It should tell you that you had an interviewer who wanted to show off, not one who wanted to access your abilities. – tpdi Jul 16 '10 at 21:19
  • I'm don't know Ruby, but I get syntax errors when trying to run it. – sberry Jul 16 '10 at 21:20
  • 19
    @tpdi - It also screams don't work here. – David Basarab Jul 16 '10 at 21:21
  • For one, I have no idea. Second, Ruby says it has syntax errors. – Jesse Jashinsky Jul 16 '10 at 21:23
  • It confirms to me that I would not want to work at that company. Care to name names? – kingjeffrey Jul 16 '10 at 21:32
  • 1
    I would have asked what it did after the interview and to have them show me – Woot4Moo Jul 16 '10 at 21:37
  • David, I thought I'd implied *that* ;) – tpdi Jul 16 '10 at 21:50
  • 3
    This code is on the JAPH Wikipedia page: http://en.wikipedia.org/wiki/Just_another_Perl_hacker – mob Jul 16 '10 at 22:16
  • Maybe you could have moved the discussion to Good Programming Practices: Maintainability, and Tooling (if the goal is to make the code hard to steal, there are obfuscaters to do that...) – pascal Jul 16 '10 at 22:47
  • 1
    This was an interview question? Seriously? I'd tell them it was bullshit, and if they wanted to talk about things that really mattered, I'd stick around. If not, I'd leave. That's ridiculous. – Brian Genisio Jul 17 '10 at 04:55
  • The "question" makes at least some sense - any long time Perl programmer would immediately see: "should be some kind of obfuscated Perl", lets try it out. -- So the "answer" would be the ability of the applicant to tell exactly the "type of program". Exactly the same thing as happened here on stackoverflow (some members immediately saw what it could be and looked it up on google). – rubber boots Jul 18 '10 at 13:07
  • It's a trick question - if you answered it straight without any objections, they'd refuse to hire you! – Andrew Grimm Oct 22 '10 at 07:48

5 Answers5

17

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
  • 249,864
  • 45
  • 407
  • 398
  • 6
    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 Jul 16 '10 at 21:47
  • 5
    > If you write Perl like that you're doing something horribly wrong.. FTFY: If you write Perl you're doing something horribly wrong. ;) – tpdi Jul 16 '10 at 21:52
  • 10
    @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 Jul 16 '10 at 21:59
  • To try to understand that, take a look at this: http://www.kichwa.com/quik_ref/spec_variables.html It does explains the special variables used. – Alexandre Jul 16 '10 at 22:21
  • This japh was created by Enoch Roode: see http://en.wikipedia.org/wiki/Just_another_Perl_hacker – dwarring Jul 16 '10 at 22:36
  • @tpdi: I flagged your comment as offensive. I'm sure you meant well, but unjustified slagging of specific technologies is not welcome here. – Ether Jul 16 '10 at 23:18
  • Ether: how exactly did they mean well? – MkV Jul 16 '10 at 23:25
  • @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 Jul 18 '10 at 02:50
1

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.

Paige Ruten
  • 172,675
  • 36
  • 177
  • 197
0

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
  • 72,212
  • 42
  • 129
  • 156
0

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

Karl
  • 6,035
  • 5
  • 30
  • 39
0

Not related to Ruby I think, above encoded message is given at https://en.wikipedia.org/wiki/Just_another_Perl_hacker with the help of only punctuation. Another one is also provided there.

''=~('(?{'.('-)@.)@_*([]@!@/)(@)@-@),@(@@+@)'
^'][)@]`}`]()`@.@]@%[`}%[@`@!#@%[').',"})')
Vishal Zambre
  • 320
  • 1
  • 10