Write a Program in Disguise

14

4

The challenge is to create a program that solves any one of the HQ9+ problems while looking like another language.

Note: This is not a golf challenge.

Rules:

  • The program must perform one of the following tasks when run.
    • Print "Hello, World!"
    • Print its entire source.
    • Print the lyrics to "99 Bottles of Beer on the Wall" (as seen here).
  • At first glance the program should look like another language.

Grading

  • Working program that satisfies the rules. +3
  • Bonus points if the "other language" can be run (syntax valid). +1
  • Even more points if the "other language" can do something useful. +3
  • Lots of bonus points for the program being valid (and useful) in even more languages. +5 for each language over 2.

Kevin Cox

Posted 2013-11-22T20:01:46.210

Reputation: 451

Question was closed 2017-04-04T21:28:52.113

1

How are they graded? You need an objective criterion. Check here: http://codegolf.stackexchange.com/help/on-topic

– MrZander – 2013-11-22T20:08:41.597

Okay, I'm adding a points system. – Kevin Cox – 2013-11-22T20:11:25.880

1

So... you're looking for a polyglot?

– Iszi – 2013-11-22T20:45:34.730

Yes. But with emphasis on "hiding" the features of all but one language to an extent. – Kevin Cox – 2013-11-22T20:51:02.963

4print ("Hello, World"); by itself will run in a lot of languages – marinus – 2013-11-24T16:58:59.510

What about the HQ9+ problem "increment the counter by one"? :P – FireFly – 2013-11-26T17:40:52.197

I kinda deemed that boring, but if you come up with something interesting I'm sure people will enjoy it :D – Kevin Cox – 2013-11-26T17:57:06.553

Answers

20

T-SQL

Print 'Hello, World!' -- she said.

Looks like English.

Danko Durbić

Posted 2013-11-22T20:01:46.210

Reputation: 10 241

18

Ruby + C + Whitespace.

This program is C but looks like (and runs as) Ruby. I also managed to throw some whitespace in there.

C prints its own source (not quite a quine unfortunately). Ruby prints 99 bottles of beer and whitespace says hello.

#include <stdio.h>  
#define def int          
#define beer main(int i){char b[1024];          
#define print(v) print(int v){            
#define end ;}                
#define if(x) fputs(b,stdout); fgets(b,1024,f); if(x){               
#define else ;} else{                       
#define puts                    
#define print        
#define open(fn) FILE *f = fopen(fn,"r");             
#define while(x) fgets(b,1024,f); while(!feof(f)){                      


def beer                
    # define beer      

    open("test.c")

   i=100;


    while ( i >= 0 )
        if ( i > 0 )
            print "#{i} bottle#{i==1&&''||'s'} of beer on the wall, "
            puts  "#{i} bottle#{i==1&&''||'s'} of beer."
            print "Take one down and pass it around, "
            puts  "#{i-1} bottle#{i==2&&''||'s'} of beer on the wall."
            puts
        else
            puts "No more bottles of beer on the wall, no more bottles of beer."
            puts "Go to the store and buy some more, 99 bottles of beer on the wall."
        end
        i -= 1
    end

    return 0      
end

beer

And since SO isn't very nice to ws here is a link.

Kevin Cox

Posted 2013-11-22T20:01:46.210

Reputation: 451

15

Golfscript / HQ9+

9

A Golfscript quine that doubles as an HQ9+ program that prints the lyrics to 99 bottles of beer.

I know this isn't a golf challenge, but I couldn't resist the desire to answer this with one character.

Silvio Mayolo

Posted 2013-11-22T20:01:46.210

Reputation: 1 817

1And I was silly enough to assume HQ9+ couldn't look like another language :D – Kevin Cox – 2013-11-23T15:04:54.280

5Yes! And crazily it looks lie the number "9". – Pierre Arlaud – 2013-11-27T14:21:51.540

7

Perl, 7 points

99 <?php
for($i='no more',$_="`\2l\";7hF'`-{9\17p\t\2ZY\14fE\25T\133J`\35l\"76~\x3";
99>${2}="$i bottle".(${$i+=1}<'))'?+s?:s:!'((').' of beer';
${3}="
Take one down and pass it around, ${1}.

".ucfirst("${1}, ${2}.${3}"))${1}=$i||${2}?"${2} on the wall":'?;\'';
print substr("${1}, ${2}.${3}".
($_^"jE\3\2OXH2O\5\15\10M$_").", ${2} on the wall.",3,13*$i*$i);

This is a Perl program that prints Hello, World!.

Score breakdown:

  • Follows all rules: +3
  • Is valid syntax in another language: +1
  • Does something interesting in the other langauge: +3

Sidenote:

Once you know what it does it might seem 'obvious' where it's hanging out, but this is a dupe.

The following:

$_="`\2l\";7hF'`-{9\17p\t\2ZY\14fE\25T\133J`\35l\"76~\x3";
print$_^"jE\3\2OXH2O\5\15\10M$_"

will output:

Go to the store and buy some moreET[J`l"76~

primo

Posted 2013-11-22T20:01:46.210

Reputation: 30 891

7

JavaScript, 7 points

<!--
h='Hello, World!'
-->

/******<br>

<b>Hello, World!</b><br>

*******<br>

This is my <i>awesome</i> Hello World page<br>

*******<br>

Look I can make images I'm so good at HTML:<br>
<img src='http://pieisgood.org/images/slice.jpg' alt='awwww you cannot see my amazing image' /><br>
<hr>

These are the things this page has:<br>
<ul>
<li>Bold</li>
<li>Italics</li>
<li>Images</li>
<li>Lists</li>
</ul><br>

I like random characters! That's what this page will finish with:<br>
;37!&$89]"]0(!)89^!&8*/81+-0;h

When run in JavaScript, produces "Hello, World!"

When viewed as HTML, looks like this:

screenshot

Scoring

  • Working program that satisfies the rules +3
  • Other language syntax is valid +1
  • Other language does something useful (you can put any HTML you want in there) +3

Doorknob

Posted 2013-11-22T20:01:46.210

Reputation: 68 138

Subtle! This assumes that the implementation evaluating it prints out the completion value of the last statement to stdout, though. – FireFly – 2013-11-26T18:05:44.660

@FireFly yes, that's what eval and all consoles do. – Doorknob – 2013-11-26T19:11:02.270

eval doesn't print it to stdout; it returns it as the result of the function call expression. But yeah, I'm just nitpicking; sorry. :p – FireFly – 2013-11-26T19:25:21.387

6

C++/Python

#include <iostream>

#define print(a) int main() {std::cout << a << std::endl;}

print("Hello, World!")

Will run in Python (both 2.x and 3.x) and compile in C++ to print (with carriage return):

Hello, World!

Noodle9

Posted 2013-11-22T20:01:46.210

Reputation: 2 776

5

The following prints "Hello,World" (and a random table) in HTML and "Hello, World!" in Whitespace.

<html>             
<head>  
<title>                     
Hello,World!    
</title>                      
<body>  
<p>                       
Hello,World!    
</p>                            
<table> 
<caption>                 
Summary 
</caption>           
<tbody> 
<tr>                            
<th>Year</th>   
<th>Revenue</th>                            
</tr>   
<tr>                     
<td>2012</td>   
<td>$100,000</td>                     
</tr>   
</tbody>                  
</table>    
<footer>                
HTML5!  
</footer>  
<!--Comment-->
</html>

Davio

Posted 2013-11-22T20:01:46.210

Reputation: 151

You can find a version of "99 Bottles" here http://www.99-bottles-of-beer.net/language-whitespace-154.html

– Davio – 2013-11-27T14:22:32.223

5

PHP, HQ9+, Brainfuck and Bash (23 points, I think)

Looks like PHP, but...

Abuse of STDERR redirection and quotes.

what the >&_@ is a "<?php
//Boring, really ( ;[ ). This just prints Hello World!"
echo "Hello, World!"; //]&> are random characters that I like putting in"
?>" program?

This runs and does things in all the languages listed, but looks like (slightly weird) PHP code.

In Bash, it merely prints "Hello, World!" (creating junk files along the way).

In HQ9+, it prints Hello World a few times (once for every occurence of H or h).

In Brainfuck, it is a "cat program" - it reads from standard input and outputs its input to standard output (><,[.,]>>).

In PHP, it prints some text:

what the >&_@ is a "Hello, World!" program?

Bash Explanation

>&_@ is disguised as harmless text substituting a curse, but it is in fact a redirection of STDERR to the file _@, so that Bash doesn't complain that what is not a valid program. It also eats the is a <?php bit and the first comment.

The first comment has a quote at the end (the quotes are there to prevent <?php from being run). echo "Hello, World!"; runs normally in both languages. The next comment serves to hide another quote, and there is an output redirection from STDERR to the file are, to eat the message " //: Is a directory".

Since the closing ?> is in quotes, it doesn't get run, and gets eaten along with the second comment and the bit after it.

user16402

Posted 2013-11-22T20:01:46.210

Reputation:

4

JavaScript, 3p

<!--
$res = Array();
$ks  = Array("", "", "");

function convert($x) { return $x["toString"](36); }
for ($i=0; $i < 1000000; $i++) {
  this[convert($i)] = $i;

  if ($i == 626 || $i == 777 || $i == 865 || $i == 1176 || $i == 35761)
    $res[$res.length] = convert($i);

  if ($i == 381  || $i == 19145) $ks[0] += convert($i);
  if ($i == 19   || $i == 31775) $ks[1] += convert($i);
  if ($i == 1033 || $i == 27893) $ks[2] += convert($i);
}
this[$ks[0]]($res[$ks[1]]("")[$ks[2]](1)[$ks[1]](" "));
-->

yay

<h1>hi</h1>   /// heading
<p>lol</p>    /// content

Masquerades the output of a misconfigured web server, as (something that at first glance looks like) PHP+HTML mess.

This started out small but grew bigger as I tried to make it less obvious what it does (when evaluated as JS)... it might've become more obfuscated than the task intended, sorry about that. Anyway, the main thing I wanted to include was my trick for embedding (a subset of) HTML such that it's also valid JavaScript code, which is what you see at the end of the code.

Edit: oh, I should mention, when run in a website context the code alerts "hello world".

FireFly

Posted 2013-11-22T20:01:46.210

Reputation: 7 107

Aha I love the dollar sign vars. The javascript html is also really cool. – Kevin Cox – 2013-11-27T01:27:39.663

Hehe, the trick with the javascript HTML is that < and > are comparisons, /.../ is a regex literal (hence the triple-slashes for comments..) and JS doesn't care one bit about the types. – FireFly – 2013-11-27T06:49:36.160

2

Python/PHP/Lua/Perl/Perl 6/Ruby/JavaScript/HQ9+ (37 points)

print("Hello, World!")

This looks like "Hello, World" in Python, but it's actually a quine written in PHP. It also works in JavaScript (SpiderMonkey), Lua, HQ9+, Perl, Perl 6 and Ruby, for bonus points.

Konrad Borowski

Posted 2013-11-22T20:01:46.210

Reputation: 11 185

2

JavaScript/Python 3/PHP/Lua/Brainfuck/bash

This looks like... Umm... Obfuscated PHP?

#!,[.,]
echo = --[[0] ] [0][0] //1#>/dev/null;echo Hello, World!;exit # ]]0
--[[0] ] [0][0] //1#<?php for ($i = 99; $i > 1;) { printf("%d bottles of beer on the wall, %d bottles of beer.\nTake one down and pass it around. %d bottle%s of beer on the wall.\n\n", $i, $i, --$i, ($i != 1 ? "s" : "")); } print("1 bottle of beer on the wall, 1 bottle of beer.\nTake on e down and pass it around. No more bottles of beer on the wall.\n\nNo more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more. 99 bottles of beer on the wall.\n"); die(); ?>
2//2; print("Hello world!"); exit();"""
console.log("Hello, World!");/*]]
f = io.open(arg[0])
print(f:read("*a"))
--[[*///]]--"""
  • If you run it in bash, JavaScript or Python it'll print "Hello, World!"
  • If you run it in Lua, it'll print the script's source code (if provided as the first argument to lua).
  • If you run it in PHP it'll print the lyrics of 99 bottles of beer
  • If you run it as Brainfuck, it'll work like cat.

Score: 27

  • Follows the rules: 3
  • Other language can run: 1
  • Does something useful: 3 (cat program)
  • Also works in JS/Python 3/Lua/bash: 20

I hope I'm not bending the rules too much :)

Nikolaj

Posted 2013-11-22T20:01:46.210

Reputation: 21

0

Java written in C++

#include <stdio.h>

typedef char* String;

class P{
public:
    static void println(String s){
        puts(s);
    }
};

class S{
public:
    P out;
};


int main()
{
    S System;
    System.out.println("Hello world!");
    return 0;
}

Score 3 points.

bacchusbeale

Posted 2013-11-22T20:01:46.210

Reputation: 1 235

0

print`open(__file__).read()`, 1000 // len('''
/* Calculate code conciseness score for some random JavaScript */

function print() {
    console.log('Hello, world!');
}

<!--''') # ASCII submarine

This looks like Python 2, and runs as Python 2, printing its own source in Python string literal form, as well as a code conciseness score of some kind! When run as ES6, however, it prints “Hello, world!”.

Ry-

Posted 2013-11-22T20:01:46.210

Reputation: 5 283